Skip to content
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
2 changes: 1 addition & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
37 changes: 33 additions & 4 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "-"])
Expand All @@ -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]
Expand Down
Loading