diff options
Diffstat (limited to 'src/watch/mirror.rs')
| -rw-r--r-- | src/watch/mirror.rs | 87 |
1 files changed, 78 insertions, 9 deletions
diff --git a/src/watch/mirror.rs b/src/watch/mirror.rs index 924eb52..07a60c0 100644 --- a/src/watch/mirror.rs +++ b/src/watch/mirror.rs @@ -46,7 +46,7 @@ fn record(db: &Db, name: &str, head: &str, mirror: &Path) -> Result<()> { } #[derive(Debug, Clone)] -pub struct Tree { +pub(crate) struct Tree { pub blobs: Vec<(String, String)>, pub files: BTreeMap<String, String>, } @@ -55,18 +55,12 @@ impl Tree { pub fn recipe_hash(&self) -> String { crate::hash::recipe_hash(&self.blobs) } - pub fn get(&self, suffix: &str) -> Option<&String> { - self.files - .iter() - .find(|(k, _)| k.ends_with(suffix)) - .map(|(_, v)| v) - } pub fn json_list(&self) -> String { serde_json::to_string(&self.blobs).unwrap_or_else(|_| "[]".into()) } } -pub fn read_tree(mirror: &Path, commit: &str) -> Result<Tree> { +pub(crate) fn read_tree(mirror: &Path, commit: &str) -> Result<Tree> { // One `ls-tree` and one `cat-file --batch` for the whole tree, so the number // of files a recipe ships does not decide how many processes a read costs. // A commit that is gone from the mirror surfaces here, from `ls-tree`, rather @@ -102,6 +96,81 @@ fn looks_textual(b: &[u8]) -> bool { !b[..b.len().min(512)].contains(&0) } -pub fn read_file(mirror: &Path, commit: &str, path: &str) -> Result<Vec<u8>> { +pub(crate) fn read_file(mirror: &Path, commit: &str, path: &str) -> Result<Vec<u8>> { git::read_blob(mirror, commit, path) } + + +#[cfg(test)] +mod tests { + use super::*; + + /// A throwaway repo with these files committed, and its tip. + /// + /// Deliberately minimal: `read_tree` only ever needs a directory git will + /// answer for, so the test does not stand up a mirror, a database or a + /// config to ask it a question about one tree. + fn repo(files: &[(&str, &[u8])]) -> (std::path::PathBuf, String) { + let nonce = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(); + let dir = std::env::temp_dir().join(format!("aur-read-tree-{}-{}", std::process::id(), nonce)); + std::fs::create_dir_all(&dir).unwrap(); + let git = |args: &[&str]| { + let out = std::process::Command::new("git") + .arg("-C") + .arg(&dir) + .args(args) + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("HOME", &dir) + .output() + .unwrap(); + assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr)); + }; + git(&["init", "-q", "-b", "master"]); + git(&["config", "user.email", "test@example.invalid"]); + git(&["config", "user.name", "test"]); + for (name, body) in files { + std::fs::write(dir.join(name), body).unwrap(); + } + git(&["add", "-A"]); + git(&["commit", "-q", "-m", "initial"]); + let head = std::fs::read_to_string(dir.join(".git/refs/heads/master")).unwrap(); + (dir, head.trim().to_string()) + } + + #[test] + fn read_tree_returns_every_blob_from_one_batch_read() { + let (dir, head) = repo(&[ + ("PKGBUILD", b"pkgbase=foo +pkgver=1.0 +pkgrel=1 +"), + ("empty.txt", b""), + ("blob.bin", b"\x00not text"), + ]); + let tree = read_tree(&dir, &head).unwrap(); + + assert!(tree.files["PKGBUILD"].contains("pkgver=1.0")); + assert_eq!(tree.files["empty.txt"], "", "an empty blob is a file, not a miss"); + assert!( + !tree.files.contains_key("blob.bin"), + "a NUL in the first block means it is not text: {:?}", + tree.files.keys().collect::<Vec<_>>() + ); + assert!(tree.blobs.iter().any(|(p, _)| p == "blob.bin"), "binary blobs are still listed"); + std::fs::remove_dir_all(&dir).ok(); + } + + #[test] + fn read_tree_fails_closed_when_the_commit_is_gone_from_the_mirror() { + let (dir, _head) = repo(&[("PKGBUILD", b"pkgbase=foo +")]); + for gone in ["0123456789012345678901234567890123456789", "deadbeef"] { + let err = read_tree(&dir, gone).unwrap_err().to_string(); + assert!(err.contains("missing from the local mirror"), "{gone}: {err}"); + } + std::fs::remove_dir_all(&dir).ok(); + } +} |
