Skip to content

Commit

Permalink
Add tests for types, types_or, exclude_types
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Oct 29, 2024
1 parent 10328bd commit eb5440a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator};
use regex::Regex;
use thiserror::Error;
use tokio::process::Command;
use tracing::{debug, error};
use tracing::{debug, error, trace};
use unicode_width::UnicodeWidthStr;
use url::Url;

Expand Down Expand Up @@ -662,9 +662,13 @@ async fn run_hook(
let filenames: Vec<_> = filenames
.filter(|&filename| {
let path = Path::new(filename);
// TODO: log error?
let file_tags = tags_from_path(path).unwrap_or_default();
filter.filter(&file_tags)
match tags_from_path(path) {
Ok(tags) => filter.filter(&tags),
Err(err) => {
trace!("Failed to get tags for {filename}: {err}");
false
}
}
})
.collect();

Expand Down
2 changes: 2 additions & 0 deletions src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,5 +808,7 @@ mod tests {
fn tags_from_filename() {
let tags = super::tags_from_filename(Path::new("test.py"));
assert_eq!(tags, vec!["python", "text"]);
let tags = super::tags_from_filename(Path::new("data.json"));
assert_eq!(tags, vec!["json", "text"]);
}
}
76 changes: 70 additions & 6 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ fn skips() -> Result<()> {
Ok(())
}

/// Test global `files`, `exclude`, and hook level `files`, `exclude`.
#[test]
fn files_and_exclude() -> Result<()> {
let context = TestContext::new();
Expand Down Expand Up @@ -264,7 +265,7 @@ fn files_and_exclude() -> Result<()> {
- id: trailing-whitespace
files: valid.json
- id: end-of-file-fixer
exclude: (valid.json|file.txt)
exclude: (valid.json|main.py)
- id: check-json
"
})?;
Expand All @@ -285,17 +286,80 @@ fn files_and_exclude() -> Result<()> {
- exit code: 1
- files were modified by this hook
Fixing valid.json
fix end of files.........................................................Failed
- hook id: end-of-file-fixer
fix end of files.........................................................Passed
check json...............................................................Passed
----- stderr -----
"#);

Ok(())
}

/// Test selecting files by type, `types`, `types_or`, and `exclude_types`.
#[test]
fn file_types() -> 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
types: [ "json" ]
- id: trailing-whitespace
types_or: [ "json", "python" ]
- id: trailing-whitespace
exclude_types: [ "json" ]
- id: trailing-whitespace
types: [ "json" ]
exclude_types: [ "json" ]
"#
})?;

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 main.py
check json...............................................................Passed
trim trailing whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook
Fixing file.txt
trim trailing whitespace.............................(no files to check)Skipped
----- stderr -----
"#);

Ok(())
}

// TODO: test `types`, `types_or`, `exclude_types`

0 comments on commit eb5440a

Please sign in to comment.