1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

df: fix block size header for multiples of 1024

Correct the column header printed by `df` when the `--block-size`
argument has a value that is a multiple of 1024. After this commit,
the header looks like "1K" or "4M" or "117G", etc., depending on the
particular value of the block size. For example:

    $ df --block-size=1024 | head -n1
    Filesystem                1K-blocks     Used Available Use% Mounted on
    $ df --block-size=2048 | head -n1
    Filesystem                2K-blocks     Used Available Use% Mounted on
    $ df --block-size=3072 | head -n1
    Filesystem                3K-blocks     Used Available Use% Mounted on
    $ df --block-size=4096 | head -n1
    Filesystem                4K-blocks     Used Available Use% Mounted on
This commit is contained in:
Jeffrey Finkelstein 2022-02-23 21:28:07 -05:00
parent 14e3f50651
commit 5cf7139467
4 changed files with 225 additions and 25 deletions

View file

@ -28,6 +28,8 @@ fn test_df_compatible_si() {
#[test]
fn test_df_output() {
// TODO These should fail because `-total` should have two dashes,
// not just one. But they don't fail.
if cfg!(target_os = "macos") {
new_ucmd!().arg("-H").arg("-total").succeeds().
stdout_only("Filesystem Size Used Available Capacity Use% Mounted on \n");
@ -121,4 +123,31 @@ fn test_total() {
// TODO We could also check here that the use percentage matches.
}
#[test]
fn test_block_size_1024() {
fn get_header(block_size: u64) -> String {
// TODO When #3057 is resolved, we should just use
//
// new_ucmd!().arg("--output=size").succeeds().stdout_move_str();
//
// instead of parsing the entire `df` table as a string.
let output = new_ucmd!()
.args(&["-B", &format!("{}", block_size)])
.succeeds()
.stdout_move_str();
let first_line = output.lines().next().unwrap();
let mut column_labels = first_line.split_whitespace();
let size_column_label = column_labels.nth(1).unwrap();
size_column_label.into()
}
assert_eq!(get_header(1024), "1K-blocks");
assert_eq!(get_header(2048), "2K-blocks");
assert_eq!(get_header(4096), "4K-blocks");
assert_eq!(get_header(1024 * 1024), "1M-blocks");
assert_eq!(get_header(2 * 1024 * 1024), "2M-blocks");
assert_eq!(get_header(1024 * 1024 * 1024), "1G-blocks");
assert_eq!(get_header(34 * 1024 * 1024 * 1024), "34G-blocks");
}
// ToDO: more tests...