Skip to content

Commit

Permalink
add env filter (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 authored Mar 13, 2023
1 parent 5dd69f3 commit 4d5e2e9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions sqlness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions sqlness/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -62,11 +63,11 @@ impl Config {
}

fn default_test_filter() -> String {
"".to_string()
".*".to_string()
}

fn default_follow_links() -> bool {
false
true
}
}

Expand Down
3 changes: 3 additions & 0 deletions sqlness/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = std::result::Result<T, SqlnessError>;
10 changes: 7 additions & 3 deletions sqlness/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,6 +222,7 @@ impl<E: EnvController> Runner<E> {
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)
Expand All @@ -236,11 +238,13 @@ impl<E: EnvController> Runner<E> {
})
.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();

Expand Down

0 comments on commit 4d5e2e9

Please sign in to comment.