summaryrefslogtreecommitdiffstats
path: root/library/std/src/fs/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/fs/tests.rs')
-rw-r--r--library/std/src/fs/tests.rs60
1 files changed, 57 insertions, 3 deletions
diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs
index 401def184..e2480bcbb 100644
--- a/library/std/src/fs/tests.rs
+++ b/library/std/src/fs/tests.rs
@@ -1,7 +1,7 @@
use crate::io::prelude::*;
use crate::env;
-use crate::fs::{self, File, OpenOptions};
+use crate::fs::{self, File, FileTimes, OpenOptions};
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
use crate::mem::MaybeUninit;
use crate::path::Path;
@@ -9,7 +9,7 @@ use crate::str;
use crate::sync::Arc;
use crate::sys_common::io::test::{tmpdir, TempDir};
use crate::thread;
-use crate::time::{Duration, Instant};
+use crate::time::{Duration, Instant, SystemTime};
use rand::RngCore;
@@ -919,6 +919,7 @@ fn symlink_noexist() {
#[test]
fn read_link() {
+ let tmpdir = tmpdir();
if cfg!(windows) {
// directory symlink
assert_eq!(check!(fs::read_link(r"C:\Users\All Users")), Path::new(r"C:\ProgramData"));
@@ -933,8 +934,11 @@ fn read_link() {
Path::new(r"C:\Users")
);
}
+ // Check that readlink works with non-drive paths on Windows.
+ let link = tmpdir.join("link_unc");
+ check!(symlink_dir(r"\\localhost\c$\", &link));
+ assert_eq!(check!(fs::read_link(&link)), Path::new(r"\\localhost\c$\"));
}
- let tmpdir = tmpdir();
let link = tmpdir.join("link");
if !got_symlink_permission(&tmpdir) {
return;
@@ -1629,3 +1633,53 @@ fn rename_directory() {
assert!(new_path.join("newdir").is_dir());
assert!(new_path.join("newdir/temp.txt").exists());
}
+
+#[test]
+fn test_file_times() {
+ #[cfg(target_os = "ios")]
+ use crate::os::ios::fs::FileTimesExt;
+ #[cfg(target_os = "macos")]
+ use crate::os::macos::fs::FileTimesExt;
+ #[cfg(target_os = "watchos")]
+ use crate::os::watchos::fs::FileTimesExt;
+ #[cfg(windows)]
+ use crate::os::windows::fs::FileTimesExt;
+
+ let tmp = tmpdir();
+ let file = File::create(tmp.join("foo")).unwrap();
+ let mut times = FileTimes::new();
+ let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345);
+ let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321);
+ times = times.set_accessed(accessed).set_modified(modified);
+ #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
+ let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123);
+ #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
+ {
+ times = times.set_created(created);
+ }
+ match file.set_times(times) {
+ // Allow unsupported errors on platforms which don't support setting times.
+ #[cfg(not(any(
+ windows,
+ all(
+ unix,
+ not(any(
+ target_os = "android",
+ target_os = "redox",
+ target_os = "espidf",
+ target_os = "horizon"
+ ))
+ )
+ )))]
+ Err(e) if e.kind() == ErrorKind::Unsupported => return,
+ Err(e) => panic!("error setting file times: {e:?}"),
+ Ok(_) => {}
+ }
+ let metadata = file.metadata().unwrap();
+ assert_eq!(metadata.accessed().unwrap(), accessed);
+ assert_eq!(metadata.modified().unwrap(), modified);
+ #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
+ {
+ assert_eq!(metadata.created().unwrap(), created);
+ }
+}