Skip to content

Commit f31c34c

Browse files
committed
Add test for FileTimes
1 parent 44fbf0c commit f31c34c

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

library/std/src/fs/tests.rs

+35-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

@@ -1629,3 +1629,36 @@ fn rename_directory() {
16291629
assert!(new_path.join("newdir").is_dir());
16301630
assert!(new_path.join("newdir/temp.txt").exists());
16311631
}
1632+
1633+
#[test]
1634+
fn test_file_times() {
1635+
#[cfg(target_os = "ios")]
1636+
use crate::os::ios::fs::FileTimesExt;
1637+
#[cfg(target_os = "macos")]
1638+
use crate::os::macos::fs::FileTimesExt;
1639+
#[cfg(target_os = "watchos")]
1640+
use crate::os::watchos::fs::FileTimesExt;
1641+
#[cfg(windows)]
1642+
use crate::os::windows::fs::FileTimesExt;
1643+
1644+
let tmp = tmpdir();
1645+
let file = File::create(tmp.join("foo")).unwrap();
1646+
let mut times = FileTimes::new();
1647+
let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345);
1648+
let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321);
1649+
times = times.set_accessed(accessed).set_modified(modified);
1650+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1651+
let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123);
1652+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1653+
{
1654+
times = times.set_created(created);
1655+
}
1656+
file.set_times(times).unwrap();
1657+
let metadata = file.metadata().unwrap();
1658+
assert_eq!(metadata.accessed().unwrap(), accessed);
1659+
assert_eq!(metadata.modified().unwrap(), modified);
1660+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1661+
{
1662+
assert_eq!(metadata.created().unwrap(), created);
1663+
}
1664+
}

0 commit comments

Comments
 (0)