Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement -noleaf #414

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
hanbings marked this conversation as resolved.
Show resolved Hide resolved
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());
}
Loading