Compare commits
1 commit
ece13d43a2
...
f97ebf6015
Author | SHA1 | Date | |
---|---|---|---|
f97ebf6015 |
6 changed files with 3 additions and 125 deletions
10
Cargo.toml
10
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kxio"
|
name = "kxio"
|
||||||
version = "1.2.0"
|
version = "1.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Paul Campbell <pcampbell@kemitix.net>"]
|
authors = ["Paul Campbell <pcampbell@kemitix.net>"]
|
||||||
description = "Provides injectable Filesystem and Network resources to make code more testable"
|
description = "Provides injectable Filesystem and Network resources to make code more testable"
|
||||||
|
@ -33,12 +33,8 @@ thiserror = "1.0"
|
||||||
tempfile = "3.10"
|
tempfile = "3.10"
|
||||||
path-clean = "1.0"
|
path-clean = "1.0"
|
||||||
|
|
||||||
# boilerplate
|
# error handling
|
||||||
derive_more = { version = "1.0.0-beta.6", features = [
|
derive_more = { version = "1.0.0-beta.6", features = ["from", "display"] }
|
||||||
"from",
|
|
||||||
"display",
|
|
||||||
"constructor",
|
|
||||||
] }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# testing
|
# testing
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
use std::{
|
|
||||||
fs::{DirEntry, ReadDir},
|
|
||||||
path::PathBuf,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub enum DirItem {
|
|
||||||
File(PathBuf),
|
|
||||||
Dir(PathBuf),
|
|
||||||
SymLink(PathBuf),
|
|
||||||
Fifo(PathBuf),
|
|
||||||
Unsupported(PathBuf),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, derive_more::Constructor)]
|
|
||||||
pub struct DirItemIterator(ReadDir);
|
|
||||||
impl Iterator for DirItemIterator {
|
|
||||||
type Item = super::Result<DirItem>;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
self.0.next().map(map_dir_item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map_dir_item(item: std::io::Result<DirEntry>) -> super::Result<DirItem> {
|
|
||||||
let item = item?;
|
|
||||||
let file_type = item.file_type()?;
|
|
||||||
if file_type.is_dir() {
|
|
||||||
Ok(DirItem::Dir(item.path()))
|
|
||||||
} else if file_type.is_file() {
|
|
||||||
Ok(DirItem::File(item.path()))
|
|
||||||
} else if file_type.is_symlink() {
|
|
||||||
Ok(DirItem::SymLink(item.path()))
|
|
||||||
} else {
|
|
||||||
Ok(DirItem::Unsupported(item.path()))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,3 @@
|
||||||
use crate::fs::DirItem;
|
|
||||||
|
|
||||||
use super::Result;
|
use super::Result;
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -38,9 +36,6 @@ pub trait FileSystemLike {
|
||||||
/// ```
|
/// ```
|
||||||
fn dir_create_all(&self, path: &Path) -> Result<()>;
|
fn dir_create_all(&self, path: &Path) -> Result<()>;
|
||||||
|
|
||||||
/// Reads the items in a directory and returns them as an iterator.
|
|
||||||
fn dir_read(&self, path: &Path) -> Result<Box<dyn Iterator<Item = Result<DirItem>>>>;
|
|
||||||
|
|
||||||
/// Reads a file into a [String]. Will attempt to read the whole file into memory in one go.
|
/// Reads a file into a [String]. Will attempt to read the whole file into memory in one go.
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
|
|
@ -8,10 +8,6 @@ pub mod like;
|
||||||
mod real;
|
mod real;
|
||||||
mod temp;
|
mod temp;
|
||||||
|
|
||||||
mod dir_item;
|
|
||||||
pub use dir_item::DirItem;
|
|
||||||
pub use dir_item::DirItemIterator;
|
|
||||||
|
|
||||||
#[derive(Debug, From, derive_more::Display)]
|
#[derive(Debug, From, derive_more::Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Io(std::io::Error),
|
Io(std::io::Error),
|
||||||
|
@ -21,11 +17,6 @@ pub enum Error {
|
||||||
base: PathBuf,
|
base: PathBuf,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
},
|
},
|
||||||
|
|
||||||
#[display("Path must be a directory: {path:?}")]
|
|
||||||
NotADirectory {
|
|
||||||
path: PathBuf,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use crate::fs::{DirItem, DirItemIterator};
|
|
||||||
|
|
||||||
pub const fn new(base: PathBuf) -> RealFileSystem {
|
pub const fn new(base: PathBuf) -> RealFileSystem {
|
||||||
RealFileSystem { base }
|
RealFileSystem { base }
|
||||||
}
|
}
|
||||||
|
@ -26,20 +24,6 @@ impl super::FileSystemLike for RealFileSystem {
|
||||||
std::fs::create_dir_all(path).map_err(Into::into)
|
std::fs::create_dir_all(path).map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dir_read(
|
|
||||||
&self,
|
|
||||||
path: &Path,
|
|
||||||
) -> super::Result<Box<dyn Iterator<Item = super::Result<DirItem>>>> {
|
|
||||||
self.validate(path)?;
|
|
||||||
if !self.path_is_dir(path)? {
|
|
||||||
return Err(super::Error::NotADirectory {
|
|
||||||
path: path.to_path_buf(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let read_dir = std::fs::read_dir(path)?;
|
|
||||||
Ok(Box::new(DirItemIterator::new(read_dir)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn file_read_to_string(&self, path: &Path) -> super::Result<String> {
|
fn file_read_to_string(&self, path: &Path) -> super::Result<String> {
|
||||||
self.validate(path)?;
|
self.validate(path)?;
|
||||||
std::fs::read_to_string(path).map_err(Into::into)
|
std::fs::read_to_string(path).map_err(Into::into)
|
||||||
|
|
|
@ -107,57 +107,6 @@ mod dir_create_all {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod dir_dir_read {
|
|
||||||
use crate::fs::DirItem;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn should_return_dir_items() -> TestResult {
|
|
||||||
let fs = fs::temp()?;
|
|
||||||
let file1 = fs.base().join("file-1");
|
|
||||||
let dir = fs.base().join("dir");
|
|
||||||
let file2 = dir.join("file-2");
|
|
||||||
fs.file_write(&file1, "file-1")?;
|
|
||||||
fs.dir_create(&dir)?;
|
|
||||||
fs.file_write(&file2, "file-2")?;
|
|
||||||
|
|
||||||
let items = fs
|
|
||||||
.dir_read(fs.base())?
|
|
||||||
.filter_map(|i| i.ok())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
assert_eq!(items.len(), 2);
|
|
||||||
assert!(items.contains(&DirItem::File(file1)));
|
|
||||||
assert!(items.contains(&DirItem::Dir(dir)));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn should_fail_on_not_a_dir() -> TestResult {
|
|
||||||
let fs = fs::temp()?;
|
|
||||||
let path = fs.base().join("file");
|
|
||||||
fs.file_write(&path, "contents")?;
|
|
||||||
|
|
||||||
let_assert!(Err(fs::Error::NotADirectory { path: _path }) = fs.dir_read(&path));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn should_fail_on_path_traversal() -> TestResult {
|
|
||||||
let fs = fs::temp()?;
|
|
||||||
let path = fs.base().join("..").join("foo");
|
|
||||||
let_assert!(
|
|
||||||
Err(fs::Error::PathTraversal {
|
|
||||||
base: _base,
|
|
||||||
path: _path
|
|
||||||
}) = fs.dir_read(&path)
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod path_exists {
|
mod path_exists {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue