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

du: give -h output the same precision as GNU coreutils

When printing the `du -h` output GNU coreutils does autoscale
the size, e.g.
```
$ truncate -s12M a
$ truncate -s8500 b
$ truncate -s133456345 c
$ truncate -s56990456345 d
$ du -h --apparent-size a b c d
12M	a
8,4K	b
128M	c
54G	d
```
Align our version to do the same by sharing the code with `ls`.

Closes: #6159
This commit is contained in:
Michael Vogt 2024-04-01 09:59:21 +02:00 committed by Daniel Hofstetter
parent d07fb73630
commit 61e0450c66
3 changed files with 41 additions and 19 deletions

View file

@ -543,6 +543,34 @@ fn test_du_h_flag_empty_file() {
.stdout_only("0\tempty.txt\n");
}
#[test]
fn test_du_h_precision() {
let test_cases = [
(133456345, "128M"),
(12 * 1024 * 1024, "12M"),
(8500, "8.4K"),
];
for &(test_len, expected_output) in &test_cases {
let (at, mut ucmd) = at_and_ucmd!();
let fpath = at.plus("test.txt");
std::fs::File::create(&fpath)
.expect("cannot create test file")
.set_len(test_len)
.expect("cannot truncate test len to size");
ucmd.arg("-h")
.arg("--apparent-size")
.arg(&fpath)
.succeeds()
.stdout_only(format!(
"{}\t{}\n",
expected_output,
&fpath.to_string_lossy()
));
}
}
#[cfg(feature = "touch")]
#[test]
fn test_du_time() {