Skip to content

Commit

Permalink
add: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a-kenji committed Oct 3, 2023
1 parent 397e4bd commit 8d21bc9
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ treefmt [FLAGS] [OPTIONS] [--] [paths]...
`-H, --hidden`

> Also traverse hidden files (files that start with a .). This behaviour can be overridden with the `no-hidden` flag.
> Also traverse hidden files (files that start with a .). This behaviour can be overridden with the `--no-hidden` flag.
`--no-hidden`

Expand Down
123 changes: 123 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,55 @@ mod tests {
assert_eq!(stats.matched_files, 9);
}
#[test]
fn test_walker_some_matches_walk_hidden() {
let tmpdir = utils::tmp_mkdir();

let black = tmpdir.path().join("black");
let nixpkgs_fmt = tmpdir.path().join("nixpkgs-fmt");
let elm_fmt = tmpdir.path().join("elm-fmt");
utils::write_binary_file(&black, " ");
utils::write_binary_file(&nixpkgs_fmt, " ");
utils::write_binary_file(&elm_fmt, " ");
let tree_root = tmpdir.path();

let files = vec!["test", "test1", "test3", ".test4"];

for file in files {
utils::write_file(tree_root.join(format!("{file}.py")), " ");
utils::write_file(tree_root.join(format!("{file}.nix")), " ");
utils::write_file(tree_root.join(format!("{file}.elm")), " ");
utils::write_file(tree_root.join(file), " ");
}

let config = format!(
"
[formatter.python]
command = {black:?}
includes = [\"*.py\"]
[formatter.nix]
command = {nixpkgs_fmt:?}
includes = [\"*.nix\"]
[formatter.elm]
command = {elm_fmt:?}
options = [\"--yes\"]
includes = [\"*.elm\"]
"
);

let root = from_string(&config).unwrap();
let mut stats = Statistics::init();

let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap();

let walker = build_walker(vec![tree_root.to_path_buf()], true);
let _matches = collect_matches_from_walker(walker, &formatters, &mut stats);

assert_eq!(stats.traversed_files, 19);
assert_eq!(stats.matched_files, 12);
}
#[test]
fn test_walker_some_matches_specific_include() {
let tmpdir = utils::tmp_mkdir();

Expand Down Expand Up @@ -1507,4 +1556,78 @@ mod tests {
assert_eq!(python_matches, expected_python_matches);
assert!(nix_matches);
}
#[test]
fn test_walker_some_matches_exclude_gitignore_hidden() {
let tmpdir = utils::tmp_mkdir();

let black = tmpdir.path().join("black");
let nixpkgs_fmt = tmpdir.path().join("nixpkgs-fmt");
utils::write_binary_file(&black, " ");
utils::write_binary_file(&nixpkgs_fmt, " ");
let tree_root = tmpdir.path();
let git_dir = tree_root.join(".git");

utils::Git::new(tmpdir.path().to_path_buf())
.git_ignore("test1.nix\n.git/*.nix")
.exclude("result\n.direnv")
.create();

let files = vec!["test", "test1", ".test4"];

for file in files {
utils::write_file(tree_root.join(format!("{file}.py")), " ");
utils::write_file(tree_root.join(format!("{file}.nix")), " ");
utils::write_file(tree_root.join(format!("{file}.py")), " ");
utils::write_file(tree_root.join(format!("{file}.nix")), " ");
utils::write_file(git_dir.join(file), " ");
utils::write_file(tree_root.join(file), " ");
}
utils::write_file(tree_root.join("result"), " ");
utils::write_file(tree_root.join(".direnv"), " ");

let config = format!(
"
[formatter.python]
command = {black:?}
includes = [\"*.py\", \"test\"]
excludes = [\"test.py\" ]
[formatter.nix]
command = {nixpkgs_fmt:?}
includes = [\"*.nix\"]
excludes = [\"test.nix\"]
"
);

let root = from_string(&config).unwrap();
let mut stats = Statistics::init();

let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap();

let walker = build_walker(vec![tree_root.to_path_buf()], true);
let matches = collect_matches_from_walker(walker, &formatters, &mut stats);

assert_eq!(stats.traversed_files, 15);
assert_eq!(stats.matched_files, 5);
let python_matches: Vec<PathBuf> = matches
.get(&FormatterName::new("python"))
.unwrap()
.keys()
.cloned()
.collect();
let nix_matches: Vec<PathBuf> = matches
.get(&FormatterName::new("nix"))
.unwrap()
.keys()
.cloned()
.collect();
let expected_nix_matches: Vec<PathBuf> =
[".test4.nix"].iter().map(|p| tree_root.join(p)).collect();
let expected_python_matches: Vec<PathBuf> = [".git/test", ".test4.py", "test", "test1.py"]
.iter()
.map(|p| tree_root.join(p))
.collect();
assert_eq!(python_matches, expected_python_matches);
assert_eq!(nix_matches, expected_nix_matches);
}
}

0 comments on commit 8d21bc9

Please sign in to comment.