Skip to content

Commit f56ca80

Browse files
authored
Dev command to run test & clean up (#56)
* feat: xtask to run test & clean up * feat(xtask): aliased command to run wasm test * typo * readme: wasm test clarity * xtask: test clean up only delete collection with `test_` prefix
1 parent 2d89530 commit f56ca80

File tree

7 files changed

+120
-2
lines changed

7 files changed

+120
-2
lines changed

.cargo/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[alias]
2-
xtask = "run --package xtask --"
2+
xtask = "run --package xtask --"
3+
test-clean = "run --package xtask -- test-clean"

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ sha2 = "0.10"
3333
thiserror = "2"
3434
url = "2"
3535
web-time = "=1.1.0"
36+
# dev dependencies
37+
tokio = { version = "1.5", features = ["macros", "rt", "rt-multi-thread"] }
38+

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,26 @@ docker run --rm \
4141
--additional-properties useSingleRequestParameter=true
4242
```
4343

44+
### Testing
45+
46+
Make sure you have a Typesense server up and running:
47+
48+
```bash
49+
docker compose up
50+
```
51+
52+
Then run this command in the root folder to run the integration tests:
53+
54+
```bash
55+
cargo test-clean -- --all-features
56+
```
57+
58+
This is an alias command which will run a script to clean up your Typesense server after the tests finish. You can pass any arguments of `cargo test` after the `--`.
59+
60+
To run test for wasm (chrome, headless):
61+
62+
```bash
63+
cargo test-clean --wasm
64+
```
65+
4466
If you'd like to contribute, please join our [Slack Community](https://join.slack.com/t/typesense-community/shared_invite/zt-mx4nbsbn-AuOL89O7iBtvkz136egSJg) and say hello!

typesense/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ trybuild = "1.0.42"
4848

4949
# native-only dev deps
5050
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
51-
tokio = { version = "1.5", features = ["macros", "rt", "rt-multi-thread"] }
51+
tokio = { workspace = true}
5252
wiremock = "0.6"
5353

5454
# wasm test deps

xtask/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ clap = { workspace = true }
1010
reqwest = { version = "0.12", features = ["blocking"] } # "blocking" is simpler for scripts
1111
serde = { workspace = true }
1212
serde_yaml = { workspace = true }
13+
typesense = { path = "../typesense"}
14+
tokio = { workspace = true}

xtask/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use clap::{Parser, ValueEnum};
33
use std::{env, fs, process::Command};
44
mod add_vendor_attributes;
55
mod preprocess_openapi;
6+
mod test_clean;
67
mod vendor_attributes;
8+
79
use preprocess_openapi::preprocess_openapi_file;
10+
use test_clean::test_clean;
811

912
const SPEC_URL: &str =
1013
"https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml";
@@ -27,6 +30,14 @@ struct Cli {
2730
/// The list of tasks to run in sequence.
2831
#[arg(required = true, value_enum)]
2932
tasks: Vec<Task>,
33+
34+
/// Flag to run tests for wasm.
35+
#[arg(long)]
36+
wasm: bool,
37+
38+
/// Arguments to forward to cargo test
39+
#[arg(last(true))]
40+
test_args: Vec<String>,
3041
}
3142

3243
#[derive(ValueEnum, Clone, Debug)]
@@ -38,18 +49,29 @@ enum Task {
3849
Fetch,
3950
/// Preprocesses fetched OpenAPI spec file into a new one
4051
Preprocess,
52+
/// Clean up test artifacts, e.g., collections
53+
TestClean,
4154
}
4255

4356
fn main() -> Result<()> {
4457
let cli = Cli::parse();
4558

59+
let rt = tokio::runtime::Runtime::new().unwrap();
60+
4661
for task in cli.tasks {
4762
println!("▶️ Running task: {:?}", task);
4863
match task {
4964
Task::CodeGen => task_codegen()?,
5065
Task::Fetch => task_fetch_api_spec()?,
5166
Task::Preprocess => preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE)
5267
.expect("Preprocess failed, aborting!"),
68+
Task::TestClean => {
69+
let test_args = cli.test_args.clone();
70+
let is_wasm = cli.wasm;
71+
rt.block_on(async move {
72+
test_clean(is_wasm, test_args).await;
73+
});
74+
}
5375
}
5476
}
5577
Ok(())

xtask/src/test_clean.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::time::Duration;
2+
use typesense::{Client, ExponentialBackoff, models::GetCollectionsParameters};
3+
4+
async fn clean_test_artifacts() {
5+
let client = Client::builder()
6+
.nodes(vec!["http://localhost:8108"])
7+
.api_key("xyz")
8+
.healthcheck_interval(Duration::from_secs(5))
9+
.retry_policy(ExponentialBackoff::builder().build_with_max_retries(3))
10+
.connection_timeout(Duration::from_secs(3))
11+
.build()
12+
.expect("Failed to create Typesense client");
13+
14+
let collections = client
15+
.collections()
16+
.retrieve(GetCollectionsParameters::new())
17+
.await
18+
.expect("Get all collections failed!");
19+
20+
println!("Cleaning up test collections...");
21+
22+
let mut collection_count = 0;
23+
24+
for collection in collections.iter() {
25+
if !collection.name.starts_with("test_") {
26+
continue;
27+
}
28+
29+
if let Err(err) = client
30+
.collection_schemaless(&collection.name)
31+
.delete()
32+
.await
33+
{
34+
eprintln!("Failed to delete {}: {}", collection.name, err);
35+
} else {
36+
collection_count += 1;
37+
println!("Deleted {}", collection.name);
38+
}
39+
}
40+
println!("Deleted {} test collections.", collection_count);
41+
println!("✅ Cleanup complete.");
42+
}
43+
44+
pub async fn test_clean(is_wasm: bool, args: Vec<String>) {
45+
let status = if is_wasm {
46+
println!("Running wasm-pack test...");
47+
std::process::Command::new("wasm-pack")
48+
.arg("test")
49+
.arg("--headless")
50+
.arg("--chrome")
51+
.args(&args)
52+
.arg("typesense")
53+
.status()
54+
.expect("Failed to run wasm-pack test")
55+
} else {
56+
println!("Running cargo test with arguments: {}", args.join(" "));
57+
std::process::Command::new("cargo")
58+
.arg("test")
59+
.args(&args)
60+
.status()
61+
.expect("Failed to run cargo test")
62+
};
63+
64+
clean_test_artifacts().await;
65+
66+
// Propagate cargo test exit code
67+
std::process::exit(status.code().unwrap_or(1));
68+
}

0 commit comments

Comments
 (0)