feat: clap wrapper around sqlness (#2400)

* feat: wrapped sqlness with clap to provide nice interface

* fix: added spaces and changed -f flag to bool
This commit is contained in:
Baasit
2023-09-16 09:53:08 +01:00
committed by GitHub
parent 627c5b7419
commit 0a692aafb0
4 changed files with 44 additions and 17 deletions

1
Cargo.lock generated
View File

@@ -9033,6 +9033,7 @@ name = "sqlness-runner"
version = "0.4.0-nightly"
dependencies = [
"async-trait",
"clap 4.4.1",
"client",
"common-base",
"common-error",

View File

@@ -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 }

View File

@@ -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<PathBuf>,
/// 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<String> = 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));

View File

@@ -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"));
pub fn get_case_dir(case_dir: Option<PathBuf>) -> 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
}
};
// 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.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).