use std::fmt;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NotFound {
declared: String,
roots: Vec<PathBuf>,
}
impl fmt::Display for NotFound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let declared = &self.declared;
if Path::new(declared).is_absolute() {
return write!(f, "adapter {declared} was not found");
}
if self.roots.is_empty() {
return write!(
f,
"adapter {declared} was not found in the directory this ran \
from, and no registry tree was given to look under"
);
}
let under = self
.roots
.iter()
.map(|r| r.display().to_string())
.collect::<Vec<_>>()
.join(" or ");
write!(
f,
"adapter {declared} was not found under {under}, nor in the \
directory this ran from. A registry names its adapter relative to \
the tree it sits in, so either that tree is not there or the entry \
needs an absolute path"
)
}
}
impl std::error::Error for NotFound {}
pub fn resolve(declared: &str, roots: &[PathBuf]) -> Result<PathBuf, NotFound> {
let named = Path::new(declared);
let mut candidates: Vec<PathBuf> = Vec::new();
if named.is_absolute() {
candidates.push(named.to_path_buf());
} else {
for root in roots {
candidates.push(root.join(named));
if let Some(base) = named.file_name() {
candidates.push(root.join(base));
}
}
candidates.push(named.to_path_buf());
}
for candidate in candidates {
if candidate.is_file() {
if let Ok(abs) = candidate.canonicalize() {
return Ok(abs);
}
}
}
Err(NotFound {
declared: declared.to_string(),
roots: roots.to_vec(),
})
}
pub fn roots_for_registry(dir: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
if let Some(up) = dir.parent().and_then(Path::parent) {
out.push(up.to_path_buf());
}
if let Some(up) = dir.parent() {
if !out.contains(&up.to_path_buf()) {
out.push(up.to_path_buf());
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn touch(path: &Path) {
std::fs::create_dir_all(path.parent().expect("has a parent")).expect("dirs");
std::fs::write(path, b"#!/usr/bin/env python3\n").expect("written");
}
#[test]
fn a_registry_two_levels_under_the_tree_finds_its_adapter() {
let tmp = tempfile::tempdir().expect("tmp");
let root = tmp.path().join("checkout");
let registry = root.join("plugins").join("registry");
std::fs::create_dir_all(®istry).expect("dirs");
let adapter = root.join("plugins").join("adapters").join("one.py");
touch(&adapter);
let roots = roots_for_registry(®istry);
let found = resolve("plugins/adapters/one.py", &roots).expect("found");
assert_eq!(found, adapter.canonicalize().expect("canonical"));
}
#[test]
fn a_registry_one_level_under_the_tree_finds_its_adapter_too() {
let tmp = tempfile::tempdir().expect("tmp");
let prefix = tmp.path().join("usr-share").join("stegobench");
let registry = prefix.join("registry");
std::fs::create_dir_all(®istry).expect("dirs");
let adapter = prefix.join("plugins").join("adapters").join("one.py");
touch(&adapter);
let roots = roots_for_registry(®istry);
let found = resolve("plugins/adapters/one.py", &roots).expect("found");
assert_eq!(found, adapter.canonicalize().expect("canonical"));
}
#[test]
fn a_flat_root_matches_on_the_file_name_alone() {
let tmp = tempfile::tempdir().expect("tmp");
let adapter = tmp.path().join("one.py");
touch(&adapter);
let found = resolve("plugins/adapters/one.py", &[tmp.path().to_path_buf()])
.expect("the flat shape resolves");
assert_eq!(found, adapter.canonicalize().expect("canonical"));
}
#[test]
fn an_absolute_adapter_is_used_as_written_and_no_root_can_shadow_it() {
let tmp = tempfile::tempdir().expect("tmp");
let real = tmp.path().join("elsewhere").join("one.py");
touch(&real);
let decoy_root = tmp.path().join("decoy");
touch(&decoy_root.join("one.py"));
let declared = real.display().to_string();
let found = resolve(&declared, std::slice::from_ref(&decoy_root)).expect("used as given");
assert_eq!(found, real.canonicalize().expect("canonical"));
let missing = tmp.path().join("nowhere").join("one.py");
let why = resolve(&missing.display().to_string(), &[decoy_root])
.expect_err("an absolute path that is not there");
assert!(
why.to_string().ends_with("was not found"),
"an absolute path should not be explained with roots: {why}"
);
}
#[test]
fn the_refusal_names_the_roots_and_never_mentions_a_clone() {
let tmp = tempfile::tempdir().expect("tmp");
let why = resolve("plugins/adapters/one.py", &[tmp.path().to_path_buf()])
.expect_err("nothing is there");
let text = why.to_string();
assert!(
text.contains(&tmp.path().display().to_string()),
"the refusal does not say where it looked: {text}"
);
assert!(
!text.contains("clone"),
"the refusal still sends a packaged install to a clone: {text}"
);
}
#[test]
fn with_no_roots_the_working_directory_is_still_tried() {
let why =
resolve("plugins/adapters/definitely-not-here.py", &[]).expect_err("nothing is there");
assert!(
why.to_string().contains("no registry tree was given"),
"{why}"
);
}
}