use std::io;
use std::path::Path;
#[cfg(windows)]
use crate::fs::_remove_dir_contents;
#[cfg(not(windows))]
use crate::unix::_remove_dir_contents;
pub fn remove_dir_contents<P: AsRef<Path>>(path: P) -> io::Result<()> {
_remove_dir_contents(path)?;
Ok(())
}
pub fn ensure_empty_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
match std::fs::create_dir(&path) {
Err(e) if e.kind() == io::ErrorKind::AlreadyExists
=> remove_dir_contents(path),
otherwise => otherwise,
}
}
#[cfg(test)]
mod test {
use tempfile::TempDir;
use crate::remove_dir_all;
use crate::remove_dir_contents;
use crate::ensure_empty_dir;
use std::fs::{self, File};
use std::path::PathBuf;
use std::io;
fn expect_failure<T>(k: io::ErrorKind, r: io::Result<T>) -> io::Result<()> {
match r {
Err(e) if e.kind() == k => Ok(()),
Err(e) => Err(e),
Ok(_) => Err(io::Error::new(
io::ErrorKind::Other,
"unexpected success".to_string(),
)),
}
}
struct Prep {
_tmp: TempDir,
ours: PathBuf,
file: PathBuf,
}
fn prep() -> Result<Prep, io::Error> {
let tmp = TempDir::new()?;
let ours = tmp.path().join("t.mkdir");
let file = ours.join("file");
fs::create_dir(&ours)?;
File::create(&file)?;
File::open(&file)?;
Ok(Prep { _tmp: tmp, ours, file })
}
#[test]
fn mkdir_rm() -> Result<(), io::Error> {
let p = prep()?;
expect_failure(io::ErrorKind::Other, remove_dir_contents(&p.file))?;
remove_dir_contents(&p.ours)?;
expect_failure(io::ErrorKind::NotFound, File::open(&p.file))?;
remove_dir_contents(&p.ours)?;
remove_dir_all(&p.ours)?;
expect_failure(io::ErrorKind::NotFound, remove_dir_contents(&p.ours))?;
Ok(())
}
#[test]
fn ensure_rm() -> Result<(), io::Error> {
let p = prep()?;
expect_failure(io::ErrorKind::Other, ensure_empty_dir(&p.file))?;
ensure_empty_dir(&p.ours)?;
expect_failure(io::ErrorKind::NotFound, File::open(&p.file))?;
ensure_empty_dir(&p.ours)?;
remove_dir_all(&p.ours)?;
ensure_empty_dir(&p.ours)?;
File::create(&p.file)?;
Ok(())
}
}