|
6 | 6 | mod sys_common;
|
7 | 7 |
|
8 | 8 | use cap_std::fs::{DirBuilder, OpenOptions};
|
| 9 | +use cap_std::time::SystemClock; |
9 | 10 | use std::io::{self, Read, Write};
|
10 | 11 | use std::path::Path;
|
11 | 12 | use std::str;
|
@@ -936,24 +937,51 @@ fn check_metadata(std: &std::fs::Metadata, cap: &cap_std::fs::Metadata) {
|
936 | 937 |
|
937 | 938 | // If the standard library supports file modified/accessed/created times,
|
938 | 939 | // then cap-std should too.
|
939 |
| - if let Ok(expected) = std.modified() { |
940 |
| - assert_eq!(expected, check!(cap.modified()).into_std()); |
| 940 | + match std.modified() { |
| 941 | + Ok(expected) => assert_eq!(expected, check!(cap.modified()).into_std()), |
| 942 | + Err(e) => assert!( |
| 943 | + cap.modified().is_err(), |
| 944 | + "modified time should be error ({}), got {:#?}", |
| 945 | + e, |
| 946 | + cap.modified() |
| 947 | + ), |
941 | 948 | }
|
942 | 949 | // The access times might be a little different due to either our own
|
943 | 950 | // or concurrent accesses.
|
944 | 951 | 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 | + match std.accessed() { |
| 953 | + Ok(expected) => { |
| 954 | + let access_tolerance = std::time::Duration::from_secs(ACCESS_TOLERANCE_SEC.into()); |
| 955 | + assert!( |
| 956 | + ((expected - access_tolerance)..(expected + access_tolerance)) |
| 957 | + .contains(&check!(cap.accessed()).into_std()), |
| 958 | + "std accessed {:#?}, cap accessed {:#?}", |
| 959 | + expected, |
| 960 | + cap.accessed() |
| 961 | + ); |
| 962 | + } |
| 963 | + Err(e) => assert!( |
| 964 | + cap.accessed().is_err(), |
| 965 | + "accessed time should be error ({}), got {:#?}", |
| 966 | + e, |
952 | 967 | cap.accessed()
|
953 |
| - ); |
| 968 | + ), |
954 | 969 | }
|
955 |
| - if let Ok(expected) = std.created() { |
956 |
| - assert_eq!(expected, check!(cap.created()).into_std()); |
| 970 | + match std.created() { |
| 971 | + Ok(expected) => assert_eq!(expected, check!(cap.created()).into_std()), |
| 972 | + Err(e) => { |
| 973 | + // An earlier bug returned the Unix epoch instead of `None` when |
| 974 | + // created times were unavailable. This tries to catch such errors, |
| 975 | + // while also allowing some targets to return valid created times |
| 976 | + // even when std doesn't. |
| 977 | + if let Ok(actual) = cap.created() { |
| 978 | + println!( |
| 979 | + "std returned error for created time ({}) but got {:#?}", |
| 980 | + e, actual |
| 981 | + ); |
| 982 | + assert_ne!(actual, SystemClock::UNIX_EPOCH); |
| 983 | + } |
| 984 | + } |
957 | 985 | }
|
958 | 986 |
|
959 | 987 | #[cfg(unix)]
|
|
0 commit comments