From 2ca1a10e4d475171bfdc9ebf7e5b55395166351e Mon Sep 17 00:00:00 2001 From: Bipul Lamsal <66175991+BipulLamsal@users.noreply.github.com> Date: Fri, 10 Apr 2026 21:44:18 +0545 Subject: [PATCH] numfmt: fix --to=auto to return with exitcode=1 (#11701) * numfmt: fix --to=auto to return with exitcode=1 * numfmt: update direct variable use in format macro * numfmt_test: remove extra test * numfmt: replacing format in translate directly --- src/uu/numfmt/locales/en-US.ftl | 2 +- src/uu/numfmt/src/format.rs | 6 +++++- src/uu/numfmt/src/numfmt.rs | 12 +++++++----- tests/by-util/test_numfmt.rs | 6 +----- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/uu/numfmt/locales/en-US.ftl b/src/uu/numfmt/locales/en-US.ftl index c0e2c5722..b1a773ac5 100644 --- a/src/uu/numfmt/locales/en-US.ftl +++ b/src/uu/numfmt/locales/en-US.ftl @@ -68,7 +68,7 @@ numfmt-error-invalid-number = invalid number: { $input } numfmt-error-missing-i-suffix = missing 'i' suffix in input: '{ $number }{ $suffix }' (e.g Ki/Mi/Gi) numfmt-error-rejecting-suffix = rejecting suffix in input: '{ $number }{ $suffix }' (consider using --from) numfmt-error-suffix-unsupported-for-unit = This suffix is unsupported for specified unit -numfmt-error-unit-auto-not-supported-with-to = Unit 'auto' isn't supported with --to options +numfmt-error-invalid-unit-argument = invalid argument '{$arg}' for '{$opt}' numfmt-error-number-too-big = Number is too big and unsupported numfmt-error-format-no-percent = format '{ $format }' has no % directive numfmt-error-format-ends-in-percent = format '{ $format }' ends in % diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index f699536fd..28e32631c 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -475,7 +475,11 @@ fn consider_suffix( let (bases, with_i) = match u { Unit::Si => (si_bases_f64(), false), Unit::Iec(with_i) => (iec_bases_f64(), with_i), - Unit::Auto => return Err(translate!("numfmt-error-unit-auto-not-supported-with-to")), + Unit::Auto => { + return Err( + translate!("numfmt-error-invalid-unit-argument", "arg" => "auto", "opt" => "--to"), + ); + } Unit::None => return Ok((n, None)), }; diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index 7b3605d37..78202dcbd 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -165,14 +165,16 @@ fn handle_buffer(mut input: R, options: &NumfmtOptions) -> UResult Result { +fn parse_unit(s: &str, opt: &str) -> Result { match s { - "auto" => Ok(Unit::Auto), + "auto" if opt != TO => Ok(Unit::Auto), "si" => Ok(Unit::Si), "iec" => Ok(Unit::Iec(false)), "iec-i" => Ok(Unit::Iec(true)), "none" => Ok(Unit::None), - _ => Err(translate!("numfmt-error-unsupported-unit")), + value => Err( + translate!("numfmt-error-invalid-unit-argument", "arg" => value, "opt" => format!("--{opt}")), + ), } } @@ -237,8 +239,8 @@ fn parse_delimiter(arg: &OsString) -> Result> { } fn parse_options(args: &ArgMatches) -> Result { - let from = parse_unit(args.get_one::(FROM).unwrap())?; - let to = parse_unit(args.get_one::(TO).unwrap())?; + let from = parse_unit(args.get_one::(FROM).unwrap(), FROM)?; + let to = parse_unit(args.get_one::(TO).unwrap(), TO)?; let from_unit = parse_unit_size(args.get_one::(FROM_UNIT).unwrap())?; let to_unit = parse_unit_size(args.get_one::(TO_UNIT).unwrap())?; diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 132299954..4d63708ba 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -1411,12 +1411,8 @@ fn test_scientific_notation_rejected_by_gnu_issue_11655() { .stderr_contains("invalid suffix in input"); } -// https://github.com/uutils/coreutils/issues/11662 -// `--to=auto` is accepted at parse time by uutils then rejected at runtime -// with exit code 2; GNU rejects it in option parsing with exit code 1. #[test] -#[ignore = "GNU compat: see uutils/coreutils#11662"] -fn test_to_auto_rejected_at_parse_time_issue_11662() { +fn test_to_auto_rejected_at_parse_time() { new_ucmd!() .args(&["--to=auto", "100"]) .fails_with_code(1)