From 247f2e55bdd5a6da3d34604eb7033fbd7df69b66 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 6 Jan 2024 16:54:29 +0100 Subject: [PATCH] seq: adjust some error messages. GNU's are better (#5798) * seq: adjust some error messages. GNU's are better tested by tests/seq/seq.pl * uucore: remove todo --------- Co-authored-by: Daniel Hofstetter --- src/uucore/src/lib/features/format/mod.rs | 23 ++++++++++++++++------- tests/by-util/test_seq.rs | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/uucore/src/lib/features/format/mod.rs b/src/uucore/src/lib/features/format/mod.rs index d213d0359..4d30753d6 100644 --- a/src/uucore/src/lib/features/format/mod.rs +++ b/src/uucore/src/lib/features/format/mod.rs @@ -57,8 +57,8 @@ pub enum FormatError { IoError(std::io::Error), NoMoreArguments, InvalidArgument(FormatArgument), - TooManySpecs, - NeedAtLeastOneSpec, + TooManySpecs(Vec), + NeedAtLeastOneSpec(Vec), WrongSpecType, } @@ -79,9 +79,16 @@ impl Display for FormatError { "%{}: invalid conversion specification", String::from_utf8_lossy(s) ), - // TODO: The next two should print the spec as well - Self::TooManySpecs => write!(f, "format has too many % directives"), - Self::NeedAtLeastOneSpec => write!(f, "format has no % directive"), + Self::TooManySpecs(s) => write!( + f, + "format '{}' has too many % directives", + String::from_utf8_lossy(s) + ), + Self::NeedAtLeastOneSpec(s) => write!( + f, + "format '{}' has no % directive", + String::from_utf8_lossy(s) + ), // TODO: Error message below needs some work Self::WrongSpecType => write!(f, "wrong % directive type was given"), Self::IoError(_) => write!(f, "io error"), @@ -303,7 +310,9 @@ impl Format { } let Some(spec) = spec else { - return Err(FormatError::NeedAtLeastOneSpec); + return Err(FormatError::NeedAtLeastOneSpec( + format_string.as_ref().to_vec(), + )); }; let formatter = F::try_from_spec(spec)?; @@ -312,7 +321,7 @@ impl Format { for item in &mut iter { match item? { FormatItem::Spec(_) => { - return Err(FormatError::TooManySpecs); + return Err(FormatError::TooManySpecs(format_string.as_ref().to_vec())); } FormatItem::Char(c) => suffix.push(c), } diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index da28181eb..9b0f9acea 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -766,3 +766,17 @@ fn test_invalid_zero_increment_value() { .no_stdout() .usage_error("invalid Zero increment value: '0'"); } + +#[test] +fn test_invalid_format() { + new_ucmd!() + .args(&["-f", "%%g", "1"]) + .fails() + .no_stdout() + .stderr_contains("format '%%g' has no % directive"); + new_ucmd!() + .args(&["-f", "%g%g", "1"]) + .fails() + .no_stdout() + .stderr_contains("format '%g%g' has too many % directives"); +}