1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

truncate: refactor parse_size() function

Change the interface provided by the `parse_size()` function to reduce
its responsibilities to just a single task: parsing a number of bytes
from a string of the form '123KB', etc. Previously, the function was
also responsible for deciding which mode truncate would operate in.

Furthermore, this commit simplifies the code for parsing the number and
unit to be less verbose and use less mutable state.

Finally, this commit adds some unit tests for the `parse_size()`
function.
This commit is contained in:
Jeffrey Finkelstein 2021-05-19 22:23:28 -04:00
parent 0c6a848314
commit 63b496eaa8
2 changed files with 122 additions and 59 deletions

View file

@ -235,3 +235,13 @@ fn test_size_and_reference() {
actual
);
}
#[test]
fn test_invalid_numbers() {
// TODO For compatibility with GNU, `truncate -s 0X` should cause
// the same error as `truncate -s 0X file`, but currently it returns
// a different error.
new_ucmd!().args(&["-s", "0X", "file"]).fails().stderr_contains("Invalid number: 0X");
new_ucmd!().args(&["-s", "0XB", "file"]).fails().stderr_contains("Invalid number: 0XB");
new_ucmd!().args(&["-s", "0B", "file"]).fails().stderr_contains("Invalid number: 0B");
}