Skip to content

Commit e976fae

Browse files
committed
Add more tests for pass_filenames
In particular, this adds: - A test for passing at most 2 files - Negative tests, checking the validation of strings and invalid integer values. Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent 1171596 commit e976fae

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

crates/prek/src/config.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,4 +2095,52 @@ mod tests {
20952095
let config = serde_saphyr::from_str::<Config>(yaml).unwrap();
20962096
insta::assert_debug_snapshot!(config);
20972097
}
2098+
2099+
#[test]
2100+
fn pass_filenames_zero_is_rejected() {
2101+
let yaml = indoc::indoc! {r"
2102+
repos:
2103+
- repo: local
2104+
hooks:
2105+
- id: invalid-pass-filenames-zero
2106+
name: invalid pass_filenames zero
2107+
entry: echo
2108+
language: system
2109+
pass_filenames: 0
2110+
"};
2111+
let result = serde_saphyr::from_str::<Config>(yaml);
2112+
assert!(result.is_err());
2113+
}
2114+
2115+
#[test]
2116+
fn pass_filenames_negative_is_rejected() {
2117+
let yaml = indoc::indoc! {r"
2118+
repos:
2119+
- repo: local
2120+
hooks:
2121+
- id: invalid-pass-filenames-negative
2122+
name: invalid pass_filenames negative
2123+
entry: echo
2124+
language: system
2125+
pass_filenames: -1
2126+
"};
2127+
let result = serde_saphyr::from_str::<Config>(yaml);
2128+
assert!(result.is_err());
2129+
}
2130+
2131+
#[test]
2132+
fn pass_filenames_string_is_rejected() {
2133+
let yaml = indoc::indoc! {r#"
2134+
repos:
2135+
- repo: local
2136+
hooks:
2137+
- id: invalid-pass-filenames-string
2138+
name: invalid pass_filenames string
2139+
entry: echo
2140+
language: system
2141+
pass_filenames: "foo"
2142+
"#};
2143+
let result = serde_saphyr::from_str::<Config>(yaml);
2144+
assert!(result.is_err());
2145+
}
20982146
}

crates/prek/tests/run.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3109,7 +3109,7 @@ fn run_with_tree_object_as_ref() -> Result<()> {
31093109
/// `pass_filenames: n` limits each invocation to at most n files.
31103110
/// With n=1, each matched file gets its own invocation.
31113111
#[test]
3112-
fn pass_filenames_integer_limits_batch_size() -> Result<()> {
3112+
fn pass_filenames_1_limits_batch_size() -> Result<()> {
31133113
let context = TestContext::new();
31143114
context.init_project();
31153115

@@ -3146,3 +3146,46 @@ fn pass_filenames_integer_limits_batch_size() -> Result<()> {
31463146

31473147
Ok(())
31483148
}
3149+
3150+
/// `pass_filenames: n` limits each invocation to at most n files.
3151+
/// With n=2 and more than 2 matching files, multiple batches are spawned.
3152+
#[test]
3153+
fn pass_filenames_2_limits_batch_size() -> Result<()> {
3154+
let context = TestContext::new();
3155+
context.init_project();
3156+
3157+
let cwd = context.work_dir();
3158+
// Use a script that errors if it receives more than two filename arguments.
3159+
context.write_pre_commit_config(indoc::indoc! {r#"
3160+
repos:
3161+
- repo: local
3162+
hooks:
3163+
- id: two-at-a-time
3164+
name: two at a time
3165+
entry: python -c "import sys; args = sys.argv[1:]; sys.exit(0 if len(args) <= 2 else 1)"
3166+
language: system
3167+
pass_filenames: 2
3168+
require_serial: true
3169+
verbose: true
3170+
"#});
3171+
3172+
cwd.child("a.txt").write_str("a")?;
3173+
cwd.child("b.txt").write_str("b")?;
3174+
cwd.child("c.txt").write_str("c")?;
3175+
cwd.child("d.txt").write_str("d")?;
3176+
cwd.child("e.txt").write_str("e")?;
3177+
context.git_add(".");
3178+
3179+
cmd_snapshot!(context.filters(), context.run().arg("--all-files"), @r"
3180+
success: true
3181+
exit_code: 0
3182+
----- stdout -----
3183+
two at a time............................................................Passed
3184+
- hook id: two-at-a-time
3185+
- duration: [TIME]
3186+
3187+
----- stderr -----
3188+
");
3189+
3190+
Ok(())
3191+
}

0 commit comments

Comments
 (0)