Skip to content

Commit 3090e62

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 2c0dcb5 commit 3090e62

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
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: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use std::fs;
1+
use std::fs::{self, File};
22
use std::io;
33
use std::path::Path;
44

5+
#[cfg(test)]
6+
extern crate tempdir;
7+
58
/// Deletes the contents of `dir_path`, but not the directory iteself.
69
///
710
/// If `dir_path` is a symlink to a directory, deletes the contents
@@ -28,3 +31,34 @@ pub fn remove_dir_contents(dir_path: &Path) -> Result<(), io::Error> {
2831

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

0 commit comments

Comments
 (0)