Skip to content

Commit 2eca023

Browse files
committed
Expand file created times test to check entire Metadata
1 parent 29cd8dd commit 2eca023

File tree

1 file changed

+88
-11
lines changed

1 file changed

+88
-11
lines changed

tests/fs_additional.rs

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ fn reopen_fd() {
874874
}
875875

876876
#[test]
877-
fn metadata_created() {
877+
fn metadata_vs_std_fs() {
878878
let tmpdir = tmpdir();
879879
check!(tmpdir.create_dir("dir"));
880880
let dir = check!(tmpdir.open_dir("dir"));
@@ -893,16 +893,93 @@ fn metadata_created() {
893893
let std_dir = check!(dir.into_std_file().metadata());
894894
let std_file = check!(file.into_std().metadata());
895895

896-
// If the standard library supports file creation times, then cap-std
897-
// should too.
898-
if let Ok(expected) = std_dir.created() {
899-
println!("std::fs supports file created times");
900-
assert_eq!(expected, check!(cap_std_dir.created()).into_std());
901-
} else {
902-
println!("std::fs doesn't support file created times");
896+
match std_dir.created() {
897+
Ok(_) => println!("std::fs supports file created times"),
898+
Err(e) => println!("std::fs doesn't support file created times: {}", e),
899+
}
900+
901+
check_metadata(&std_dir, &cap_std_dir);
902+
check_metadata(&std_file, &cap_std_file);
903+
check_metadata(&std_file, &cap_std_dir_entry);
904+
}
905+
906+
fn check_metadata(std: &std::fs::Metadata, cap: &cap_std::fs::Metadata) {
907+
assert_eq!(std.is_dir(), cap.is_dir());
908+
assert_eq!(std.is_file(), cap.is_file());
909+
assert_eq!(std.is_symlink(), cap.is_symlink());
910+
assert_eq!(std.file_type().is_dir(), cap.file_type().is_dir());
911+
assert_eq!(std.file_type().is_file(), cap.file_type().is_file());
912+
assert_eq!(std.file_type().is_symlink(), cap.file_type().is_symlink());
913+
if cfg!(unix) {
914+
use std::os::unix::fs::FileTypeExt;
915+
assert_eq!(
916+
std.file_type().is_block_device(),
917+
cap.file_type().is_block_device()
918+
);
919+
assert_eq!(
920+
std.file_type().is_char_device(),
921+
cap.file_type().is_char_device()
922+
);
923+
assert_eq!(std.file_type().is_fifo(), cap.file_type().is_fifo());
924+
assert_eq!(std.file_type().is_socket(), cap.file_type().is_socket());
925+
}
926+
927+
assert_eq!(std.len(), cap.len());
928+
929+
assert_eq!(std.permissions().readonly(), cap.permissions().readonly());
930+
if cfg!(unix) {
931+
use std::os::unix::fs::PermissionsExt;
932+
// The standard library returns file format bits with `mode()`, whereas
933+
// cap-std currently doesn't.
934+
assert_eq!(std.permissions().mode() & 0o7777, cap.permissions().mode());
903935
}
904-
if let Ok(expected) = std_file.created() {
905-
assert_eq!(expected, check!(cap_std_file.created()).into_std());
906-
assert_eq!(expected, check!(cap_std_dir_entry.created()).into_std());
936+
937+
// If the standard library supports file modified/accessed/created times,
938+
// then cap-std should too.
939+
if let Ok(expected) = std.modified() {
940+
assert_eq!(expected, check!(cap.modified()).into_std());
941+
}
942+
// The access times might be a little different due to either our own
943+
// or concurrent accesses.
944+
const ACCESS_TOLERANCE_SEC: u32 = 60;
945+
if let Ok(expected) = std.accessed() {
946+
let access_tolerance = std::time::Duration::from_secs(ACCESS_TOLERANCE_SEC.into());
947+
assert!(
948+
((expected - access_tolerance)..(expected + access_tolerance))
949+
.contains(&check!(cap.accessed()).into_std()),
950+
"std accessed {:#?}, cap accessed {:#?}",
951+
expected,
952+
cap.accessed()
953+
);
954+
}
955+
if let Ok(expected) = std.created() {
956+
assert_eq!(expected, check!(cap.created()).into_std());
957+
}
958+
959+
if cfg!(unix) {
960+
use std::os::unix::fs::MetadataExt;
961+
assert_eq!(std.dev(), cap.dev());
962+
assert_eq!(std.ino(), cap.ino());
963+
assert_eq!(std.mode(), cap.mode());
964+
assert_eq!(std.nlink(), cap.nlink());
965+
assert_eq!(std.uid(), cap.uid());
966+
assert_eq!(std.gid(), cap.gid());
967+
assert_eq!(std.rdev(), cap.rdev());
968+
assert_eq!(std.size(), cap.size());
969+
assert!(
970+
((std.atime() - i64::from(ACCESS_TOLERANCE_SEC))
971+
..(std.atime() + i64::from(ACCESS_TOLERANCE_SEC)))
972+
.contains(&cap.atime()),
973+
"std atime {}, cap atime {}",
974+
std.atime(),
975+
cap.atime()
976+
);
977+
assert!((0..1_000_000_000).contains(&cap.atime_nsec()));
978+
assert_eq!(std.mtime(), cap.mtime());
979+
assert_eq!(std.mtime_nsec(), cap.mtime_nsec());
980+
assert_eq!(std.ctime(), cap.ctime());
981+
assert_eq!(std.ctime_nsec(), cap.ctime_nsec());
982+
assert_eq!(std.blksize(), cap.blksize());
983+
assert_eq!(std.blocks(), cap.blocks());
907984
}
908985
}

0 commit comments

Comments
 (0)