Skip to content

Commit 079e1c8

Browse files
committed
fix doctests, add real tests
1 parent d3d906f commit 079e1c8

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<()> {
@@ -1512,7 +1513,8 @@ impl Dir {
15121513
/// # Examples
15131514
///
15141515
/// ```no_run
1515-
/// use std::fs::Dir;
1516+
/// #![feature(dirfd)]
1517+
/// use std::{fs::Dir, io::Read};
15161518
///
15171519
/// fn main() -> std::io::Result<()> {
15181520
/// let dir = Dir::new("foo")?;
@@ -1541,7 +1543,8 @@ impl Dir {
15411543
/// # Examples
15421544
///
15431545
/// ```no_run
1544-
/// use std::fs::Dir;
1546+
/// #![feature(dirfd)]
1547+
/// use std::fs::{Dir, OpenOptions};
15451548
///
15461549
/// fn main() -> std::io::Result<()> {
15471550
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
@@ -1554,7 +1557,7 @@ impl Dir {
15541557
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
15551558
}
15561559

1557-
/// Attempts to open a file relative to this directory.
1560+
/// Attempts to open a file in read-only mode relative to this directory.
15581561
///
15591562
/// # Errors
15601563
///
@@ -1566,7 +1569,8 @@ impl Dir {
15661569
/// # Examples
15671570
///
15681571
/// ```no_run
1569-
/// use std::fs::Dir;
1572+
/// #![feature(dirfd)]
1573+
/// use std::{fs::Dir, io::Read};
15701574
///
15711575
/// fn main() -> std::io::Result<()> {
15721576
/// let dir = Dir::new("foo")?;
@@ -1593,7 +1597,8 @@ impl Dir {
15931597
/// # Examples
15941598
///
15951599
/// ```no_run
1596-
/// use std::fs::Dir;
1600+
/// #![feature(dirfd)]
1601+
/// use std::{fs::{Dir, OpenOptions}, io::Read};
15971602
///
15981603
/// fn main() -> std::io::Result<()> {
15991604
/// let dir = Dir::new("foo")?;
@@ -1620,6 +1625,7 @@ impl Dir {
16201625
/// # Examples
16211626
///
16221627
/// ```no_run
1628+
/// #![feature(dirfd)]
16231629
/// use std::fs::Dir;
16241630
///
16251631
/// fn main() -> std::io::Result<()> {
@@ -1646,6 +1652,7 @@ impl Dir {
16461652
/// # Examples
16471653
///
16481654
/// ```no_run
1655+
/// #![feature(dirfd)]
16491656
/// use std::fs::Dir;
16501657
///
16511658
/// fn main() -> std::io::Result<()> {
@@ -1672,6 +1679,7 @@ impl Dir {
16721679
/// # Examples
16731680
///
16741681
/// ```no_run
1682+
/// #![feature(dirfd)]
16751683
/// use std::fs::Dir;
16761684
///
16771685
/// 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)