Compare commits
No commits in common. "main" and "v1.1.0" have entirely different histories.
9 changed files with 8 additions and 141 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:4.2
|
image: docker.io/woodpeckerci/plugin-docker-buildx:3.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.81
|
image: docker.io/rust:1.77
|
||||||
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,10 +63,10 @@ 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:latest
|
image: docker.io/woodpeckerci/plugin-gitea-release:0.3
|
||||||
settings:
|
settings:
|
||||||
base_url: https://git.kemitix.net
|
base_url: https://git.kemitix.net
|
||||||
api_key:
|
api_key:
|
||||||
from_secret: FORGEJO_RELEASE_PLUGIN
|
from_secret: FORGEJO_RELEASE_PLUGIN
|
||||||
target: main
|
target: main
|
||||||
prerelease: false
|
prerelease: true
|
||||||
|
|
12
Cargo.toml
12
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kxio"
|
name = "kxio"
|
||||||
version = "1.2.0"
|
version = "1.1.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.10"
|
secrecy = "0.8"
|
||||||
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,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", 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};
|
||||||
|
@ -10,9 +8,6 @@ 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,10 +8,6 @@ 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,14 +17,7 @@ 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 {}
|
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
pub const fn new(base: PathBuf) -> FileSystem {
|
pub const fn new(base: PathBuf) -> FileSystem {
|
||||||
|
@ -39,7 +28,6 @@ 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,12 +1,9 @@
|
||||||
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,
|
||||||
}
|
}
|
||||||
|
@ -26,20 +23,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)
|
||||||
|
|
|
@ -14,7 +14,6 @@ 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,9 +36,3 @@ impl<T> NetResponse<T> {
|
||||||
self.response_body
|
self.response_body
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<NetResponse<()>> for () {
|
|
||||||
fn from(_value: NetResponse<()>) -> Self {
|
|
||||||
// ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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