Compare commits
13 commits
Author | SHA1 | Date | |
---|---|---|---|
|
d90a1c42c8 | ||
|
9943a0fe4e | ||
|
a2e91a871b | ||
|
ca7be5c484 | ||
|
7335e78552 | ||
|
9c819fd856 | ||
|
3f805b3ad5 | ||
274f70e485 | |||
d0aee99c83 | |||
|
93c74487a1 | ||
|
f261474ed0 | ||
faa9f291d4 | |||
a84248eca3 |
9 changed files with 138 additions and 7 deletions
|
@ -2,7 +2,7 @@ steps:
|
||||||
update-builder-image:
|
update-builder-image:
|
||||||
when:
|
when:
|
||||||
- event: cron
|
- event: cron
|
||||||
image: docker.io/woodpeckerci/plugin-docker-buildx:3.2
|
image: docker.io/woodpeckerci/plugin-docker-buildx:4.2
|
||||||
settings:
|
settings:
|
||||||
username: kemitix
|
username: kemitix
|
||||||
repo: git.kemitix.net/kemitix/kxio-builder
|
repo: git.kemitix.net/kemitix/kxio-builder
|
||||||
|
@ -53,7 +53,7 @@ steps:
|
||||||
publish_to_crates_io:
|
publish_to_crates_io:
|
||||||
when:
|
when:
|
||||||
- event: tag
|
- event: tag
|
||||||
image: docker.io/rust:1.77
|
image: docker.io/rust:1.81
|
||||||
commands:
|
commands:
|
||||||
- cargo login "$CARGO_REGISTRY_TOKEN"
|
- cargo login "$CARGO_REGISTRY_TOKEN"
|
||||||
- cargo publish --registry crates-io --no-verify
|
- cargo publish --registry crates-io --no-verify
|
||||||
|
@ -63,7 +63,7 @@ steps:
|
||||||
when:
|
when:
|
||||||
- event: tag
|
- event: tag
|
||||||
# INFO: https://woodpecker-ci.org/plugins/Gitea%20Release
|
# INFO: https://woodpecker-ci.org/plugins/Gitea%20Release
|
||||||
image: docker.io/woodpeckerci/plugin-gitea-release:0.3
|
image: docker.io/woodpeckerci/plugin-gitea-release:latest
|
||||||
settings:
|
settings:
|
||||||
base_url: https://git.kemitix.net
|
base_url: https://git.kemitix.net
|
||||||
api_key:
|
api_key:
|
||||||
|
|
12
Cargo.toml
12
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kxio"
|
name = "kxio"
|
||||||
version = "1.1.1"
|
version = "1.2.0"
|
||||||
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"
|
||||||
|
@ -23,7 +23,7 @@ tracing = "0.1"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
http = "1.1"
|
http = "1.1"
|
||||||
reqwest = "0.12"
|
reqwest = "0.12"
|
||||||
secrecy = "0.8"
|
secrecy = "0.10"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde-xml-rs = "0.6"
|
serde-xml-rs = "0.6"
|
||||||
|
@ -33,8 +33,12 @@ thiserror = "1.0"
|
||||||
tempfile = "3.10"
|
tempfile = "3.10"
|
||||||
path-clean = "1.0"
|
path-clean = "1.0"
|
||||||
|
|
||||||
# error handling
|
# boilerplate
|
||||||
derive_more = { version = "1.0.0-beta.6", features = ["from", "display"] }
|
derive_more = { version = "1.0.0-beta", features = [
|
||||||
|
"from",
|
||||||
|
"display",
|
||||||
|
"constructor",
|
||||||
|
] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# testing
|
# testing
|
||||||
|
|
37
src/fs/dir_item.rs
Normal file
37
src/fs/dir_item.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
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,3 +1,5 @@
|
||||||
|
use crate::fs::DirItem;
|
||||||
|
|
||||||
use super::Result;
|
use super::Result;
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -8,6 +10,9 @@ pub trait FileSystemLike {
|
||||||
fn dir_create(&self, path: &Path) -> Result<()>;
|
fn dir_create(&self, path: &Path) -> Result<()>;
|
||||||
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>>>>;
|
||||||
|
|
||||||
fn file_read_to_string(&self, path: &Path) -> Result<String>;
|
fn file_read_to_string(&self, path: &Path) -> Result<String>;
|
||||||
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;
|
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,10 @@ 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),
|
||||||
|
@ -17,6 +21,11 @@ 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 {}
|
||||||
|
|
||||||
|
@ -30,6 +39,7 @@ pub fn temp() -> Result<FileSystem> {
|
||||||
temp::new().map(FileSystem::Temp)
|
temp::new().map(FileSystem::Temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub enum FileSystem {
|
pub enum FileSystem {
|
||||||
Real(real::RealFileSystem),
|
Real(real::RealFileSystem),
|
||||||
Temp(temp::TempFileSystem),
|
Temp(temp::TempFileSystem),
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
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 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct RealFileSystem {
|
pub struct RealFileSystem {
|
||||||
base: PathBuf,
|
base: PathBuf,
|
||||||
}
|
}
|
||||||
|
@ -23,6 +26,20 @@ 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)
|
||||||
|
|
|
@ -14,6 +14,7 @@ pub(super) fn new() -> super::Result<TempFileSystem> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct TempFileSystem {
|
pub struct TempFileSystem {
|
||||||
real: super::real::RealFileSystem,
|
real: super::real::RealFileSystem,
|
||||||
_temp_dir: Arc<Mutex<TempDir>>,
|
_temp_dir: Arc<Mutex<TempDir>>,
|
||||||
|
|
|
@ -36,3 +36,9 @@ impl<T> NetResponse<T> {
|
||||||
self.response_body
|
self.response_body
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<NetResponse<()>> for () {
|
||||||
|
fn from(_value: NetResponse<()>) -> Self {
|
||||||
|
// ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -107,6 +107,57 @@ 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