pub mod descriptor;
pub mod resolve;
pub use descriptor::{RepoSource, RepoSpec, WorkspaceDescriptor, WorkspaceMeta};
pub use resolve::{git_cache_dir, resolve_sources};
use std::path::{Path, PathBuf};
pub fn discover_descriptor(start: &Path) -> Option<PathBuf> {
let mut cur = start
.canonicalize()
.unwrap_or_else(|_| start.to_path_buf());
loop {
let cand = cur.join("nornir-workspace.toml");
if cand.is_file() {
return Some(cand);
}
if !cur.pop() {
return None;
}
}
}
#[cfg(test)]
mod discover_tests {
use super::*;
#[test]
fn discovers_descriptor_walking_up_from_subdir() {
let tmp = std::env::temp_dir().join(format!(
"nornir-discover-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let ws = tmp.join("workspace_demo");
let deep = ws.join("sub/a/b");
std::fs::create_dir_all(&deep).unwrap();
let desc = ws.join("nornir-workspace.toml");
std::fs::write(&desc, "[workspace]\nname = \"demo\"\n").unwrap();
let found = discover_descriptor(&ws).expect("found at ws root");
assert_eq!(found.canonicalize().unwrap(), desc.canonicalize().unwrap());
let found_deep = discover_descriptor(&deep).expect("found walking up");
assert_eq!(
found_deep.canonicalize().unwrap(),
desc.canonicalize().unwrap()
);
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn returns_none_when_no_descriptor() {
let tmp = std::env::temp_dir().join(format!(
"nornir-discover-none-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&tmp).unwrap();
let sub = tmp.join("empty/here");
std::fs::create_dir_all(&sub).unwrap();
assert!(!sub.join("nornir-workspace.toml").exists());
let _ = std::fs::remove_dir_all(&tmp);
}
}