use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;
use crate::types::project::CommitId;
use crate::types::project::GitUrl;
use crate::types::resolve::FetchError;
use crate::types::tree::ContentHash;
pub fn cache_home(
airsstack_home: Option<&OsStr>,
home: Option<&OsStr>,
) -> Result<PathBuf, FetchError> {
match airsstack_home {
Some(value) if !value.is_empty() => Ok(PathBuf::from(value)),
_ => match home {
Some(value) if !value.is_empty() => Ok(PathBuf::from(value).join(".airsstack")),
_ => Err(FetchError::HomeUnset),
},
}
}
#[must_use]
pub fn url_dir(root: &Path, url: &GitUrl) -> PathBuf {
let digest = ContentHash::of_bytes(url.as_str().as_bytes());
root.join("cache").join("git").join(digest.hex())
}
#[must_use]
pub fn rev_dir(url_dir: &Path, rev: &CommitId) -> PathBuf {
url_dir.join(format!("{rev}.git"))
}
#[must_use]
pub fn temp_name(pid: u32, n: u64) -> String {
format!("tmp-{pid}-{n}")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::report::Diagnostic;
use crate::types::report::Location;
type TestResult = Result<(), Box<dyn std::error::Error>>;
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
const URL: &str = "https://github.com/airsstack/agentstacks";
#[test]
fn cache_home_refuses_a_missing_home() -> TestResult {
let error = cache_home(None, None).err().ok_or("expected a refusal")?;
assert!(matches!(error, FetchError::HomeUnset));
assert_eq!(
Diagnostic::new(Location::file("agentstacks.toml"), error).to_string(),
"agentstacks.toml: AIRSSTACK_HOME and HOME are not set, so the cache (~/.airsstack/cache) has no home"
);
Ok(())
}
#[test]
fn cache_home_refuses_an_empty_home() -> TestResult {
let error = cache_home(Some(OsStr::new("")), Some(OsStr::new("")))
.err()
.ok_or("expected a refusal")?;
assert!(matches!(error, FetchError::HomeUnset));
assert_eq!(
Diagnostic::new(Location::file("agentstacks.toml"), error).to_string(),
"agentstacks.toml: AIRSSTACK_HOME and HOME are not set, so the cache (~/.airsstack/cache) has no home"
);
Ok(())
}
#[test]
fn cache_home_prefers_airsstack_home() -> TestResult {
assert_eq!(
cache_home(
Some(OsStr::new("/sandbox/home")),
Some(OsStr::new("/home/u"))
)?,
Path::new("/sandbox/home")
);
assert_eq!(
cache_home(Some(OsStr::new("/sandbox/home")), None)?,
Path::new("/sandbox/home")
);
Ok(())
}
#[test]
fn cache_home_falls_back_to_home_dot_airsstack() -> TestResult {
assert_eq!(
cache_home(None, Some(OsStr::new("/home/u")))?,
Path::new("/home/u/.airsstack")
);
assert_eq!(
cache_home(Some(OsStr::new("")), Some(OsStr::new("/home/u")))?,
Path::new("/home/u/.airsstack")
);
Ok(())
}
#[test]
fn url_dir_is_the_sha256_of_the_url() -> TestResult {
let url = GitUrl::new(URL)?;
let dir = url_dir(Path::new("/home/u/.airsstack"), &url);
let hex = ContentHash::of_bytes(URL.as_bytes());
assert_eq!(
dir,
Path::new("/home/u/.airsstack/cache/git").join(hex.hex())
);
assert_eq!(
dir.parent(),
Some(Path::new("/home/u/.airsstack/cache/git"))
);
Ok(())
}
#[test]
fn rev_dir_appends_rev_dot_git() -> TestResult {
let dir = url_dir(Path::new("/home/u"), &GitUrl::new(URL)?);
let rev_dir = rev_dir(&dir, &CommitId::new(REV)?);
assert_eq!(rev_dir, dir.join(format!("{REV}.git")));
assert_eq!(
rev_dir.file_name().and_then(OsStr::to_str),
Some(format!("{REV}.git").as_str())
);
Ok(())
}
#[test]
fn temp_name_counts_up() {
assert_eq!(temp_name(42, 0), "tmp-42-0");
assert_eq!(temp_name(42, 1), "tmp-42-1");
}
}