From 41b44f35225c8647646b1d8d76d3068edbe84a4d Mon Sep 17 00:00:00 2001 From: Baasit Date: Sat, 16 Sep 2023 09:53:08 +0100 Subject: [PATCH] feat: clap wrapper around sqlness (#2400) * feat: wrapped sqlness with clap to provide nice interface * fix: added spaces and changed -f flag to bool --- Cargo.lock | 1 + tests/runner/Cargo.toml | 1 + tests/runner/src/main.rs | 38 +++++++++++++++++++++++++++++--------- tests/runner/src/util.rs | 21 +++++++++++++-------- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ead7019e0976..c069c7e3c891 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9033,6 +9033,7 @@ name = "sqlness-runner" version = "0.4.0-nightly" dependencies = [ "async-trait", + "clap 4.4.1", "client", "common-base", "common-error", diff --git a/tests/runner/Cargo.toml b/tests/runner/Cargo.toml index 89ae0233a602..69894f30932c 100644 --- a/tests/runner/Cargo.toml +++ b/tests/runner/Cargo.toml @@ -6,6 +6,7 @@ license.workspace = true [dependencies] async-trait = "0.1" +clap = { version = "4.0", features = ["derive"] } client = { workspace = true } common-base = { workspace = true } common-error = { workspace = true } diff --git a/tests/runner/src/main.rs b/tests/runner/src/main.rs index 3f6a757ad3e2..8cf02d53668b 100644 --- a/tests/runner/src/main.rs +++ b/tests/runner/src/main.rs @@ -12,20 +12,39 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::path::PathBuf; + +use clap::Parser; use env::Env; use sqlness::{ConfigBuilder, Runner}; mod env; mod util; +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +/// SQL Harness for GrepTimeDB +struct Args { + /// Directory of test cases + #[clap(short, long)] + case_dir: Option, + + /// Fail this run as soon as one case fails if true + #[arg(short, long, default_value = "false")] + fail_fast: bool, + + /// Environment Configuration File + #[clap(short, long, default_value = "config.toml")] + env_config_file: String, + + /// Name of test cases to run. Accept as a regexp. + #[clap(short, long, default_value = ".*")] + test_filter: String, +} + #[tokio::main] async fn main() { - let mut args: Vec = std::env::args().collect(); - let test_filter = if args.len() > 1 { - args.pop().unwrap() - } else { - "".to_string() - }; + let args = Args::parse(); #[cfg(windows)] let data_home = std::env::temp_dir(); @@ -33,10 +52,11 @@ async fn main() { let data_home = std::path::PathBuf::from("/tmp"); let config = ConfigBuilder::default() - .case_dir(util::get_case_dir()) - .fail_fast(false) - .test_filter(test_filter) + .case_dir(util::get_case_dir(args.case_dir)) + .fail_fast(args.fail_fast) + .test_filter(args.test_filter) .follow_links(true) + .env_config_file(args.env_config_file) .build() .unwrap(); let runner = Runner::new(config, Env::new(data_home)); diff --git a/tests/runner/src/util.rs b/tests/runner/src/util.rs index a052b6e3e34b..c51b81db25bb 100644 --- a/tests/runner/src/util.rs +++ b/tests/runner/src/util.rs @@ -62,15 +62,20 @@ where /// Get the dir of test cases. This function only works when the runner is run /// under the project's dir because it depends on some envs set by cargo. -pub fn get_case_dir() -> String { - // retrieve the manifest runner (./tests/runner) - let mut runner_crate_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - - // change directory to cases' dir from runner's (should be runner/../cases) - let _ = runner_crate_path.pop(); - runner_crate_path.push("cases"); +pub fn get_case_dir(case_dir: Option) -> String { + let runner_path = match case_dir { + Some(path) => path, + None => { + // retrieve the manifest runner (./tests/runner) + let mut runner_crate_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + // change directory to cases' dir from runner's (should be runner/../cases) + let _ = runner_crate_path.pop(); + runner_crate_path.push("cases"); + runner_crate_path + } + }; - runner_crate_path.into_os_string().into_string().unwrap() + runner_path.into_os_string().into_string().unwrap() } /// Get the dir that contains workspace manifest (the top-level Cargo.toml).