feat(fs): add .dir(path).remove()
This commit is contained in:
parent
16e57b8ca9
commit
ba3a388705
3 changed files with 32 additions and 1 deletions
|
@ -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.
|
||||
|
|
|
@ -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<PathHandle<'base, 'path>> for FileHandle<'base, 'path> {
|
||||
type Error = crate::fs::Error;
|
||||
|
|
14
tests/fs.rs
14
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::*;
|
||||
|
|
Loading…
Reference in a new issue