Skip to content

Commit 30791c3

Browse files
committed
Add test case for remove_dir_contents
We check a few error behaviours too. Signed-off-by: Ian Jackson <[email protected]>
1 parent 8654e61 commit 30791c3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ libc = "0.2"
2424

2525
[dev-dependencies]
2626
doc-comment = "0.3"
27+
tempdir = "0.3"

src/portable.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,43 @@ pub fn remove_dir_contents(dir_path: &Path) -> Result<(), io::Error> {
2828

2929
Ok(())
3030
}
31+
32+
#[cfg(test)]
33+
mod test {
34+
extern crate tempdir;
35+
use crate::remove_dir_all;
36+
use crate::remove_dir_contents;
37+
use std::fs::{self, File};
38+
use std::io;
39+
40+
fn expect_failure<T>(k: io::ErrorKind, r: io::Result<T>) -> io::Result<()> {
41+
match r {
42+
Err(e) if e.kind() == k => Ok(()),
43+
Err(e) => Err(e),
44+
Ok(_) => Err(io::Error::new(
45+
io::ErrorKind::Other,
46+
"unexpected success".to_string(),
47+
)),
48+
}
49+
}
50+
51+
#[test]
52+
fn mkdir_rm() -> Result<(), io::Error> {
53+
let tmp = tempdir::TempDir::new("t.remove_dir_all")?;
54+
let ours = tmp.path().join("t.mkdir");
55+
let file = ours.join("file");
56+
fs::create_dir(&ours)?;
57+
File::create(&file)?;
58+
File::open(&file)?;
59+
60+
expect_failure(io::ErrorKind::Other, remove_dir_contents(&file))?;
61+
62+
remove_dir_contents(&ours)?;
63+
expect_failure(io::ErrorKind::NotFound, File::open(&file))?;
64+
65+
remove_dir_contents(&ours)?;
66+
remove_dir_all(&ours)?;
67+
remove_dir_contents(&ours)?;
68+
Ok(())
69+
}
70+
}

0 commit comments

Comments
 (0)