Skip to content

Commit

Permalink
Add test for fail_fast
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Oct 29, 2024
1 parent 820527f commit 4870707
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async fn run_hooks(
let (hook_success, new_diff) =
run_hook(hook, &filenames, skips, diff, columns, verbose, printer).await?;

success |= hook_success;
success &= hook_success;
diff = new_diff;
if !success && (fail_fast || hook.fail_fast) {
break;
Expand Down
62 changes: 62 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,65 @@ fn file_types() -> Result<()> {

Ok(())
}

/// Abort the run if a hook fails.
#[test]
fn fail_fast() -> Result<()> {
let context = TestContext::new();

context.init_project();

let cwd = context.workdir();
cwd.child("file.txt").write_str("Hello, world! ")?;
cwd.child("json.json").write_str("{}\n ")?;
cwd.child("main.py").write_str(r#"print "abc" "#)?;

// Global files and exclude.
context
.workdir()
.child(".pre-commit-config.yaml")
.write_str(indoc::indoc! {r#"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
fail_fast: false
types: [ "json" ]
- id: trailing-whitespace
fail_fast: true
- id: trailing-whitespace
- id: trailing-whitespace
"#
})?;

Command::new("git")
.arg("add")
.arg(".")
.current_dir(cwd)
.assert()
.success();

cmd_snapshot!(context.filters(), context.run(), @r#"
success: true
exit_code: 0
----- stdout -----
Cloning https://github.com/pre-commit/[email protected]
Installing environment for https://github.com/pre-commit/[email protected]
trim trailing whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook
Fixing json.json
trim trailing whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook
Fixing file.txt
Fixing main.py
----- stderr -----
"#);

Ok(())
}

0 comments on commit 4870707

Please sign in to comment.