Skip to content

Commit 0a82f16

Browse files
committed
fix doctests, add real tests
1 parent bdeb57e commit 0a82f16

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
@@ -143,6 +143,7 @@ pub enum TryLockError {
143143
/// Opens a directory and then a file inside it.
144144
///
145145
/// ```no_run
146+
/// #![feature(dirfd)]
146147
/// use std::fs::Dir;
147148
///
148149
/// fn main() -> std::io::Result<()> {
@@ -1491,7 +1492,8 @@ impl Dir {
14911492
/// # Examples
14921493
///
14931494
/// ```no_run
1494-
/// use std::fs::Dir;
1495+
/// #![feature(dirfd)]
1496+
/// use std::{fs::Dir, io::Read};
14951497
///
14961498
/// fn main() -> std::io::Result<()> {
14971499
/// let dir = Dir::new("foo")?;
@@ -1520,7 +1522,8 @@ impl Dir {
15201522
/// # Examples
15211523
///
15221524
/// ```no_run
1523-
/// use std::fs::Dir;
1525+
/// #![feature(dirfd)]
1526+
/// use std::fs::{Dir, OpenOptions};
15241527
///
15251528
/// fn main() -> std::io::Result<()> {
15261529
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
@@ -1533,7 +1536,7 @@ impl Dir {
15331536
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
15341537
}
15351538

1536-
/// Attempts to open a file relative to this directory.
1539+
/// Attempts to open a file in read-only mode relative to this directory.
15371540
///
15381541
/// # Errors
15391542
///
@@ -1545,7 +1548,8 @@ impl Dir {
15451548
/// # Examples
15461549
///
15471550
/// ```no_run
1548-
/// use std::fs::Dir;
1551+
/// #![feature(dirfd)]
1552+
/// use std::{fs::Dir, io::Read};
15491553
///
15501554
/// fn main() -> std::io::Result<()> {
15511555
/// let dir = Dir::new("foo")?;
@@ -1572,7 +1576,8 @@ impl Dir {
15721576
/// # Examples
15731577
///
15741578
/// ```no_run
1575-
/// use std::fs::Dir;
1579+
/// #![feature(dirfd)]
1580+
/// use std::{fs::{Dir, OpenOptions}, io::Read};
15761581
///
15771582
/// fn main() -> std::io::Result<()> {
15781583
/// let dir = Dir::new("foo")?;
@@ -1599,6 +1604,7 @@ impl Dir {
15991604
/// # Examples
16001605
///
16011606
/// ```no_run
1607+
/// #![feature(dirfd)]
16021608
/// use std::fs::Dir;
16031609
///
16041610
/// fn main() -> std::io::Result<()> {
@@ -1625,6 +1631,7 @@ impl Dir {
16251631
/// # Examples
16261632
///
16271633
/// ```no_run
1634+
/// #![feature(dirfd)]
16281635
/// use std::fs::Dir;
16291636
///
16301637
/// fn main() -> std::io::Result<()> {
@@ -1651,6 +1658,7 @@ impl Dir {
16511658
/// # Examples
16521659
///
16531660
/// ```no_run
1661+
/// #![feature(dirfd)]
16541662
/// use std::fs::Dir;
16551663
///
16561664
/// 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)