Skip to content

Commit 246dcbc

Browse files
committed
Add test for FileTimes
1 parent db90a0b commit 246dcbc

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

library/std/src/fs/tests.rs

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use crate::io::prelude::*;
22

33
use crate::env;
4-
use crate::fs::{self, File, OpenOptions};
4+
use crate::fs::{self, File, FileTimes, OpenOptions};
55
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
66
use crate::mem::MaybeUninit;
77
use crate::path::Path;
88
use crate::str;
99
use crate::sync::Arc;
1010
use crate::sys_common::io::test::{tmpdir, TempDir};
1111
use crate::thread;
12-
use crate::time::{Duration, Instant};
12+
use crate::time::{Duration, Instant, SystemTime};
1313

1414
use rand::RngCore;
1515

@@ -1633,3 +1633,53 @@ fn rename_directory() {
16331633
assert!(new_path.join("newdir").is_dir());
16341634
assert!(new_path.join("newdir/temp.txt").exists());
16351635
}
1636+
1637+
#[test]
1638+
fn test_file_times() {
1639+
#[cfg(target_os = "ios")]
1640+
use crate::os::ios::fs::FileTimesExt;
1641+
#[cfg(target_os = "macos")]
1642+
use crate::os::macos::fs::FileTimesExt;
1643+
#[cfg(target_os = "watchos")]
1644+
use crate::os::watchos::fs::FileTimesExt;
1645+
#[cfg(windows)]
1646+
use crate::os::windows::fs::FileTimesExt;
1647+
1648+
let tmp = tmpdir();
1649+
let file = File::create(tmp.join("foo")).unwrap();
1650+
let mut times = FileTimes::new();
1651+
let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345);
1652+
let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321);
1653+
times = times.set_accessed(accessed).set_modified(modified);
1654+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1655+
let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123);
1656+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1657+
{
1658+
times = times.set_created(created);
1659+
}
1660+
match file.set_times(times) {
1661+
// Allow unsupported errors on platforms which don't support setting times.
1662+
#[cfg(not(any(
1663+
windows,
1664+
all(
1665+
unix,
1666+
not(any(
1667+
target_os = "android",
1668+
target_os = "redox",
1669+
target_os = "espidf",
1670+
target_os = "horizon"
1671+
))
1672+
)
1673+
)))]
1674+
Err(e) if e.kind() == ErrorKind::Unsupported => return,
1675+
Err(e) => panic!("error setting file times: {e:?}"),
1676+
Ok(_) => {}
1677+
}
1678+
let metadata = file.metadata().unwrap();
1679+
assert_eq!(metadata.accessed().unwrap(), accessed);
1680+
assert_eq!(metadata.modified().unwrap(), modified);
1681+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1682+
{
1683+
assert_eq!(metadata.created().unwrap(), created);
1684+
}
1685+
}

0 commit comments

Comments
 (0)