From 66a0219ba323e1931ff8b4ae7aded2ead87ca91e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 3 Nov 2024 17:49:34 +0000 Subject: [PATCH] feat(fs): add .path(path).metadata() --- README.md | 2 +- src/fs/path.rs | 18 ++++++++++++++++++ tests/fs.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4234a5f..e5dc156 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/fs/path.rs b/src/fs/path.rs index 2af600d..c6aacce 100644 --- a/src/fs/path.rs +++ b/src/fs/path.rs @@ -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 { + self.check_error()?; + std::fs::metadata(self.as_pathbuf()).map_err(Error::Io) + } } impl<'base, 'path> From> for PathBuf { fn from(path: PathReal) -> Self { diff --git a/tests/fs.rs b/tests/fs.rs index b706668..7ee9bdc 100644 --- a/tests/fs.rs +++ b/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::*;