Skip to content

Commit

Permalink
Show stderr on exec with rm -i like find
Browse files Browse the repository at this point in the history
  • Loading branch information
spk committed Dec 3, 2017
1 parent c165ff0 commit cb4e1b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/exec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {
/// Executes a command.
#[cfg(all(unix))]
pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {
use libc::{close, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};
use libc::{close, dup2, pipe, STDOUT_FILENO};
use std::fs::File;
use std::os::unix::process::CommandExt;
use std::os::unix::io::FromRawFd;
Expand All @@ -64,7 +64,6 @@ pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {
let child = cmd.before_exec(move || unsafe {
// Redirect the child's std{out,err} to the write ends of our pipe.
dup2(stdout_fds[1], STDOUT_FILENO);
dup2(stderr_fds[1], STDERR_FILENO);

// Close all the fds we created here, so EOF will be sent when the program exits.
close(stdout_fds[0]);
Expand Down
23 changes: 17 additions & 6 deletions tests/testenv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ impl TestEnv {

/// Assert that calling *fd* with the specified arguments produces the expected output.
pub fn assert_output(&self, args: &[&str], expected: &str) {
self.assert_output_subdirectory(".", args, expected)
self.assert_output_subdirectory(".", args, expected, "")
}

/// Assert that calling *fd* with the specified arguments produces the expected stderr.
pub fn assert_output_error(&self, args: &[&str], expected: &str) {
self.assert_output_subdirectory(".", args, "", expected)
}

/// Assert that calling *fd* in the specified path under the root working directory,
Expand All @@ -170,7 +175,8 @@ impl TestEnv {
&self,
path: P,
args: &[&str],
expected: &str,
expected_stdout: &str,
expected_stderr: &str,
) {
// Setup *fd* command.
let mut cmd = process::Command::new(&self.fd_exe);
Expand All @@ -186,12 +192,17 @@ impl TestEnv {
}

// Normalize both expected and actual output.
let expected = normalize_output(expected, true);
let actual = normalize_output(&String::from_utf8_lossy(&output.stdout), false);
let expected_stdout = normalize_output(expected_stdout, true);
let actual_stdout = normalize_output(&String::from_utf8_lossy(&output.stdout), false);
let expected_stderr = normalize_output(expected_stderr, true);
let actual_stderr = normalize_output(&String::from_utf8_lossy(&output.stderr), false);

// Compare actual output to expected output.
if expected != actual {
panic!(format_output_error(args, &expected, &actual));
if expected_stdout != actual_stdout {
panic!(format_output_error(args, &expected_stdout, &actual_stdout));
}
if expected_stderr != actual_stderr {
panic!(format_output_error(args, &expected_stderr, &actual_stderr));
}
}
}
10 changes: 10 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn test_explicit_root_path() {
../../one/two/C.Foo2
../../one/two/three/d.foo
../../one/two/three/directory_foo",
""
);

te.assert_output_subdirectory(
Expand All @@ -103,6 +104,7 @@ fn test_explicit_root_path() {
../three
../three/d.foo
../three/directory_foo",
""
);
}

Expand Down Expand Up @@ -505,6 +507,7 @@ fn test_symlink() {
{dir}/symlink",
dir = &parent_parent
),
""
);

te.assert_output_subdirectory(
Expand All @@ -519,6 +522,7 @@ fn test_symlink() {
dir = if cfg!(windows) { "symlink" } else { "one/two" },
abs_path = &abs_path
),
""
);

te.assert_output(
Expand Down Expand Up @@ -550,6 +554,7 @@ fn test_symlink() {
dir = if cfg!(windows) { "symlink" } else { "one/two" },
abs_path = &abs_path
),
""
);

te.assert_output(
Expand Down Expand Up @@ -639,6 +644,11 @@ fn test_exec() {
),
);

te.assert_output_error(
&["a.foo", "--exec", "rm", "-i", "{}"],
"rm: remove regular empty file 'a.foo'? ",
);

te.assert_output(
&["foo", "--exec", "echo", "{}"],
"a.foo
Expand Down

0 comments on commit cb4e1b6

Please sign in to comment.