Skip to content

Commit

Permalink
Implement -noleaf (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbings authored Jul 9, 2024
1 parent a0b5713 commit 9c11f11
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,11 @@ fn build_matcher_tree(

return Ok((i, top_level_matcher.build()));
}
"-noleaf" => {
// No change of behavior
config.no_leaf_dirs = true;
None
}
"-d" | "-depth" => {
// TODO add warning if it appears after actual testing criterion
config.depth_first = true;
Expand Down
17 changes: 17 additions & 0 deletions src/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Config {
sorted_output: bool,
help_requested: bool,
version_requested: bool,
no_leaf_dirs: bool,
}

impl Default for Config {
Expand All @@ -33,6 +34,10 @@ impl Default for Config {
sorted_output: false,
help_requested: false,
version_requested: false,
// Directory information and traversal are done by walkdir,
// and this configuration field will exist as
// a compatibility item for GNU findutils.
no_leaf_dirs: false,
}
}
}
Expand Down Expand Up @@ -156,6 +161,7 @@ fn process_dir<'a>(
}
Ok(entry) => {
let mut matcher_io = matchers::MatcherIO::new(deps);

if matcher.matches(&entry, &mut matcher_io) {
found_count += 1;
}
Expand Down Expand Up @@ -1219,4 +1225,15 @@ mod tests {

assert_eq!(rc, 0);
}

#[test]
#[cfg(unix)]
fn test_noleaf() {
use crate::find::tests::FakeDependencies;

let deps = FakeDependencies::new();
let rc = find_main(&["find", "./test_data/simple/subdir", "-noleaf"], &deps);

assert_eq!(rc, 0);
}
}
12 changes: 12 additions & 0 deletions tests/find_cmd_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,15 @@ fn find_samefile() {
.stdout(predicate::str::is_empty())
.stderr(predicate::str::contains("not-exist-file"));
}

#[test]
#[serial(working_dir)]
fn find_noleaf() {
Command::cargo_bin("find")
.expect("found binary")
.args(["test_data/simple/subdir", "-noleaf"])
.assert()
.success()
.stdout(predicate::str::contains("test_data/simple/subdir"))
.stderr(predicate::str::is_empty());
}

0 comments on commit 9c11f11

Please sign in to comment.