feat(fs): add .path(path).metadata()
This commit is contained in:
parent
19754d5796
commit
66a0219ba3
3 changed files with 49 additions and 1 deletions
|
@ -9,7 +9,7 @@ Provides injectable Filesystem and Network resources to make code more testable.
|
|||
- [x] `std::fs::create_dir` - `dir(path).create()` - Creates a new, empty directory at the provided path
|
||||
- [x] `std::fs::create_dir_all` - `dir(path).create_all()` - Recursively create a directory and all of its parent components if they are missing.
|
||||
- [x] `std::fs::hard_link` - `file(path).hard_link(other)` - Creates a new hard link on the filesystem.
|
||||
- [ ] `std::fs::metadata` - `path(path).metadata()` - Given a path, query the file system to get information about a file, directory, etc.
|
||||
- [x] `std::fs::metadata` - `path(path).metadata()` - Given a path, query the file system to get information about a file, directory, etc.
|
||||
- [x] `std::fs::read` - `file(path).reader().bytes()` - Read the entire contents of a file into a bytes vector.
|
||||
- [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.
|
||||
|
|
|
@ -244,6 +244,24 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
|
|||
self.check_error()?;
|
||||
self.as_pathbuf().canonicalize().map_err(Error::Io)
|
||||
}
|
||||
|
||||
/// Returns the metadata for a path.
|
||||
///
|
||||
/// Wrapper for [std::fs::metadata]
|
||||
///
|
||||
/// ```
|
||||
/// # fn try_main() -> kxio::fs::Result<()> {
|
||||
/// let fs = kxio::fs::temp()?;
|
||||
/// let path = fs.base().join("foo");
|
||||
/// let file = fs.file(&path);
|
||||
/// let metadata = file.metadata()?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn metadata(&self) -> Result<std::fs::Metadata> {
|
||||
self.check_error()?;
|
||||
std::fs::metadata(self.as_pathbuf()).map_err(Error::Io)
|
||||
}
|
||||
}
|
||||
impl<'base, 'path> From<PathHandle<'base, 'path, PathMarker>> for PathBuf {
|
||||
fn from(path: PathReal<PathMarker>) -> Self {
|
||||
|
|
30
tests/fs.rs
30
tests/fs.rs
|
@ -7,6 +7,36 @@ type TestResult = Result<(), fs::Error>;
|
|||
mod path {
|
||||
use super::*;
|
||||
|
||||
mod metadata {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_return_metadata() -> TestResult {
|
||||
let fs = fs::temp().expect("temp fs");
|
||||
let path = fs.base().join("foo");
|
||||
let file = fs.file(&path);
|
||||
file.write("bar").expect("write");
|
||||
let md = file.metadata().expect("metadata");
|
||||
assert!(md.is_file());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_fail_on_path_traversal() -> TestResult {
|
||||
let fs = fs::temp().expect("temp fs");
|
||||
let path = fs.base().join("..").join("foo");
|
||||
let_assert!(
|
||||
Err(fs::Error::PathTraversal {
|
||||
base: _base,
|
||||
path: _path
|
||||
}) = fs.file(&path).metadata()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod path_of {
|
||||
use super::*;
|
||||
|
||||
|
|
Loading…
Reference in a new issue