diff --git a/src/output.rs b/src/output.rs index 371a37a..d74d5da 100644 --- a/src/output.rs +++ b/src/output.rs @@ -201,7 +201,7 @@ impl<'a> OutputWriter<'a> { /// Write an IO error to stderr. pub fn report_io_error(&self, label: &OsStr, err: &io::Error) { - if !self.config.no_messages && !self.config.quiet { + if !self.config.no_messages { // Strip the trailing " (os error XX)" so the message matches GNU grep. let _ = writeln!( io::stderr(), diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 9f63e12..a38d570 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -252,14 +252,20 @@ fn quiet_match_overrides_file_error() { // With -q, a match makes grep exit 0 even if an earlier file could not be // opened. Without -q the missing file still yields exit 2, and -q with no // match keeps the error status. - // -q exits at the first match without draining stdin, so the harness's - // write can lose the race and fail with EPIPE; that error is expected here. + // -q still reports a file it cannot read, but files after the first match + // are never opened. -qs suppresses the report; the status is still 2. + #[cfg(not(windows))] + let expected = "grep: no-such-file: No such file or directory\n"; + #[cfg(windows)] + let expected = "grep: no-such-file: The system cannot find the file specified.\n"; let (_s, mut c) = ucmd(); c.args(&["-q", "abc", "no-such-file", "-"]) + // -q may exit before the harness finishes writing stdin (EPIPE). .ignore_stdin_write_error() .pipe_in("abcd\n") .succeeds() - .no_output(); + .no_stdout() + .stderr_is(expected); let (_s, mut c) = ucmd(); c.args(&["abc", "no-such-file", "-"]) @@ -269,7 +275,30 @@ fn quiet_match_overrides_file_error() { let (_s, mut c) = ucmd(); c.args(&["-q", "zzz", "no-such-file", "-"]) .pipe_in("abcd\n") - .fails_with_code(2); + .fails_with_code(2) + .no_stdout() + .stderr_is(expected); + + let (scene, mut c) = ucmd(); + scene.fixtures.write("hit", "x\n"); + c.args(&["-q", "x", "hit", "no-such-file"]) + .succeeds() + .no_output(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["-qs", "x", "no-such-file"]) + .fails_with_code(2) + .no_output(); + + #[cfg(unix)] + { + let (scene, mut c) = ucmd(); + scene.fixtures.mkdir("dir"); + c.args(&["-q", "x", "dir"]) + .fails_with_code(2) + .no_stdout() + .stderr_is("grep: dir: Is a directory\n"); + } } #[test]