8000 df: always round up usage percentage (#3208) by cakebaker · Pull Request #3230 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

df: always round up usage percentage (#3208) #3230

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

Merged
merged 1 commit into from
Mar 8, 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
32 changes: 31 additions & 1 deletion src/uu/df/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand Down Expand Up @@ -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 "
);
}
}
35 changes: 34 additions & 1 deletion tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
0