From ed70a288033248b707c0a01acfecf320e260cfbe Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 20:33:43 +0000 Subject: tests: build each fixture's repos once per binary and copy them Every env_with_fixture call built an AUR stand-in and up to two upstreams from scratch - six git processes a repo, and most fixtures build three of them. The trees are built once per test binary under a cache and copied into the tempdir now, so each test still owns a mutable copy and fx.bump still commits into it. A recipe that names its own directories is the one thing the copy cannot get right, so those few rewrite their work tree and amend instead. Also stopped paying two git config calls and a rev-parse per repo: identity goes in the environment of every test git call, and a repo this file made has one branch and one commit, so the ref file is the tip. --- tests/common/mod.rs | 194 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 160 insertions(+), 34 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index b4f40dd..5489cfa 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,6 +1,8 @@ #![allow(dead_code)] +use std::collections::HashMap; use std::path::{Path, PathBuf}; +use std::sync::{LazyLock, Mutex, OnceLock}; use aur_build::config::{Config, TestHooks}; use aur_build::db::{self, Db}; @@ -263,8 +265,6 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture { let data = root.join(format!("fixture/{name}")); std::fs::create_dir_all(&data).unwrap(); let aur = root.join(format!("aur/{name}.git")); - std::fs::create_dir_all(&aur).unwrap(); - let upstream = root.join(format!("upstream/{name}")); let upstream2 = root.join(format!("upstream/{name}-2")); let subs = [ @@ -272,10 +272,56 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture { ("@UPSTREAM_DIR@".to_string(), upstream.display().to_string()), ("@UPSTREAM2_DIR@".to_string(), upstream2.display().to_string()), ]; - let mut has_upstream2 = false; - let mut recipe_files: Vec<(String, String)> = Vec::new(); - let mut has_upstream_ref = false; + let pristine = pristine_fixture(name); + for (fname, body) in &pristine.data_files { + std::fs::write(data.join(fname), substitute(body, &subs)).unwrap(); + } + copy_tree(&pristine.aur, &aur); + if pristine.upstream.exists() { + copy_tree(&pristine.upstream, &upstream); + } + if pristine.upstream2.exists() { + copy_tree(&pristine.upstream2, &upstream2); + } + if pristine.path_dependent { + // The recipe names directories this test owns, so the copied commit + // still speaks the cache's paths. Rewrite the work tree and fold it into + // the single commit rather than rebuilding the repo from nothing. + for (fname, body) in &pristine.recipe_files { + std::fs::write(aur.join(fname), substitute(body, &subs)).unwrap(); + } + git(&aur, &["add", "-A"]); + git(&aur, &["commit", "-q", "--amend", "-m", "initial"]); + } + + Fixture { + name: name.to_string(), + src, + aur, + data, + manifest, + upstream: pristine.upstream.exists().then_some(upstream), + upstream2: pristine.upstream2.exists().then_some(upstream2), + } +} + +/// A corpus entry as it sits on disk, placeholders intact. +struct Corpus { + recipe_files: Vec<(String, String)>, + data_files: Vec<(String, String)>, + upstream: bool, + upstream2: bool, +} + +fn read_corpus(name: &str) -> Corpus { + let src = corpus_root().join(name); + let mut corpus = Corpus { + recipe_files: Vec::new(), + data_files: Vec::new(), + upstream: false, + upstream2: false, + }; for entry in std::fs::read_dir(&src).unwrap() { let path = entry.unwrap().path(); let fname = path.file_name().unwrap().to_string_lossy().to_string(); @@ -283,40 +329,108 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture { continue; } let body = std::fs::read_to_string(&path).unwrap_or_default(); - let body = subs.iter().fold(body, |b, (from, to)| b.replace(from, to)); - has_upstream_ref |= body.contains(&upstream.display().to_string()); - has_upstream2 |= body.contains(&upstream2.display().to_string()); + corpus.upstream |= body.contains("@UPSTREAM_DIR@"); + corpus.upstream2 |= body.contains("@UPSTREAM2_DIR@"); if matches!(fname.as_str(), "payload.bin" | "data.bin") { - std::fs::write(data.join(&fname), &body).unwrap(); + corpus.data_files.push((fname, body)); } else { - recipe_files.push((fname, body)); + corpus.recipe_files.push((fname, body)); } } - if has_upstream_ref { + corpus +} + +/// The fixture repos, built once per test binary rather than once per test. +/// Everything in them is path-independent except a recipe that names its own +/// directories, so a test copies the trees and only rewrites when it must. +#[derive(Clone)] +struct Pristine { + aur: PathBuf, + upstream: PathBuf, + upstream2: PathBuf, + recipe_files: Vec<(String, String)>, + data_files: Vec<(String, String)>, + path_dependent: bool, +} + +static PRISTINE: LazyLock>> = LazyLock::new(|| Mutex::new(HashMap::new())); +static CACHE_ROOT: OnceLock = OnceLock::new(); + +fn pristine_fixture(name: &str) -> Pristine { + let mut cache = PRISTINE.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(found) = cache.get(name) { + return found.clone(); + } + let root = CACHE_ROOT + .get_or_init(|| tempfile::tempdir().expect("fixture cache")) + .path() + .to_path_buf(); + let corpus = read_corpus(name); + + let data = root.join(format!("{name}/fixture")); + let aur = root.join(format!("{name}/aur.git")); + let upstream = root.join(format!("{name}/upstream")); + let upstream2 = root.join(format!("{name}/upstream-2")); + std::fs::create_dir_all(&data).unwrap(); + let subs = [ + ("@AUR_DIR@".to_string(), data.display().to_string()), + ("@UPSTREAM_DIR@".to_string(), upstream.display().to_string()), + ("@UPSTREAM2_DIR@".to_string(), upstream2.display().to_string()), + ]; + let path_dependent = corpus + .recipe_files + .iter() + .chain(corpus.data_files.iter()) + .any(|(_, b)| b.contains("@AUR_DIR@") || b.contains("@UPSTREAM_DIR@") || b.contains("@UPSTREAM2_DIR@")); + + for (fname, body) in &corpus.data_files { + std::fs::write(data.join(fname), substitute(body, &subs)).unwrap(); + } + if corpus.upstream { make_git_repo(&upstream, &[("README", "upstream\n")], "upstream: initial"); + if std::fs::read_to_string(corpus_root().join(name).join("PKGBUILD")) + .unwrap_or_default() + .contains("#tag=") + { + git(&upstream, &["tag", "v1"]); + } } - if has_upstream2 { + if corpus.upstream2 { make_git_repo(&upstream2, &[("README", "second upstream\n")], "upstream: initial"); } - if std::fs::read_to_string(src.join("PKGBUILD")) - .unwrap_or_default() - .contains("#tag=") - { - git(&upstream, &["tag", "v1"]); - } - make_git_repo( - &aur, - &recipe_files.iter().map(|(n, b)| (n.as_str(), b.as_str())).collect::>(), - "initial", - ); - Fixture { - name: name.to_string(), - src, + let recipe: Vec<(&str, String)> = corpus + .recipe_files + .iter() + .map(|(n, b)| (n.as_str(), substitute(b, &subs))) + .collect(); + make_git_repo(&aur, &recipe.iter().map(|(n, b)| (*n, b.as_str())).collect::>(), "initial"); + + let pristine = Pristine { aur, - data, - manifest, - upstream: has_upstream_ref.then_some(upstream), - upstream2: has_upstream2.then_some(upstream2), + upstream, + upstream2, + recipe_files: corpus.recipe_files, + data_files: corpus.data_files, + path_dependent, + }; + cache.insert(name.to_string(), pristine.clone()); + pristine +} + +fn substitute(body: &str, subs: &[(String, String)]) -> String { + subs.iter().fold(body.to_string(), |b, (from, to)| b.replace(from, to)) +} + +fn copy_tree(from: &Path, to: &Path) { + std::fs::create_dir_all(to).unwrap(); + for entry in std::fs::read_dir(from).unwrap() { + let entry = entry.unwrap(); + let target = to.join(entry.file_name()); + if entry.file_type().unwrap().is_dir() { + copy_tree(&entry.path(), &target); + } else { + std::fs::copy(&entry.path(), &target).unwrap(); + } } } @@ -396,8 +510,6 @@ pub fn pkgbuild(pkgbase: &str, pkgver: &str, pkgrel: &str, extra: &str) -> Strin pub fn make_git_repo(dir: &Path, files: &[(&str, &str)], msg: &str) -> String { std::fs::create_dir_all(dir).unwrap(); git(dir, &["init", "-q", "-b", "master"]); - git(dir, &["config", "user.email", "t@example.invalid"]); - git(dir, &["config", "user.name", "t"]); for (name, body) in files { let p = dir.join(name); if let Some(parent) = p.parent() { @@ -407,7 +519,17 @@ pub fn make_git_repo(dir: &Path, files: &[(&str, &str)], msg: &str) -> String { } git(dir, &["add", "-A"]); git(dir, &["commit", "-q", "-m", msg]); - git(dir, &["rev-parse", "HEAD"]) + head_of(dir) +} + +/// The tip without spawning `git rev-parse`: a repo this file made has one branch +/// and one commit, so the ref file is the answer. +pub fn head_of(dir: &Path) -> String { + std::fs::read_to_string(dir.join(".git/refs/heads/master")) + .or_else(|_| std::fs::read_to_string(dir.join(".git/HEAD"))) + .expect("ref") + .trim() + .to_string() } pub fn git_add_commit(dir: &Path, files: &[(&str, &str)], msg: &str) -> String { @@ -416,7 +538,7 @@ pub fn git_add_commit(dir: &Path, files: &[(&str, &str)], msg: &str) -> String { } git(dir, &["add", "-A"]); git(dir, &["commit", "-q", "-m", msg]); - git(dir, &["rev-parse", "HEAD"]) + head_of(dir) } pub fn git(dir: &Path, args: &[&str]) -> String { @@ -424,6 +546,10 @@ pub fn git(dir: &Path, args: &[&str]) -> String { .arg("-C") .arg(dir) .args(args) + .env("GIT_AUTHOR_NAME", "t") + .env("GIT_AUTHOR_EMAIL", "t@example.invalid") + .env("GIT_COMMITTER_NAME", "t") + .env("GIT_COMMITTER_EMAIL", "t@example.invalid") .output() .expect("git"); assert!( -- cgit v1.2.3