mirror of
https://github.com/uutils/grep.git
synced 2026-06-10 16:15:11 -07:00
grep: strip trailing (os error N) from file error messages to match GNU (#6)
This commit is contained in:
+7
-1
@@ -8,6 +8,7 @@ use crate::context_buffer::LineView;
|
||||
use std::ffi::OsStr;
|
||||
use std::io::{self, BufWriter, StdoutLock, Write};
|
||||
use std::path::Path;
|
||||
use uucore::error::strip_errno;
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
const BUF_SIZE: usize = 128 * 1024;
|
||||
@@ -195,7 +196,12 @@ 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 {
|
||||
eprintln!("grep: {label}: {err}", label = label.to_string_lossy());
|
||||
// Strip the trailing " (os error XX)" so the message matches GNU grep.
|
||||
eprintln!(
|
||||
"grep: {label}: {err}",
|
||||
label = label.to_string_lossy(),
|
||||
err = strip_errno(err)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1030,6 +1030,16 @@ fn nonexistent_file_is_error() {
|
||||
.stderr_contains("does-not-exist");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonexistent_file_error_has_no_os_error_suffix() {
|
||||
// GNU prints "grep: <file>: No such file or directory" with no
|
||||
// " (os error 2)" suffix; strip_errno keeps us byte-compatible.
|
||||
let (_s, mut c) = ucmd();
|
||||
c.args(&["x", "does-not-exist"])
|
||||
.fails_with_code(2)
|
||||
.stderr_is("grep: does-not-exist: No such file or directory\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dash_argument_means_stdin() {
|
||||
let (_s, mut c) = ucmd();
|
||||
|
||||
Reference in New Issue
Block a user