dd: Error message of invalid args is matched with GNU (#3831)

This commit is contained in:
Przemysław Fuchs 2022-08-17 11:40:42 +02:00 committed by GitHub
parent 9fad6fde35
commit 3acbd1c048
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 3 deletions

View file

@ -146,7 +146,7 @@ impl Input<File> {
}
opts.open(fname)
.map_err_context(|| "failed to open input file".to_string())?
.map_err_context(|| format!("failed to open {}", fname.quote()))?
};
// The --skip and --iseek flags are additive. On a file, they seek.

View file

@ -35,6 +35,7 @@ pub enum ParseError {
IbsOutOfRange,
ObsOutOfRange,
CbsOutOfRange,
InvalidNumber(String),
}
impl ParseError {
@ -56,6 +57,7 @@ impl ParseError {
Self::IbsOutOfRange => Self::IbsOutOfRange,
Self::ObsOutOfRange => Self::ObsOutOfRange,
Self::CbsOutOfRange => Self::CbsOutOfRange,
Self::InvalidNumber(_) => Self::InvalidNumber(s),
}
}
}
@ -79,7 +81,12 @@ impl std::fmt::Display for ParseError {
write!(f, "Only one ov conv=excl or conv=nocreat may be specified")
}
Self::FlagNoMatch(arg) => {
write!(f, "Unrecognized iflag=FLAG or oflag=FLAG -> {}", arg)
// Additional message about 'dd --help' is displayed only in this situation.
write!(
f,
"invalid input flag: {}\nTry 'dd --help' for more information.",
arg
)
}
Self::ConvFlagNoMatch(arg) => {
write!(f, "Unrecognized conv=CONV -> {}", arg)
@ -115,6 +122,9 @@ impl std::fmt::Display for ParseError {
Self::Unimplemented(arg) => {
write!(f, "feature not implemented on this system -> {}", arg)
}
Self::InvalidNumber(arg) => {
write!(f, "invalid number: {}", arg)
}
}
}
}
@ -389,7 +399,7 @@ fn parse_bytes_no_x(s: &str) -> Result<u64, ParseError> {
(None, None, None) => match uucore::parse_size::parse_size(s) {
Ok(n) => (n, 1),
Err(ParseSizeError::InvalidSuffix(s)) | Err(ParseSizeError::ParseFailure(s)) => {
return Err(ParseError::MultiplierStringParseFailure(s))
return Err(ParseError::InvalidNumber(s))
}
Err(ParseSizeError::SizeTooBig(s)) => {
return Err(ParseError::MultiplierStringOverflow(s))

View file

@ -1187,3 +1187,62 @@ fn test_final_stats_si_iec() {
let s = result.stderr_str();
assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1 KB, 1024 B) copied,"));
}
#[test]
fn test_invalid_number_arg_gnu_compatibility() {
let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"];
for command in commands {
new_ucmd!()
.args(&[format!("{}=", command)])
.fails()
.stderr_is("dd: invalid number: ");
new_ucmd!()
.args(&[format!("{}=29d", command)])
.fails()
.stderr_is("dd: invalid number: 29d");
}
}
#[test]
fn test_invalid_flag_arg_gnu_compatibility() {
let commands = vec!["iflag", "oflag"];
for command in commands {
new_ucmd!()
.args(&[format!("{}=", command)])
.fails()
.stderr_is("dd: invalid input flag: \nTry 'dd --help' for more information.");
new_ucmd!()
.args(&[format!("{}=29d", command)])
.fails()
.stderr_is("dd: invalid input flag: 29d\nTry 'dd --help' for more information.");
}
}
#[test]
fn test_invalid_file_arg_gnu_compatibility() {
new_ucmd!()
.args(&["if="])
.fails()
.stderr_is("dd: failed to open '': No such file or directory");
new_ucmd!()
.args(&["if=81as9bn8as9g302az8ns9.pdf.zip.pl.com"])
.fails()
.stderr_is(
"dd: failed to open '81as9bn8as9g302az8ns9.pdf.zip.pl.com': No such file or directory",
);
new_ucmd!()
.args(&["of="])
.fails()
.stderr_is("dd: failed to open '': No such file or directory");
new_ucmd!()
.args(&["of=81as9bn8as9g302az8ns9.pdf.zip.pl.com"])
.pipe_in("")
.succeeds();
}