Skip to content

Commit 1849b41

Browse files
committed
add more tests
1 parent 1a58e0c commit 1849b41

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

library/std/src/fs/tests.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::char::MAX_LEN_UTF8;
1818
target_vendor = "apple",
1919
))]
2020
use crate::fs::TryLockError;
21-
use crate::fs::{self, File, FileTimes, OpenOptions, create_dir};
21+
use crate::fs::{self, File, FileTimes, OpenOptions, create_dir, exists};
2222
use crate::io::prelude::*;
2323
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
2424
use crate::mem::MaybeUninit;
@@ -2131,13 +2131,7 @@ fn test_dir_remove_file() {
21312131
drop(f);
21322132
let dir = check!(Dir::new(tmpdir.path()));
21332133
check!(dir.remove_file("foo.txt"));
2134-
let result = File::open(tmpdir.join("foo.txt"));
2135-
#[cfg(all(unix, not(target_os = "vxworks")))]
2136-
error!(result, "No such file or directory");
2137-
#[cfg(target_os = "vxworks")]
2138-
error!(result, "no such file or directory");
2139-
#[cfg(windows)]
2140-
error!(result, 2); // ERROR_FILE_NOT_FOUND
2134+
assert!(!matches!(exists(tmpdir.join("foo.txt")), Ok(true)));
21412135
}
21422136

21432137
#[test]
@@ -2146,13 +2140,7 @@ fn test_dir_remove_dir() {
21462140
check!(create_dir(tmpdir.join("foo")));
21472141
let dir = check!(Dir::new(tmpdir.path()));
21482142
check!(dir.remove_dir("foo"));
2149-
let result = Dir::new(tmpdir.join("foo"));
2150-
#[cfg(all(unix, not(target_os = "vxworks")))]
2151-
error!(result, "No such file or directory");
2152-
#[cfg(target_os = "vxworks")]
2153-
error!(result, "no such file or directory");
2154-
#[cfg(windows)]
2155-
error!(result, 2); // ERROR_FILE_NOT_FOUND
2143+
assert!(!matches!(exists(tmpdir.join("foo")), Ok(true)));
21562144
}
21572145

21582146
#[test]
@@ -2177,3 +2165,39 @@ fn test_dir_create_dir() {
21772165
check!(dir.create_dir("foo"));
21782166
check!(Dir::new(tmpdir.join("foo")));
21792167
}
2168+
2169+
#[test]
2170+
fn test_dir_open_dir() {
2171+
let tmpdir = tmpdir();
2172+
let dir1 = check!(Dir::new(tmpdir.path()));
2173+
check!(dir1.create_dir("foo"));
2174+
let dir2 = check!(Dir::new(tmpdir.path().join("foo")));
2175+
let mut f = check!(dir2.open_with("bar.txt", &OpenOptions::new().create(true).write(true)));
2176+
check!(f.write(b"baz"));
2177+
check!(f.flush());
2178+
drop(f);
2179+
let dir3 = check!(dir1.open_dir("foo"));
2180+
let mut f = check!(dir3.open("bar.txt"));
2181+
let mut buf = [0u8; 3];
2182+
check!(f.read_exact(&mut buf));
2183+
assert_eq!(b"baz", &buf);
2184+
}
2185+
2186+
#[test]
2187+
fn test_dir_symlink() {
2188+
let tmpdir = tmpdir();
2189+
if !got_symlink_permission(&tmpdir) {
2190+
return;
2191+
};
2192+
2193+
let dir = check!(Dir::new(tmpdir.path()));
2194+
let mut f = check!(dir.open_with("foo.txt", &OpenOptions::new().write(true).create(true)));
2195+
check!(f.write(b"quux"));
2196+
check!(f.flush());
2197+
drop(f);
2198+
check!(dir.symlink("foo.txt", "bar.txt"));
2199+
let mut f = check!(dir.open("bar.txt"));
2200+
let mut buf = [0u8; 4];
2201+
check!(f.read_exact(&mut buf));
2202+
assert_eq!(b"quux", &buf);
2203+
}

library/std/src/sys/fs/unix.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ impl Dir {
373373
}
374374

375375
pub fn open_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<Self> {
376-
run_path_with_cstr(path.as_ref(), &|path| self.open_c_dir(path, &OpenOptions::new()))
376+
let mut opts = OpenOptions::new();
377+
opts.read(true);
378+
run_path_with_cstr(path.as_ref(), &|path| self.open_c_dir(path, &opts))
377379
}
378380

379381
pub fn open_dir_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<Self> {

0 commit comments

Comments
 (0)