From 4d5e2e96f81dcf7ec537e19a80b44abccf1a2cb0 Mon Sep 17 00:00:00 2001 From: Jiacai Liu Date: Mon, 13 Mar 2023 19:52:46 +0800 Subject: [PATCH] add env filter (#47) --- sqlness/Cargo.toml | 1 + sqlness/src/config.rs | 11 ++++++----- sqlness/src/error.rs | 3 +++ sqlness/src/runner.rs | 10 +++++++--- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/sqlness/Cargo.toml b/sqlness/Cargo.toml index 6494df0..99065c8 100644 --- a/sqlness/Cargo.toml +++ b/sqlness/Cargo.toml @@ -14,6 +14,7 @@ async-trait = "0.1" derive_builder = "0.11" mysql = { version = "23.0.1", optional = true } prettydiff = "0.6.2" +regex = "1.7.1" serde = { version = "1.0", features = ["derive"] } thiserror = "1.0" toml = "0.5" diff --git a/sqlness/src/config.rs b/sqlness/src/config.rs index 410aacd..c140e91 100644 --- a/sqlness/src/config.rs +++ b/sqlness/src/config.rs @@ -29,13 +29,14 @@ pub struct Config { #[builder(default = "true")] #[serde(default = "Config::default_fail_fast")] pub fail_fast: bool, - /// If specified, only run cases containing this string in their names. + /// Test only matched testcases, default `.*` + /// Env is prepended before filename, eg `{env}:{filename}` #[builder(default = "Config::default_test_filter()")] #[serde(default = "Config::default_test_filter")] pub test_filter: String, /// Whether follow symbolic links when searching test case files. - /// Defaults to "false" (not follow symbolic links). - #[builder(default = "false")] + /// Defaults to "true" (follow symbolic links). + #[builder(default = "true")] #[serde(default = "Config::default_follow_links")] pub follow_links: bool, } @@ -62,11 +63,11 @@ impl Config { } fn default_test_filter() -> String { - "".to_string() + ".*".to_string() } fn default_follow_links() -> bool { - false + true } } diff --git a/sqlness/src/error.rs b/sqlness/src/error.rs index 25efe66..5c5fb7e 100644 --- a/sqlness/src/error.rs +++ b/sqlness/src/error.rs @@ -26,6 +26,9 @@ pub enum SqlnessError { #[error("Run failed. {count} cases can't pass")] RunFailed { count: usize }, + + #[error("Invalid regexp, source error: {0}")] + Regex(#[from] regex::Error), } pub(crate) type Result = std::result::Result; diff --git a/sqlness/src/runner.rs b/sqlness/src/runner.rs index 93cd354..61ff6b4 100644 --- a/sqlness/src/runner.rs +++ b/sqlness/src/runner.rs @@ -9,6 +9,7 @@ use std::time::Instant; use prettydiff::basic::{DiffOp, SliceChangeset}; use prettydiff::diff_lines; +use regex::Regex; use walkdir::WalkDir; use crate::case::TestCase; @@ -221,6 +222,7 @@ impl Runner { let mut root = PathBuf::from_str(&self.config.case_dir).unwrap(); root.push(env); + let filter = Regex::new(&self.config.test_filter)?; let test_case_extension = self.config.test_case_extension.as_str(); let mut cases: Vec<_> = WalkDir::new(&root) .follow_links(self.config.follow_links) @@ -236,11 +238,13 @@ impl Runner { }) .map(|path| path.with_extension("")) .filter(|path| { - path.file_name() + let filename = path + .file_name() .unwrap_or_default() .to_str() - .unwrap_or_default() - .contains(&self.config.test_filter) + .unwrap_or_default(); + let filename_with_env = format!("{env}:{filename}"); + filter.is_match(&filename_with_env) }) .collect();