Skip to content
Open
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
26 changes: 23 additions & 3 deletions src/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,10 @@ fn process_dir(
}

let mut matcher_io = matchers::MatcherIO::for_walk(deps, dir.as_ref(), config);
// This is implemented for -execdir +.
if let Some(dir) = current_dir.take() {
matcher.finished_dir(dir.as_path(), &mut matcher_io);
}
matcher.finished(&mut matcher_io);
// This is implemented for exec +.
match matcher_io.exit_code() {
0 => {}
code => ret = code,
Expand Down Expand Up @@ -500,8 +499,15 @@ fn do_find(args: &[&str], deps: &dyn Dependencies) -> Result<i32, Box<dyn Error>

let mut ret = 0;
let mut quit = false;
let mut path_err = None;
for path in paths {
let path = path?;
let path = match path {
Ok(path) => path,
Err(err) => {
path_err = Some(err);
break;
}
};
let dir_ret = process_dir(
&path,
&paths_and_matcher.config,
Expand All @@ -520,6 +526,20 @@ fn do_find(args: &[&str], deps: &dyn Dependencies) -> Result<i32, Box<dyn Error>
ret = 1;
}

// Like GNU find, -exec ... + collects arguments across all the starting
// points, so the pending batch is only run once every path is done (or
// find stops early).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a test for the -exec ... + -quit case too?

let mut matcher_io = matchers::MatcherIO::new(deps);
paths_and_matcher.matcher.finished(&mut matcher_io);
match matcher_io.exit_code() {
0 => {}
code => ret = code,
}

if let Some(err) = path_err {
return Err(err);
}

Ok(ret)
}

Expand Down
126 changes: 126 additions & 0 deletions tests/find_exec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,132 @@ fn find_exec_multi() {
);
}

#[test]
fn find_exec_multi_across_starting_points() {
// -exec ... + collects the arguments from every starting point into one
// batch, as GNU find does, instead of running once per starting point.
let temp_dir = tempfile::Builder::new()
.prefix("find_exec_multi_across_starting_points")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let deps = FakeDependencies::new();

let rc = find_main(
&[
"find",
&fix_up_slashes("./test_data/simple/subdir"),
&fix_up_slashes("./test_data/depth/1"),
"-type",
"f",
"-exec",
&path_to_testing_commandline(),
temp_dir_path.as_ref(),
"--sort",
"{}",
"+",
],
&deps,
);

assert_eq!(rc, 0);
assert_eq!(deps.get_output_as_string(), "");

let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}\nargs=\n--sort\n./test_data/depth/1/2/3/f3\n./test_data/depth/1/2/f2\n./test_data/depth/1/f1\n./test_data/simple/subdir/ABBBC\n",
env::current_dir().unwrap().to_string_lossy()
))
);
// The command ran only once.
assert!(!temp_dir.path().join("2.txt").exists());
}

#[test]
fn find_exec_multi_with_quit() {
// -quit stops find early, but the pending -exec ... + batch still runs,
// once, with the arguments collected so far, as GNU find does.
let temp_dir = tempfile::Builder::new()
.prefix("find_exec_multi_with_quit")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let deps = FakeDependencies::new();

let rc = find_main(
&[
"find",
&fix_up_slashes("./test_data/simple/subdir"),
&fix_up_slashes("./test_data/depth/1"),
"-type",
"f",
"-exec",
&path_to_testing_commandline(),
temp_dir_path.as_ref(),
"--sort",
"{}",
"+",
"-quit",
],
&deps,
);

assert_eq!(rc, 0);
assert_eq!(deps.get_output_as_string(), "");

let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}\nargs=\n--sort\n./test_data/simple/subdir/ABBBC\n",
env::current_dir().unwrap().to_string_lossy()
))
);
// The command ran only once.
assert!(!temp_dir.path().join("2.txt").exists());
}

#[test]
fn find_exec_multi_failure_sets_exit_code() {
// A failing -exec ... + batch, which now runs after all the starting
// points, still makes find exit with status 1.
let temp_dir = tempfile::Builder::new()
.prefix("find_exec_multi_failure_sets_exit_code")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let deps = FakeDependencies::new();

let rc = find_main(
&[
"find",
&fix_up_slashes("./test_data/simple/subdir"),
&fix_up_slashes("./test_data/depth/1"),
"-type",
"f",
"-exec",
&path_to_testing_commandline(),
temp_dir_path.as_ref(),
"--exit_with_failure",
"{}",
"+",
],
&deps,
);

assert_eq!(rc, 1);
assert!(temp_dir.path().join("1.txt").exists());
assert!(!temp_dir.path().join("2.txt").exists());
}

#[test]
fn find_execdir_multi() {
let temp_dir = Builder::new()
Expand Down
13 changes: 13 additions & 0 deletions tests/test_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ fn files0_streams_before_invalid_utf8() {
.stderr_contains("invalid utf-8 sequence");
}

/// A pending `-exec ... +` batch still runs when reading a later
/// `-files0-from` entry fails, and the read error is still reported.
#[cfg(unix)]
#[test]
fn files0_read_error_still_runs_exec_plus_batch() {
ucmd()
.pipe_in(b"./test_data/simple/abbbc\0\xff\0" as &[u8])
.args(&["-files0-from", "-", "-exec", "echo", "{}", "+"])
.fails()
.stdout_is("./test_data/simple/abbbc\n")
.stderr_contains("invalid utf-8 sequence");
}

/// A starting point given on the command line can't be mixed with
/// -files0-from, even when it is `.`, the same path find uses by default.
#[test]
Expand Down
Loading