diff options
| author | aur-build <aur-build@localhost> | 2026-11-17 21:30:38 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-17 21:30:38 +0000 |
| commit | a6f69962e85ccf0f0cfd86f7c85bf0f9a2c8c09e (patch) | |
| tree | d86bc97c0da59ffe56f41c9dc8edc1ae21d19a5a /tests/common/mod.rs | |
| parent | f586d11f6a1711145ba4804787217a1bab58751d (diff) | |
| download | aur-build-a6f69962e85ccf0f0cfd86f7c85bf0f9a2c8c09e.tar.gz aur-build-a6f69962e85ccf0f0cfd86f7c85bf0f9a2c8c09e.zip | |
test: toolchain shims and the fake-package corpus (§16a)
The harness that makes the rest of this developable. Six real binaries
named exactly like the tools the pipeline shells out to, sharing one
shim module, driven entirely by environment so no production code path
is test-only:
AUR_SHIM_DIR where invocations are recorded (jsonl)
AUR_SHIM_MANIFEST the fixture manifest to obey
AUR_SHIM_FAIL optional tool forced to fail
makechrootpkg emits real .pkg.tar.zst files with .PKGINFO and
.BUILDINFO (the full chroot world, which is why §10.1 filters it),
makepkg answers --printsrcinfo and --vercheck for Stage B, repo-add
rebuilds a gzip'd .db with %FILENAME%/%VERSION%/%SHA256SUM% DESC
entries, gpg writes a detached signature, mkarchroot creates a warm
root. Each one records its invocation so tests assert on what actually
ran.
testcorpus/ holds 17 fixtures, each a PKGBUILD plus a manifest with the
scripted outcome: plain-make, with-patch, install-hooks, skip-checksum,
split-pkgbase, build-fails, check-fails, missing-dep, dep-cycle,
epoch-bump, pkgrel-reset, downgrade, digest-drift, pin-drift,
git-sourced, git-submodules, unresolvable. The harness commits a fixture
into a real git repo standing in for AUR, materialises a local upstream
git repo for VCS fixtures, and swaps payload bytes for the drift cases.
Config.child_env is what puts the shims on PATH for child processes, so
the pipeline runs completely unmodified under test.
Part of P0 (DESIGN.md §17).
Diffstat (limited to 'tests/common/mod.rs')
| -rw-r--r-- | tests/common/mod.rs | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs index b598b80..d5a4f81 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -22,6 +22,10 @@ impl Env { signing_key_id: "TESTKEY".into(), ..Config::default() }; + Self::with_cfg(tmp, cfg) + } + + pub fn with_cfg(tmp: tempfile::TempDir, cfg: Config) -> Self { cfg.ensure_dirs().expect("ensure_dirs"); db::migrate(&cfg.db_path()).expect("migrate"); let db = Db::open_migrated(&cfg.db_path()).expect("open db"); @@ -175,6 +179,153 @@ impl Drop for Env { } } +// ------------------------------------------------------------- fixtures ---- + +/// Root of the checked-in fake-package corpus (§16a). +pub fn corpus_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testcorpus") +} + +/// Directory holding the shim executables cargo just built. Prepending this to +/// PATH is the whole test harness: the pipeline runs unmodified. +pub fn shim_dir() -> PathBuf { + Path::new(&env!("CARGO_BIN_EXE_makechrootpkg")) + .parent() + .unwrap() + .to_path_buf() +} + +/// An environment wired to one corpus fixture: the fixture is committed into a +/// real git repo standing in for AUR, an optional local upstream git repo exists +/// for VCS sources, and the shims are told to obey the fixture manifest. +pub fn env_with_fixture(name: &str) -> (Env, Fixture) { + let tmp = tempfile::tempdir().expect("tempdir"); + let fx = materialize_fixture(tmp.path(), name); + let cfg = Config { + root: tmp.path().to_path_buf(), + signing_key_id: "TESTKEY".into(), + path_prepend: Some(shim_dir()), + shim_manifest: Some(fx.manifest.clone()), + shim_dir: Some(tmp.path().join("shimlog")), + ..Config::default() + }; + std::fs::create_dir_all(cfg.shim_dir.as_ref().unwrap()).unwrap(); + cfg.ensure_dirs().unwrap(); + let env = Env::with_cfg(tmp, cfg); + env.subscribe(name); + (env, fx) +} + +/// Copy a corpus fixture into a server root: recipe files into a git repo that +/// stands in for AUR, payload files beside it so their bytes can be swapped, +/// and a local upstream repo when the recipe references one. +pub fn materialize_fixture(root: &Path, name: &str) -> Fixture { + let src = corpus_root().join(name); + assert!(src.exists(), "no fixture {name} in {}", corpus_root().display()); + let manifest = root.join("manifest.json"); + std::fs::copy(src.join("manifest.json"), &manifest).expect("copy manifest"); + + // Data files that a source URL points at live outside the recipe so a test + // can swap their bytes (the digest-drift case) without touching git. + 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 subs = [ + ("@AUR_DIR@".to_string(), data.display().to_string()), + ("@UPSTREAM_DIR@".to_string(), upstream.display().to_string()), + ]; + + let mut recipe_files: Vec<(String, String)> = Vec::new(); + let mut has_upstream_ref = 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(); + if fname == "manifest.json" { + 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()); + if matches!(fname.as_str(), "payload.bin" | "data.bin") { + std::fs::write(data.join(&fname), &body).unwrap(); + } else { + recipe_files.push((fname, body)); + } + } + if has_upstream_ref { + make_git_repo(&upstream, &[("README", "upstream\n")], "upstream: initial"); + } + make_git_repo( + &aur, + &recipe_files.iter().map(|(n, b)| (n.as_str(), b.as_str())).collect::<Vec<_>>(), + "initial", + ); + Fixture { + name: name.to_string(), + src, + aur, + data, + manifest, + upstream: has_upstream_ref.then_some(upstream), + } +} + +pub struct Fixture { + pub name: String, + pub src: PathBuf, + pub aur: PathBuf, + pub data: PathBuf, + pub manifest: PathBuf, + pub upstream: Option<PathBuf>, +} + +impl Fixture { + /// Commit a new recipe revision, rewriting `pkgver`/`pkgrel`/`epoch`. + pub fn bump(&self, changes: &[(&str, &str)]) -> String { + let path = self.aur.join("PKGBUILD"); + let text = std::fs::read_to_string(&path).unwrap(); + let mut out = String::new(); + for line in text.lines() { + let key = line.split('=').next().unwrap_or("").to_string(); + match changes.iter().find(|(k, _)| *k == key) { + Some((_, v)) => out.push_str(&format!("{key}={v}\n")), + None => out.push_str(line), + } + out.push('\n'); + } + std::fs::write(&path, out).unwrap(); + git(&self.aur, &["add", "-A"]); + git(&self.aur, &["commit", "-q", "-m", "bump"]); + git(&self.aur, &["rev-parse", "HEAD"]) + } + + /// Swap the bytes behind a `SKIP` source without touching the recipe. + pub fn swap_payload(&self, body: &str) { + for f in ["payload.bin", "data.bin"] { + let p = self.data.join(f); + if p.exists() { + std::fs::write(p, body).unwrap(); + return; + } + } + panic!("fixture {} has no payload file", self.name); + } + + /// Add a commit to the local upstream repo (pin drift, §5.2). + pub fn upstream_commit(&self, body: &str) -> String { + let up = self.upstream.clone().expect("fixture has no upstream repo"); + git_add_commit(&up, &[("README", body)], body); + git(&up, &["rev-parse", "HEAD"]) + } + + pub fn manifest_json(&self) -> serde_json::Value { + serde_json::from_str(&std::fs::read_to_string(&self.manifest).unwrap()).unwrap() + } +} + /// A minimal PKGBUILD for tests that need recipe bytes without a fixture dir. pub fn pkgbuild(pkgbase: &str, pkgver: &str, pkgrel: &str, extra: &str) -> String { format!( |
