Show stderr on exec with `rm -i` like `find`

This commit is contained in:
Laurent Arnoud 2017-12-03 22:59:36 +01:00
parent 8914b43845
commit 3b8c87fb65
No known key found for this signature in database
GPG Key ID: A79DAB2374F95DD5
3 changed files with 48 additions and 8 deletions

View File

@ -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;
@ -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]);

View File

@ -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,
@ -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);
@ -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));
}
}
}

View File

@ -14,6 +14,7 @@ mod testenv;
use testenv::TestEnv;
use regex::escape;
use std::env;
fn get_absolute_root_path(env: &TestEnv) -> String {
let path = env.test_root()
@ -93,6 +94,7 @@ fn test_explicit_root_path() {
../../one/two/C.Foo2
../../one/two/three/d.foo
../../one/two/three/directory_foo",
"",
);
te.assert_output_subdirectory(
@ -103,6 +105,7 @@ fn test_explicit_root_path() {
../three
../three/d.foo
../three/directory_foo",
"",
);
}
@ -505,6 +508,7 @@ fn test_symlink() {
{dir}/symlink",
dir = &parent_parent
),
"",
);
te.assert_output_subdirectory(
@ -519,6 +523,7 @@ fn test_symlink() {
dir = if cfg!(windows) { "symlink" } else { "one/two" },
abs_path = &abs_path
),
"",
);
te.assert_output(
@ -550,6 +555,7 @@ fn test_symlink() {
dir = if cfg!(windows) { "symlink" } else { "one/two" },
abs_path = &abs_path
),
"",
);
te.assert_output(
@ -639,6 +645,30 @@ fn test_exec() {
),
);
// On travis Trusty quotes are differents with `rm -i`
// a.foo instead of locally 'a.foo'
match env::var("TRAVIS_OS_NAME") {
Ok(val) => {
if val == "linux" {
te.assert_output_error(
&["a.foo", "--exec", "rm", "-i", "{}"],
"rm: remove regular empty file a.foo? ",
);
} else if val == "osx" {
te.assert_output_error(
&["a.foo", "--exec", "rm", "-i", "{}"],
"remove a.foo? ",
);
}
}
Err(_) => {
te.assert_output_error(
&["a.foo", "--exec", "rm", "-i", "{}"],
"rm: remove regular empty file 'a.foo'? ",
);
}
}
te.assert_output(
&["foo", "--exec", "echo", "{}"],
"a.foo