Skip to content

Commit f74ff7f

Browse files
committed
fix doctests, add real tests
1 parent 7ac742d commit f74ff7f

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

library/std/src/fs.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pub enum TryLockError {
164164
/// Opens a directory and then a file inside it.
165165
///
166166
/// ```no_run
167+
/// #![feature(dirfd)]
167168
/// use std::fs::Dir;
168169
///
169170
/// fn main() -> std::io::Result<()> {
@@ -1507,7 +1508,8 @@ impl Dir {
15071508
/// # Examples
15081509
///
15091510
/// ```no_run
1510-
/// use std::fs::Dir;
1511+
/// #![feature(dirfd)]
1512+
/// use std::{fs::Dir, io::Read};
15111513
///
15121514
/// fn main() -> std::io::Result<()> {
15131515
/// let dir = Dir::new("foo")?;
@@ -1536,7 +1538,8 @@ impl Dir {
15361538
/// # Examples
15371539
///
15381540
/// ```no_run
1539-
/// use std::fs::Dir;
1541+
/// #![feature(dirfd)]
1542+
/// use std::fs::{Dir, OpenOptions};
15401543
///
15411544
/// fn main() -> std::io::Result<()> {
15421545
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
@@ -1549,7 +1552,7 @@ impl Dir {
15491552
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
15501553
}
15511554

1552-
/// Attempts to open a file relative to this directory.
1555+
/// Attempts to open a file in read-only mode relative to this directory.
15531556
///
15541557
/// # Errors
15551558
///
@@ -1561,7 +1564,8 @@ impl Dir {
15611564
/// # Examples
15621565
///
15631566
/// ```no_run
1564-
/// use std::fs::Dir;
1567+
/// #![feature(dirfd)]
1568+
/// use std::{fs::Dir, io::Read};
15651569
///
15661570
/// fn main() -> std::io::Result<()> {
15671571
/// let dir = Dir::new("foo")?;
@@ -1588,7 +1592,8 @@ impl Dir {
15881592
/// # Examples
15891593
///
15901594
/// ```no_run
1591-
/// use std::fs::Dir;
1595+
/// #![feature(dirfd)]
1596+
/// use std::{fs::{Dir, OpenOptions}, io::Read};
15921597
///
15931598
/// fn main() -> std::io::Result<()> {
15941599
/// let dir = Dir::new("foo")?;
@@ -1615,6 +1620,7 @@ impl Dir {
16151620
/// # Examples
16161621
///
16171622
/// ```no_run
1623+
/// #![feature(dirfd)]
16181624
/// use std::fs::Dir;
16191625
///
16201626
/// fn main() -> std::io::Result<()> {
@@ -1641,6 +1647,7 @@ impl Dir {
16411647
/// # Examples
16421648
///
16431649
/// ```no_run
1650+
/// #![feature(dirfd)]
16441651
/// use std::fs::Dir;
16451652
///
16461653
/// fn main() -> std::io::Result<()> {
@@ -1667,6 +1674,7 @@ impl Dir {
16671674
/// # Examples
16681675
///
16691676
/// ```no_run
1677+
/// #![feature(dirfd)]
16701678
/// use std::fs::Dir;
16711679
///
16721680
/// fn main() -> std::io::Result<()> {

library/std/src/fs/tests.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rand::RngCore;
22

3+
use super::Dir;
34
#[cfg(any(
45
windows,
56
target_os = "freebsd",
@@ -17,7 +18,7 @@ use crate::char::MAX_LEN_UTF8;
1718
target_vendor = "apple",
1819
))]
1920
use crate::fs::TryLockError;
20-
use crate::fs::{self, File, FileTimes, OpenOptions};
21+
use crate::fs::{self, File, FileTimes, OpenOptions, create_dir};
2122
use crate::io::prelude::*;
2223
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
2324
use crate::mem::MaybeUninit;
@@ -2084,3 +2085,85 @@ fn test_rename_junction() {
20842085
// Junction links are always absolute so we just check the file name is correct.
20852086
assert_eq!(fs::read_link(&dest).unwrap().file_name(), Some(not_exist.as_os_str()));
20862087
}
2088+
2089+
#[test]
2090+
fn test_dir_smoke_test() {
2091+
let tmpdir = tmpdir();
2092+
check!(Dir::new(tmpdir.path()));
2093+
}
2094+
2095+
#[test]
2096+
fn test_dir_read_file() {
2097+
let tmpdir = tmpdir();
2098+
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2099+
check!(f.write(b"bar"));
2100+
check!(f.flush());
2101+
drop(f);
2102+
let dir = check!(Dir::new(tmpdir.path()));
2103+
let mut f = check!(dir.open("foo.txt"));
2104+
let mut buf = [0u8; 3];
2105+
check!(f.read_exact(&mut buf));
2106+
assert_eq!(b"bar", &buf);
2107+
}
2108+
2109+
#[test]
2110+
fn test_dir_write_file() {
2111+
let tmpdir = tmpdir();
2112+
let dir = check!(Dir::new(tmpdir.path()));
2113+
let mut f = check!(dir.open_with("foo.txt", &OpenOptions::new().write(true).create(true)));
2114+
check!(f.write(b"bar"));
2115+
check!(f.flush());
2116+
drop(f);
2117+
let mut f = check!(File::open(tmpdir.join("foo.txt")));
2118+
let mut buf = [0u8; 3];
2119+
check!(f.read_exact(&mut buf));
2120+
assert_eq!(b"bar", &buf);
2121+
}
2122+
2123+
#[test]
2124+
fn test_dir_remove_file() {
2125+
let tmpdir = tmpdir();
2126+
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2127+
check!(f.write(b"bar"));
2128+
check!(f.flush());
2129+
drop(f);
2130+
let dir = check!(Dir::new(tmpdir.path()));
2131+
check!(dir.remove_file("foo.txt"));
2132+
let result = File::open(tmpdir.join("foo.txt"));
2133+
#[cfg(all(unix, not(target_os = "vxworks")))]
2134+
error!(result, "No such file or directory");
2135+
#[cfg(target_os = "vxworks")]
2136+
error!(result, "no such file or directory");
2137+
#[cfg(windows)]
2138+
error!(result, 2); // ERROR_FILE_NOT_FOUND
2139+
}
2140+
2141+
#[test]
2142+
fn test_dir_remove_dir() {
2143+
let tmpdir = tmpdir();
2144+
check!(create_dir(tmpdir.join("foo")));
2145+
let dir = check!(Dir::new(tmpdir.path()));
2146+
check!(dir.remove_dir("foo"));
2147+
let result = Dir::new(tmpdir.join("foo"));
2148+
#[cfg(all(unix, not(target_os = "vxworks")))]
2149+
error!(result, "No such file or directory");
2150+
#[cfg(target_os = "vxworks")]
2151+
error!(result, "no such file or directory");
2152+
#[cfg(windows)]
2153+
error!(result, 2); // ERROR_FILE_NOT_FOUND
2154+
}
2155+
2156+
#[test]
2157+
fn test_dir_rename_file() {
2158+
let tmpdir = tmpdir();
2159+
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2160+
check!(f.write(b"bar"));
2161+
check!(f.flush());
2162+
drop(f);
2163+
let dir = check!(Dir::new(tmpdir.path()));
2164+
check!(dir.rename("foo.txt", &dir, "baz.txt"));
2165+
let mut f = check!(File::open(tmpdir.join("baz.txt")));
2166+
let mut buf = [0u8; 3];
2167+
check!(f.read_exact(&mut buf));
2168+
assert_eq!(b"bar", &buf);
2169+
}

0 commit comments

Comments
 (0)