Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numfmt: show error if "i" suffix is missing #3713

Merged
merged 1 commit into from
Jul 13, 2022
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
10 changes: 9 additions & 1 deletion src/uu/numfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ fn parse_suffix(s: &str) -> Result<(f64, Option<Suffix>)> {

fn remove_suffix(i: f64, s: Option<Suffix>, u: &Unit) -> Result<f64> {
match (s, u) {
(None, _) => Ok(i),
(Some((raw_suffix, false)), &Unit::Auto) | (Some((raw_suffix, false)), &Unit::Si) => {
match raw_suffix {
RawSuffix::K => Ok(i * 1e3),
Expand All @@ -123,6 +122,15 @@ fn remove_suffix(i: f64, s: Option<Suffix>, u: &Unit) -> Result<f64> {
RawSuffix::Z => Ok(i * IEC_BASES[7]),
RawSuffix::Y => Ok(i * IEC_BASES[8]),
},
(None, &Unit::Iec(true)) => Err(format!(
"missing 'i' suffix in input: '{}' (e.g Ki/Mi/Gi)",
i
)),
(Some((raw_suffix, false)), &Unit::Iec(true)) => Err(format!(
"missing 'i' suffix in input: '{}{:?}' (e.g Ki/Mi/Gi)",
i, raw_suffix
)),
(None, _) => Ok(i),
(_, _) => Err("This suffix is unsupported for specified unit".to_owned()),
}
}
Expand Down
17 changes: 12 additions & 5 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ fn test_from_iec_i() {
}

#[test]
#[ignore] // FIXME: GNU from iec-i requires suffix
fn test_from_iec_i_requires_suffix() {
new_ucmd!()
.args(&["--from=iec-i", "1024"])
.fails()
.stderr_is("numfmt: missing 'i' suffix in input: '1024' (e.g Ki/Mi/Gi)");
let numbers = vec!["1024", "10M"];

for number in numbers {
new_ucmd!()
.args(&["--from=iec-i", number])
.fails()
.code_is(2)
.stderr_is(format!(
"numfmt: missing 'i' suffix in input: '{}' (e.g Ki/Mi/Gi)",
number
));
}
}

#[test]
Expand Down