Skip to content

Commit bf718d8

Browse files
committed
fix doctests, add real tests
1 parent 148fd24 commit bf718d8

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

library/std/src/fs.rs

+13-5
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<()> {
@@ -1440,7 +1441,8 @@ impl Dir {
14401441
/// # Examples
14411442
///
14421443
/// ```no_run
1443-
/// use std::fs::Dir;
1444+
/// #![feature(dirfd)]
1445+
/// use std::{fs::Dir, io::Read};
14441446
///
14451447
/// fn main() -> std::io::Result<()> {
14461448
/// let dir = Dir::new("foo")?;
@@ -1469,7 +1471,8 @@ impl Dir {
14691471
/// # Examples
14701472
///
14711473
/// ```no_run
1472-
/// use std::fs::Dir;
1474+
/// #![feature(dirfd)]
1475+
/// use std::fs::{Dir, OpenOptions};
14731476
///
14741477
/// fn main() -> std::io::Result<()> {
14751478
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
@@ -1482,7 +1485,7 @@ impl Dir {
14821485
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
14831486
}
14841487

1485-
/// Attempts to open a file relative to this directory.
1488+
/// Attempts to open a file in read-only mode relative to this directory.
14861489
///
14871490
/// # Errors
14881491
///
@@ -1494,7 +1497,8 @@ impl Dir {
14941497
/// # Examples
14951498
///
14961499
/// ```no_run
1497-
/// use std::fs::Dir;
1500+
/// #![feature(dirfd)]
1501+
/// use std::{fs::Dir, io::Read};
14981502
///
14991503
/// fn main() -> std::io::Result<()> {
15001504
/// let dir = Dir::new("foo")?;
@@ -1521,7 +1525,8 @@ impl Dir {
15211525
/// # Examples
15221526
///
15231527
/// ```no_run
1524-
/// use std::fs::Dir;
1528+
/// #![feature(dirfd)]
1529+
/// use std::{fs::{Dir, OpenOptions}, io::Read};
15251530
///
15261531
/// fn main() -> std::io::Result<()> {
15271532
/// let dir = Dir::new("foo")?;
@@ -1548,6 +1553,7 @@ impl Dir {
15481553
/// # Examples
15491554
///
15501555
/// ```no_run
1556+
/// #![feature(dirfd)]
15511557
/// use std::fs::Dir;
15521558
///
15531559
/// fn main() -> std::io::Result<()> {
@@ -1574,6 +1580,7 @@ impl Dir {
15741580
/// # Examples
15751581
///
15761582
/// ```no_run
1583+
/// #![feature(dirfd)]
15771584
/// use std::fs::Dir;
15781585
///
15791586
/// fn main() -> std::io::Result<()> {
@@ -1600,6 +1607,7 @@ impl Dir {
16001607
/// # Examples
16011608
///
16021609
/// ```no_run
1610+
/// #![feature(dirfd)]
16031611
/// use std::fs::Dir;
16041612
///
16051613
/// fn main() -> std::io::Result<()> {

library/std/src/fs/tests.rs

+84-1
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;
@@ -2024,3 +2025,85 @@ fn test_rename_junction() {
20242025
// Junction links are always absolute so we just check the file name is correct.
20252026
assert_eq!(fs::read_link(&dest).unwrap().file_name(), Some(not_exist.as_os_str()));
20262027
}
2028+
2029+
#[test]
2030+
fn test_dir_smoke_test() {
2031+
let tmpdir = tmpdir();
2032+
check!(Dir::new(tmpdir.path()));
2033+
}
2034+
2035+
#[test]
2036+
fn test_dir_read_file() {
2037+
let tmpdir = tmpdir();
2038+
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2039+
check!(f.write(b"bar"));
2040+
check!(f.flush());
2041+
drop(f);
2042+
let dir = check!(Dir::new(tmpdir.path()));
2043+
let mut f = check!(dir.open("foo.txt"));
2044+
let mut buf = [0u8; 3];
2045+
check!(f.read_exact(&mut buf));
2046+
assert_eq!(b"bar", &buf);
2047+
}
2048+
2049+
#[test]
2050+
fn test_dir_write_file() {
2051+
let tmpdir = tmpdir();
2052+
let dir = check!(Dir::new(tmpdir.path()));
2053+
let mut f = check!(dir.open_with("foo.txt", &OpenOptions::new().write(true).create(true)));
2054+
check!(f.write(b"bar"));
2055+
check!(f.flush());
2056+
drop(f);
2057+
let mut f = check!(File::open(tmpdir.join("foo.txt")));
2058+
let mut buf = [0u8; 3];
2059+
check!(f.read_exact(&mut buf));
2060+
assert_eq!(b"bar", &buf);
2061+
}
2062+
2063+
#[test]
2064+
fn test_dir_remove_file() {
2065+
let tmpdir = tmpdir();
2066+
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2067+
check!(f.write(b"bar"));
2068+
check!(f.flush());
2069+
drop(f);
2070+
let dir = check!(Dir::new(tmpdir.path()));
2071+
check!(dir.remove_file("foo.txt"));
2072+
let result = File::open(tmpdir.join("foo.txt"));
2073+
#[cfg(all(unix, not(target_os = "vxworks")))]
2074+
error!(result, "No such file or directory");
2075+
#[cfg(target_os = "vxworks")]
2076+
error!(result, "no such file or directory");
2077+
#[cfg(windows)]
2078+
error!(result, 2); // ERROR_FILE_NOT_FOUND
2079+
}
2080+
2081+
#[test]
2082+
fn test_dir_remove_dir() {
2083+
let tmpdir = tmpdir();
2084+
check!(create_dir(tmpdir.join("foo")));
2085+
let dir = check!(Dir::new(tmpdir.path()));
2086+
check!(dir.remove_dir("foo"));
2087+
let result = Dir::new(tmpdir.join("foo"));
2088+
#[cfg(all(unix, not(target_os = "vxworks")))]
2089+
error!(result, "No such file or directory");
2090+
#[cfg(target_os = "vxworks")]
2091+
error!(result, "no such file or directory");
2092+
#[cfg(windows)]
2093+
error!(result, 2); // ERROR_FILE_NOT_FOUND
2094+
}
2095+
2096+
#[test]
2097+
fn test_dir_rename_file() {
2098+
let tmpdir = tmpdir();
2099+
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2100+
check!(f.write(b"bar"));
2101+
check!(f.flush());
2102+
drop(f);
2103+
let dir = check!(Dir::new(tmpdir.path()));
2104+
check!(dir.rename("foo.txt", &dir, "baz.txt"));
2105+
let mut f = check!(File::open(tmpdir.join("baz.txt")));
2106+
let mut buf = [0u8; 3];
2107+
check!(f.read_exact(&mut buf));
2108+
assert_eq!(b"bar", &buf);
2109+
}

0 commit comments

Comments
 (0)