Skip to content

Commit

Permalink
relax the refreshed test conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
dlowe committed Nov 27, 2024
1 parent 821bd0c commit 51b7017
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 67 deletions.
7 changes: 5 additions & 2 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,16 @@ fn new_disk(
DiskKind::Unknown(-1)
};

let (total_space, available_space, is_read_only) =
let (total_space, available_space, is_read_only) = if refresh_kind.details() {
match unsafe { load_statvfs_values(mount_point, refresh_kind) } {
Some((total_space, available_space, is_read_only)) => {
(total_space, available_space, is_read_only)
}
None => (0, 0, false),
};
}
} else {
(0, 0, false)
};

let is_removable = if refresh_kind.details() {
removable_entries
Expand Down
140 changes: 75 additions & 65 deletions tests/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,71 +53,81 @@ fn test_disk_refresh_kind() {
}

let assertions = |name: &'static str, disks: &Disks| {
for disk in disks.list().iter() {
if refreshes.kind() {
#[cfg(not(target_os = "freebsd"))]
assert_ne!(
disk.kind(),
DiskKind::Unknown(-1),
"{name}: disk.kind should be refreshed"
);
} else {
assert_eq!(
disk.kind(),
DiskKind::Unknown(-1),
"{name}: disk.kind should not be refreshed"
);
}

if refreshes.details() {
assert_ne!(
disk.available_space(),
Default::default(),
"{name}: disk.available_space should be refreshed"
);
assert_ne!(
disk.total_space(),
Default::default(),
"{name}: disk.total_space should be refreshed"
);
// We can't assert anything about booleans, since false is indistinguishable from
// not-refreshed
} else {
assert_eq!(
disk.available_space(),
Default::default(),
"{name}: disk.available_space should not be refreshed"
);
assert_eq!(
disk.total_space(),
Default::default(),
"{name}: disk.total_space should not be refreshed"
);
assert_eq!(
disk.is_read_only(),
<bool as Default>::default(),
"{name}: disk.is_read_only should not be refreshed"
);
assert_eq!(
disk.is_removable(),
<bool as Default>::default(),
"{name}: disk.is_removable should not be refreshed"
);
}

if refreshes.io_usage() {
assert_ne!(
disk.usage(),
Default::default(),
"{name}: disk.usage should be refreshed"
);
} else {
assert_eq!(
disk.usage(),
Default::default(),
"{name}: disk.usage should not be refreshed"
);
}
if refreshes.kind() {
// This would ideally assert that *all* are refreshed, but we settle for a weaker
// assertion because failures can't be distinguished from "not refreshed" values.
#[cfg(not(target_os = "freebsd"))]
assert!(
disks
.iter()
.any(|disk| disk.kind() != DiskKind::Unknown(-1)),
"{name}: disk.kind should be refreshed"
);
} else {
assert!(
disks
.iter()
.all(|disk| disk.kind() == DiskKind::Unknown(-1)),
"{name}: disk.kind should not be refreshed"
);
}

if refreshes.details() {
// These would ideally assert that *all* are refreshed, but we settle for a weaker
// assertion because failures can't be distinguished from "not refreshed" values.
assert!(
disks
.iter()
.any(|disk| disk.available_space() != Default::default()),
"{name}: disk.available_space should be refreshed"
);
assert!(
disks
.iter()
.any(|disk| disk.total_space() != Default::default()),
"{name}: disk.total_space should be refreshed"
);
// We can't assert anything about booleans, since false is indistinguishable from
// not-refreshed
} else {
assert!(
disks
.iter()
.all(|disk| disk.available_space() == Default::default()),
"{name}: disk.available_space should not be refreshed"
);
assert!(
disks
.iter()
.all(|disk| disk.total_space() == Default::default()),
"{name}: disk.total_space should not be refreshed"
);
assert!(
disks
.iter()
.all(|disk| disk.is_read_only() == <bool as Default>::default()),
"{name}: disk.is_read_only should not be refreshed"
);
assert!(
disks
.iter()
.all(|disk| disk.is_removable() == <bool as Default>::default()),
"{name}: disk.is_removable should not be refreshed"
);
}

if refreshes.io_usage() {
// This would ideally assert that *all* are refreshed, but we settle for a weaker
// assertion because failures can't be distinguished from "not refreshed" values.
assert!(
disks.iter().any(|disk| disk.usage() != Default::default()),
"{name}: disk.usage should be refreshed"
);
} else {
assert!(
disks.iter().all(|disk| disk.usage() == Default::default()),
"{name}: disk.usage should not be refreshed"
);
}
};

Expand Down

0 comments on commit 51b7017

Please sign in to comment.