Skip to content

Commit 8c0a1c7

Browse files
committed
Add tests for glob file patterns
1 parent d0f759d commit 8c0a1c7

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/cli/run/filter.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,24 @@ async fn collect_files_from_args(
374374

375375
Ok(files)
376376
}
377+
378+
#[cfg(test)]
379+
mod tests {
380+
use super::*;
381+
382+
fn glob_pattern(pattern: &str) -> FilePattern {
383+
serde_yaml::from_str::<FilePattern>(&format!("glob: {pattern}"))
384+
.expect("glob pattern should deserialize")
385+
}
386+
387+
#[test]
388+
fn filename_filter_supports_glob_include_and_exclude() {
389+
let include = glob_pattern("src/**/*.rs");
390+
let exclude = glob_pattern("src/**/ignored.rs");
391+
let filter = FilenameFilter::new(Some(&include), Some(&exclude));
392+
393+
assert!(filter.filter(Path::new("src/lib/main.rs")));
394+
assert!(!filter.filter(Path::new("src/lib/ignored.rs")));
395+
assert!(!filter.filter(Path::new("tests/main.rs")));
396+
}
397+
}

src/config.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,49 @@ mod tests {
10371037
assert!(parsed.exclude.is_match("dist/app"));
10381038
}
10391039

1040+
#[test]
1041+
fn file_patterns_expose_sources_and_display() {
1042+
let pattern: FilePattern = serde_yaml::from_str(indoc::indoc! {r"
1043+
glob:
1044+
- src/**/*.rs
1045+
- crates/**/src/**/*.rs
1046+
"})
1047+
.expect("glob list should parse");
1048+
assert_eq!(
1049+
pattern.sources(),
1050+
&[
1051+
"src/**/*.rs".to_string(),
1052+
"crates/**/src/**/*.rs".to_string()
1053+
]
1054+
);
1055+
assert_eq!(
1056+
pattern.sources_display(),
1057+
"src/**/*.rs, crates/**/src/**/*.rs"
1058+
);
1059+
assert!(pattern.is_match("src/main.rs"));
1060+
assert!(pattern.is_match("crates/foo/src/lib.rs"));
1061+
assert!(!pattern.is_match("tests/main.rs"));
1062+
}
1063+
1064+
#[test]
1065+
fn reject_empty_glob_list() {
1066+
let err = serde_yaml::from_str::<FilePattern>("glob: []").expect_err("empty list should fail");
1067+
assert!(
1068+
err.to_string().contains("glob list cannot be empty"),
1069+
"unexpected error: {err}"
1070+
);
1071+
}
1072+
1073+
#[test]
1074+
fn invalid_glob_pattern_errors() {
1075+
let err = serde_yaml::from_str::<FilePattern>("glob: \"[\"").expect_err("invalid glob should fail");
1076+
let msg = err.to_string().to_lowercase();
1077+
assert!(
1078+
msg.contains("glob"),
1079+
"error should mention glob issues: {msg}"
1080+
);
1081+
}
1082+
10401083
#[test]
10411084
fn parse_repos() {
10421085
// Local hook should not have `rev`

src/hooks/meta_hooks.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub fn identity(_hook: &Hook, filenames: &[&Path]) -> (i32, Vec<u8>) {
257257
#[cfg(test)]
258258
mod tests {
259259
use super::*;
260+
use prek_consts::{ALT_CONFIG_FILE, CONFIG_FILE};
260261
use crate::config::SerdeRegex;
261262

262263
fn regex_pattern(pattern: &str) -> FilePattern {
@@ -284,4 +285,22 @@ mod tests {
284285
let exclude = regex_pattern(r"^html/");
285286
assert!(excludes_any(&files, None, Some(&exclude)));
286287
}
288+
289+
#[test]
290+
fn meta_hook_patterns_cover_config_files() {
291+
let apply = MetaHook::from_id("check-hooks-apply").expect("known meta hook");
292+
let apply_files = apply.0.options.files.as_ref().expect("files should be set");
293+
assert!(apply_files.is_match(CONFIG_FILE));
294+
assert!(apply_files.is_match(ALT_CONFIG_FILE));
295+
296+
let useless = MetaHook::from_id("check-useless-excludes").expect("known meta hook");
297+
let useless_files = useless.0.options.files.as_ref().expect("files should be set");
298+
assert!(useless_files.is_match(CONFIG_FILE));
299+
assert!(useless_files.is_match(ALT_CONFIG_FILE));
300+
assert!(!useless_files.sources().is_empty());
301+
302+
let identity = MetaHook::from_id("identity").expect("known meta hook");
303+
assert!(identity.0.options.files.is_none());
304+
assert_eq!(identity.0.options.verbose, Some(true));
305+
}
287306
}

0 commit comments

Comments
 (0)