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

Merge pull request #3230 from cakebaker/ticket_3208_2nd_attempt

df: always round up usage percentage (#3208)
This commit is contained in:
Sylvestre Ledru 2022-03-08 19:37:44 +01:00 committed by GitHub
commit b43db8b552
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 2 deletions

View file

@ -222,7 +222,7 @@ impl<'a> DisplayRow<'a> {
fn percentage(fraction: Option<f64>) -> String {
match fraction {
None => "-".to_string(),
Some(x) => format!("{:.0}%", 100.0 * x),
Some(x) => format!("{:.0}%", (100.0 * x).ceil()),
}
}
@ -551,4 +551,34 @@ mod tests {
"my_device my_type 4.0Ki 1.0Ki 3.0Ki 25% my_mount "
);
}
#[test]
fn test_row_display_round_up_usage() {
let options = Options {
block_size: BlockSize::Bytes(1),
..Default::default()
};
let row = Row {
fs_device: "my_device".to_string(),
fs_type: "my_type".to_string(),
fs_mount: "my_mount".to_string(),
bytes: 100,
bytes_used: 25,
bytes_free: 75,
bytes_usage: Some(0.251),
#[cfg(target_os = "macos")]
bytes_capacity: Some(0.5),
inodes: 10,
inodes_used: 2,
inodes_free: 8,
inodes_usage: Some(0.2),
};
assert_eq!(
DisplayRow::new(row, &options).to_string(),
"my_device 100 25 75 26% my_mount "
);
}
}

View file

@ -120,7 +120,40 @@ fn test_total() {
assert_eq!(computed_total_size, reported_total_size);
assert_eq!(computed_total_used, reported_total_used);
assert_eq!(computed_total_avail, reported_total_avail);
// TODO We could also check here that the use percentage matches.
}
#[test]
fn test_use_percentage() {
// Example output:
//
// Filesystem 1K-blocks Used Available Use% Mounted on
// udev 3858016 0 3858016 0% /dev
// ...
// /dev/loop14 63488 63488 0 100% /snap/core20/1361
let output = new_ucmd!().succeeds().stdout_move_str();
// Skip the header line.
let lines: Vec<&str> = output.lines().skip(1).collect();
for line in lines {
let mut iter = line.split_whitespace();
iter.next();
let reported_size = iter.next().unwrap().parse::<f64>().unwrap();
let reported_used = iter.next().unwrap().parse::<f64>().unwrap();
// Skip "Available" column
iter.next();
if cfg!(target_os = "macos") {
// Skip "Capacity" column
iter.next();
}
let reported_percentage = iter.next().unwrap();
let reported_percentage = reported_percentage[..reported_percentage.len() - 1]
.parse::<u8>()
.unwrap();
let computed_percentage = (100.0 * (reported_used / reported_size)).ceil() as u8;
assert_eq!(computed_percentage, reported_percentage);
}
}
#[test]