diff --git a/README.md b/README.md index 6101895..8c95a80 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Provides injectable Filesystem and Network resources to make code more testable. - [x] `std::fs::read_dir` - `dir(path).read()` - Returns an iterator over the entries within a directory. - [ ] `std::fs::read_link` - `link(path).read()` - Reads a symbolic link, returning the file that the link points to. - [x] `std::fs::read_to_string` - `file(path).reader().to_string()` - Read the entire contents of a file into a string. -- [ ] `std::fs::remove_dir` - `dir(path).remove()` - Removes an empty directory. +- [x] `std::fs::remove_dir` - `dir(path).remove()` - Removes an empty directory. - [ ] `std::fs::remove_dir_all` - `dir(path).remove_all()` - Removes a directory at this path, after removing all its contents. Use carefully! - [ ] `std::fs::remove_file` - `file(path).remove()` - Removes a file from the filesystem. - [ ] `std::fs::rename` - `path(path).rename()` - Rename a file or directory to a new name, replacing the original file if to already exists. diff --git a/src/fs/dir.rs b/src/fs/dir.rs index c8d11b3..5c49cab 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -58,6 +58,23 @@ impl<'base, 'path> DirHandle<'base, 'path> { let read_dir = std::fs::read_dir(self.as_pathbuf()).map_err(Error::Io)?; Ok(Box::new(DirItemIterator::new(read_dir))) } + + /// Removes an empty directory. + /// + /// Wrapper for [std::fs::remove_dir] + /// + /// ``` + /// # fn try_main() -> kxio::fs::Result<()> { + /// let fs = kxio::fs::temp()?; + /// let path = fs.base().join("foo").join("bar"); + /// let dir = fs.dir(&path); + /// dir.remove()?; + /// # Ok(()) + /// } + pub fn remove(&self) -> Result<()> { + self.check_error()?; + std::fs::remove_dir(self.as_pathbuf()).map_err(Error::Io) + } } impl<'base, 'path> TryFrom> for FileHandle<'base, 'path> { type Error = crate::fs::Error; diff --git a/tests/fs.rs b/tests/fs.rs index 9c2198f..05ee9ea 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -250,6 +250,20 @@ mod dir_dir_read { Ok(()) } } +mod dir_remove { + use super::*; + #[test] + fn should_remove_a_dir() -> TestResult { + let fs = fs::temp().expect("temp fs"); + let path = fs.base().join("foo"); + fs.dir(&path).create().expect("create"); + fs.dir(&path).remove().expect("remove"); + let exists = fs.path(&path).exists().expect("exists"); + assert!(!exists); + + Ok(()) + } +} mod path_exists { use super::*;