use crate::workspace::{RelPath, WorkspaceFs, WorkspaceRoot};
use origin_domain::ErrorKind;
pub async fn run_all<F: WorkspaceFs>(fs: &F, root: &WorkspaceRoot) {
lists_an_empty_directory(fs, root).await;
lists_a_directory_with_entries(fs, root).await;
reads_a_file(fs, root).await;
parent_traversal_is_rejected_early(fs, root).await;
}
async fn lists_an_empty_directory<F: WorkspaceFs>(fs: &F, root: &WorkspaceRoot) {
let entries = fs
.list_dir(
root,
&RelPath::new(".").expect("'.' is a valid relative path"),
)
.await
.expect("listing '.' must not fail");
let _ = entries;
}
async fn lists_a_directory_with_entries<F: WorkspaceFs>(fs: &F, root: &WorkspaceRoot) {
let entries = fs
.list_dir(root, &RelPath::new(".").expect("'.' is valid"))
.await
.expect("listing '.' must not fail");
for entry in &entries {
assert!(
!entry.name.is_empty(),
"a directory entry must have a non-empty name"
);
}
}
async fn reads_a_file<F: WorkspaceFs>(fs: &F, root: &WorkspaceRoot) {
let path = RelPath::new("README.md").expect("valid relative path");
let result = fs.read_file(root, &path).await;
match result {
Ok(content) => {
let _ = content;
}
Err(err) => {
let kind = err.kind();
assert_ne!(
kind,
ErrorKind::Permission,
"read_file for a path within the root must not be rejected \
as Permission (path={})",
path.as_path().display()
);
}
}
}
async fn parent_traversal_is_rejected_early<F: WorkspaceFs>(fs: &F, root: &WorkspaceRoot) {
let result = RelPath::new("../escape");
assert!(
result.is_err(),
"RelPath must reject `..` at construction, before any adapter sees it"
);
let root_list = fs.list_dir(root, &RelPath::new(".").unwrap()).await;
assert!(root_list.is_ok(), "listing the root via '.' must succeed");
}