summaryrefslogtreecommitdiffstats
path: root/tests/common
diff options
context:
space:
mode:
Diffstat (limited to 'tests/common')
-rw-r--r--tests/common/mod.rs151
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!(