Created
November 8, 2018 16:18
-
-
Save soyuka/b8184891f70ddb7c4ffbf822971a769b to your computer and use it in GitHub Desktop.
rmdir rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
use std::path::Path; | |
use std::io; | |
/// Remove dir, if non empty just skip | |
fn rmdir<P: AsRef<Path>>(path: P) -> Result<(), io::Error> { | |
if let Err(e) = fs::remove_dir(path) { | |
if e.kind() != io::ErrorKind::Other { | |
return Err(e); | |
} | |
} | |
Ok(()) | |
} | |
#[cfg(test)] | |
mod tests { | |
use std::io::Write; | |
use std::env::{temp_dir}; | |
use std::fs; | |
use super::rmdir; | |
#[test] | |
fn test_remove_nonempty_dir() { | |
let tmpdir = temp_dir(); | |
let dir = tmpdir.join("nonempty"); | |
fs::create_dir_all(&dir).is_ok(); | |
let mut file = fs::File::create(dir.join("test")).unwrap(); | |
file.write_all(b"foo").is_ok(); | |
assert!(rmdir(&dir).is_ok()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment