From 84640bb2dd9d6f5adbacd81784c3ab46a21fe296 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 27 Apr 2024 18:32:26 +0100 Subject: [PATCH] feat(fs): add new contructors for real and temp --- src/filesystem.rs | 15 ++++++++++++--- src/lib.rs | 3 +++ src/tests/filesystem.rs | 12 ++++++++++++ src/tests/mod.rs | 4 ++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/tests/filesystem.rs create mode 100644 src/tests/mod.rs diff --git a/src/filesystem.rs b/src/filesystem.rs index 0f9cd80..3237579 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -7,18 +7,27 @@ use std::{ use tempfile::{tempdir, TempDir}; use tracing::{debug, info}; +pub fn real(cwd: Option) -> FileSystem { + let cwd = cwd.unwrap_or_default(); + FileSystem::Real(RealFileSystem::new(cwd)) +} +pub fn temp() -> std::io::Result { + TempFileSystem::new().map(FileSystem::Temp) +} + #[derive(Clone, Debug)] pub enum FileSystem { Real(RealFileSystem), Temp(TempFileSystem), } impl FileSystem { + #[deprecated(since = "1.1.0", note = "Use [kxio::filesystem::real()] instead")] pub fn new_real(cwd: Option) -> Self { - let cwd = cwd.unwrap_or_default(); - Self::Real(RealFileSystem::new(cwd)) + real(cwd) } + #[deprecated(since = "1.1.0", note = "Use [kxio::filesystem::temp()] instead")] pub fn new_temp() -> std::io::Result { - TempFileSystem::new().map(Self::Temp) + temp() } } impl Deref for FileSystem { diff --git a/src/lib.rs b/src/lib.rs index b8d834d..b37925b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,6 @@ pub mod filesystem; #[cfg(feature = "network")] pub mod network; + +#[cfg(test)] +mod tests; diff --git a/src/tests/filesystem.rs b/src/tests/filesystem.rs new file mode 100644 index 0000000..ede71e9 --- /dev/null +++ b/src/tests/filesystem.rs @@ -0,0 +1,12 @@ +type TestResult = Result<(), Box>; + +#[test] +fn write_read_file_exists() -> TestResult { + let fs = crate::filesystem::temp()?; + let pathbuf = fs.write_file("foo", "content")?; + let c = fs.read_file("foo")?; + assert_eq!(c, "content"); + assert!(fs.file_exists(&pathbuf)); + + Ok(()) +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..b8d834d --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,4 @@ +#[cfg(feature = "fs")] +pub mod filesystem; +#[cfg(feature = "network")] +pub mod network;