summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 07:16:36 +0000
committeraur-build <aur-build@localhost>2026-11-18 07:16:36 +0000
commit0b3cb443e70b46ec61455255c3ddfc2a26997613 (patch)
tree457d5e0bf3b1733b79829de3c146e1cabcc85e8c /tests
parentd7c386a703a9b69211d83854547218d6e1da9a51 (diff)
downloadaur-build-0b3cb443e70b46ec61455255c3ddfc2a26997613.tar.gz
aur-build-0b3cb443e70b46ec61455255c3ddfc2a26997613.zip
watch: two-stage VCS watcher — ls-remote probe, pkgver() resolution, pin capture (§5.2)
Stage A probes every VCS source in source=() with git ls-remote, respecting #commit/#tag/#branch fragments, and returns one UpstreamPin per source. If the tip set equals the accepted release's pins, nothing can have changed and Stage B never runs — the common case costs one round trip per source and no clone. svn/hg have no cheap universal probe, so they are reported as unprobed and force Stage B rather than being silently treated as unchanged. Stage B runs only on movement: it fetches into a persistent vcs_cache (rather than re-cloning a multi-gigabyte upstream daily), runs makepkg --vercheck, and captures the pins from the clone it already has. Results are cached on (pkgbase, recipe_hash, upstream_tip) with a per-pkgbase cooldown, and a process-wide slot cap bounds concurrent resolutions. The resolved epoch:pkgver-pkgrel plus the pins go into identity_hash, so a different commit is a different thing to approve. Also fixes a misclassification the new path exposed: a VCS source's digest is its pin, so upstream movement showed up as an integrity_alert. Integrity events are now restricted to fixed downloads; VCS movement under a frozen pkgver is pin_drift, as §5.2 specifies. New fixtures git-multi (two upstreams) and git-tagged (#tag pin). 10 tests in tests/vcs.rs.
Diffstat (limited to 'tests')
-rw-r--r--tests/common/mod.rs35
-rw-r--r--tests/db.rs2
-rw-r--r--tests/vcs.rs263
3 files changed, 299 insertions, 1 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 0dc5396..d2582c9 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -245,10 +245,13 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture {
std::fs::create_dir_all(&aur).unwrap();
let upstream = root.join(format!("upstream/{name}"));
+ let upstream2 = root.join(format!("upstream/{name}-2"));
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 mut has_upstream2 = false;
let mut recipe_files: Vec<(String, String)> = Vec::new();
let mut has_upstream_ref = false;
@@ -261,6 +264,7 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture {
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());
if matches!(fname.as_str(), "payload.bin" | "data.bin") {
std::fs::write(data.join(&fname), &body).unwrap();
} else {
@@ -270,6 +274,16 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture {
if has_upstream_ref {
make_git_repo(&upstream, &[("README", "upstream\n")], "upstream: initial");
}
+ if has_upstream2 {
+ make_git_repo(&upstream2, &[("README", "second upstream\n")], "upstream: initial");
+ }
+ // A `#tag=v1` source needs the tag to exist.
+ 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::<Vec<_>>(),
@@ -282,6 +296,7 @@ pub fn materialize_fixture(root: &Path, name: &str) -> Fixture {
data,
manifest,
upstream: has_upstream_ref.then_some(upstream),
+ upstream2: has_upstream2.then_some(upstream2),
}
}
@@ -292,6 +307,8 @@ pub struct Fixture {
pub data: PathBuf,
pub manifest: PathBuf,
pub upstream: Option<PathBuf>,
+ /// A second upstream repo, for pkgbases that build from two of them.
+ pub upstream2: Option<PathBuf>,
}
impl Fixture {
@@ -333,6 +350,24 @@ impl Fixture {
git(&up, &["rev-parse", "HEAD"])
}
+ /// Add a commit to the second upstream repo.
+ pub fn upstream2_commit(&self, body: &str) -> String {
+ let up = self.upstream2.clone().expect("fixture has no second upstream repo");
+ git_add_commit(&up, &[(("README"), body)], body);
+ git(&up, &["rev-parse", "HEAD"])
+ }
+
+ /// HEAD of the (first) upstream repo.
+ pub fn upstream_head(&self) -> String {
+ let up = self.upstream.clone().expect("fixture has no upstream repo");
+ git(&up, &["rev-parse", "HEAD"])
+ }
+
+ pub fn upstream2_head(&self) -> String {
+ let up = self.upstream2.clone().expect("fixture has no second upstream repo");
+ 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()
}
diff --git a/tests/db.rs b/tests/db.rs
index f64262a..15c8319 100644
--- a/tests/db.rs
+++ b/tests/db.rs
@@ -39,7 +39,7 @@ fn migrations_apply_from_empty_and_are_idempotent() {
Ok(c.pragma_query_value(None, "user_version", |r| r.get::<_, u32>(0))?)
})
.unwrap();
- assert_eq!(v, 6, "all migrations applied");
+ assert_eq!(v, 7, "all migrations applied");
// Re-running migration over an existing DB is a no-op, not an error.
assert_eq!(db::migrate(&env.cfg.db_path()).unwrap(), 0);
let journal: String = env
diff --git a/tests/vcs.rs b/tests/vcs.rs
new file mode 100644
index 0000000..2572c60
--- /dev/null
+++ b/tests/vcs.rs
@@ -0,0 +1,263 @@
+//! Stage A / Stage B: the VCS watcher (§5.2).
+//!
+//! The whole point of the two-stage funnel is that the expensive thing (clone +
+//! run `pkgver()`) happens only when the cheap thing (`git ls-remote`) says
+//! something moved. These tests assert both halves: that the answer is right,
+//! and that it was paid for only once.
+
+mod common;
+
+use aur_build::db::{self, model::*};
+use aur_build::review::approve::approve as do_approve;
+use aur_build::review::{candidates, diff};
+use aur_build::watch::{detect, upstream};
+use common::*;
+
+fn detect_and_take(env: &Env, name: &str) -> (detect::Outcome, Option<CandidateRow>) {
+ let r = detect::check_pkgbase(&env.cfg, &env.db, name).unwrap();
+ let c = candidates::pending_candidates(&env.db).unwrap().pop();
+ (r.outcome, c)
+}
+
+fn stage_b_rows(env: &Env) -> i64 {
+ env.query_i64("SELECT count(*) FROM stage_b_cache", &[]).unwrap()
+}
+
+fn makepkg_calls(env: &Env) -> usize {
+ aur_build::shims::called_with(&env.cfg.shim_dir.clone().unwrap(), "makepkg", "--vercheck")
+}
+
+#[test]
+fn stage_a_probes_every_vcs_source_not_just_the_first() {
+ let (env, fx) = env_with_fixture("git-multi");
+ // Two upstream repos, two pins — a pkgbase that builds from more than one
+ // upstream can have either one move alone (§5.2).
+ let (outcome, c) = detect_and_take(&env, "git-multi");
+ assert!(matches!(outcome, detect::Outcome::Candidate { .. }), "{outcome:?}");
+ let c = c.unwrap();
+ let pins: Vec<UpstreamPin> = serde_json::from_str(&c.upstream_pins_json).unwrap();
+ assert_eq!(pins.len(), 2, "one pin per VCS source: {pins:?}");
+ assert_eq!(pins[0].commit, fx.upstream_head(), "first source pin");
+ assert_eq!(pins[1].commit, fx.upstream2_head(), "second source pin");
+ assert_eq!(pins[0].index, 0);
+ assert_eq!(pins[1].index, 1);
+ // The resolved version came from Stage B, not from the frozen pkgver= line.
+ assert_eq!(c.pkgver, "r1");
+}
+
+#[test]
+fn stage_a_respects_a_tag_pin() {
+ let (env, fx) = env_with_fixture("git-tagged");
+ let head_at_tag = git(&fx.upstream.clone().unwrap(), &["rev-parse", "v1"]);
+ let (_outcome, c) = detect_and_take(&env, "git-tagged");
+ let pins: Vec<UpstreamPin> = serde_json::from_str(&c.unwrap().upstream_pins_json).unwrap();
+ assert_eq!(pins.len(), 1);
+ assert_eq!(pins[0].ref_, "v1", "the #tag=v1 fragment is the ref we asked for");
+ assert_eq!(pins[0].commit, head_at_tag, "the tag resolved to its commit");
+
+ // Upstream moves; a tagged pin must not follow it.
+ fx.upstream_commit("upstream: moved past the tag\n");
+ let (outcome, _) = detect_and_take(&env, "git-tagged");
+ assert!(
+ matches!(outcome, detect::Outcome::NoChange),
+ "a tag pin does not drift with the branch: {outcome:?}"
+ );
+}
+
+#[test]
+fn stage_a_short_circuits_before_stage_b_when_nothing_moved() {
+ let (env, _fx) = env_with_fixture("git-sourced");
+ let (_outcome, c) = detect_and_take(&env, "git-sourced");
+ let c = c.unwrap();
+ let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
+ do_approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap();
+ let id = aur_build::pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
+ aur_build::pipeline::run(&env.cfg, &env.db, &id).unwrap();
+
+ // Now poll again with nothing changed. Stage A finds the same tip set, so
+ // Stage B must not run at all — this is the common case, every day.
+ let before = makepkg_calls(&env);
+ let cache_before = stage_b_rows(&env);
+ let r = detect::check_pkgbase(&env.cfg, &env.db, "git-sourced").unwrap();
+ assert!(matches!(r.outcome, detect::Outcome::NoChange), "{:?}", r.outcome);
+ assert_eq!(makepkg_calls(&env), before, "Stage B ran on an unchanged tip set");
+ assert_eq!(stage_b_rows(&env), cache_before, "the cache was rewritten");
+}
+
+#[test]
+fn stage_b_resolves_pkgver_and_captures_the_pins() {
+ let (env, fx) = env_with_fixture("git-sourced");
+ let head = fx.upstream_head();
+ let (_outcome, c) = detect_and_take(&env, "git-sourced");
+ let c = c.unwrap();
+ // The candidate's version is what `pkgver()` computed, not the frozen field.
+ assert_eq!(c.pkgver, "r1.0");
+ assert_eq!(c.pkgrel, "1");
+ // And the pins it will be built from are recorded now, while the clone is
+ // already there — not later, which would mean cloning again.
+ let pins: Vec<UpstreamPin> = serde_json::from_str(&c.upstream_pins_json).unwrap();
+ assert_eq!(pins[0].commit, head);
+ // The pins are part of the identity: a different commit is a different
+ // thing to approve, even under the same version string.
+ let with_pins = Identity {
+ aur_commit: "x".into(),
+ epoch: "0".into(),
+ pkgver: "r1.0".into(),
+ pkgrel: "1".into(),
+ upstream_pins: pins.clone(),
+ source_digests: vec![],
+ };
+ let without = Identity { upstream_pins: vec![], ..with_pins.clone() };
+ assert_ne!(with_pins.hash(), without.hash(), "pins must be in identity_hash");
+}
+
+#[test]
+fn stage_b_is_cached_on_pkgbase_recipe_and_tip() {
+ let (env, fx) = env_with_fixture("git-sourced");
+ detect_and_take(&env, "git-sourced");
+ assert_eq!(stage_b_rows(&env), 1);
+ let calls = makepkg_calls(&env);
+ assert!(calls >= 1, "Stage B must actually run once");
+
+ // Same recipe, same tip: the cached answer is reused, no new resolution.
+ detect::check_pkgbase(&env.cfg, &env.db, "git-sourced").unwrap();
+ assert_eq!(makepkg_calls(&env), calls, "cache miss on an unchanged key");
+ assert_eq!(stage_b_rows(&env), 1);
+
+ // A new tip is a new key, so it resolves again — but the cooldown window
+ // holds it back, which is what keeps a chatty upstream from turning
+ // detection into a build loop.
+ fx.upstream_commit("upstream: new tip\n");
+ detect::check_pkgbase(&env.cfg, &env.db, "git-sourced").unwrap();
+ assert_eq!(stage_b_rows(&env), 1, "cooldown should suppress the re-resolve");
+ // Outside the window it resolves again.
+ env.exec("UPDATE stage_b_cache SET resolved_at='2000-01-01T00:00:00Z'", &[]);
+ detect::check_pkgbase(&env.cfg, &env.db, "git-sourced").unwrap();
+ assert_eq!(stage_b_rows(&env), 2, "a new tip outside cooldown must resolve");
+ assert_eq!(makepkg_calls(&env), calls + 1);
+}
+
+#[test]
+fn stage_b_concurrency_cap_is_enforced() {
+ // The cap is a process-wide guard; with the default of 1 a second caller
+ // waits for the first. Two threads, one slot: the second must not enter
+ // until the first releases.
+ let (env, _fx) = env_with_fixture("git-sourced");
+ let slots = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let s1 = slots.clone();
+ let held = upstream::StageBSlot::acquire(1);
+ let entered = std::thread::spawn(move || {
+ let _g = upstream::StageBSlot::acquire(1);
+ s1.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ });
+ std::thread::sleep(std::time::Duration::from_millis(80));
+ assert_eq!(
+ slots.load(std::sync::atomic::Ordering::SeqCst),
+ 0,
+ "a second Stage B entered while the only slot was held"
+ );
+ drop(held);
+ entered.join().unwrap();
+ assert_eq!(slots.load(std::sync::atomic::Ordering::SeqCst), 1);
+ let _ = env;
+}
+
+#[test]
+fn an_unprobeable_source_forces_stage_b_instead_of_claiming_no_change() {
+ // svn/hg have no cheap universal probe. The honest answer is "we do not
+ // know", which means Stage B runs; claiming "nothing moved" would be a lie
+ // that silently stops detection.
+ let recipe = aur_build::recipe::parse_pkgbuild(
+ &std::fs::read_to_string(corpus_root().join("git-sourced/PKGBUILD")).unwrap(),
+ )
+ .unwrap();
+ let mut recipe = recipe;
+ recipe.source_entries = vec![aur_build::db::model::SourceEntry {
+ index: 0,
+ url: "svn+svn://example.invalid/repo".into(),
+ filename: None,
+ commit: None,
+ tag: None,
+ branch: None,
+ vcs: Some("svn".into()),
+ local: false,
+ }];
+ let a = upstream::probe(&recipe, &[]).unwrap();
+ assert!(!a.unprobed.is_empty(), "svn must be reported as unprobed");
+ assert!(a.needs_stage_b(), "unprobed must force Stage B, not short-circuit");
+}
+
+#[test]
+fn a_moved_tip_with_an_unchanged_pkgver_is_a_notice_not_a_candidate() {
+ // §5.2: pins moved, version frozen — pacman cannot offer an upgrade, so
+ // there is nothing to approve. It becomes a notice, and is never dropped.
+ let (env, fx) = env_with_fixture("pin-drift");
+ let (_outcome, c) = detect_and_take(&env, "pin-drift");
+ let c = c.unwrap();
+ let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
+ do_approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap();
+ let id = aur_build::pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
+ aur_build::pipeline::run(&env.cfg, &env.db, &id).unwrap();
+
+ // Upstream moves. The shim still reports the same pkgver (that is what a
+ // degenerate `pkgver()` does), so nothing is publishable.
+ fx.upstream_commit("upstream: 14 more commits\n");
+ env.exec("UPDATE stage_b_cache SET resolved_at='2000-01-01T00:00:00Z'", &[]);
+ let (outcome, new_candidate) = detect_and_take(&env, "pin-drift");
+ match outcome {
+ detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::PIN_DRIFT),
+ other => panic!("expected a pin_drift notice, got {other:?}"),
+ }
+ assert!(new_candidate.is_none(), "a non-publishable change opens no review");
+ // But it is recorded, and says why (§5.2 wording).
+ let notices = db::list_notices(&env.db, false).unwrap();
+ assert_eq!(notices.len(), 1);
+ assert_eq!(notices[0].kind, notice_kind::PIN_DRIFT);
+ let detail = notices[0].detail_json.clone();
+ assert!(detail.contains("nothing publishable"), "{detail}");
+ // The index keeps serving the artifact built from the old pins (I11).
+ assert_eq!(
+ env.query_i64("SELECT count(*) FROM package WHERE state='in_index'", &[]),
+ Some(1)
+ );
+}
+
+#[test]
+fn a_moved_tip_with_a_changed_pkgver_is_a_normal_candidate() {
+ let (env, fx) = env_with_fixture("git-sourced");
+ let (_outcome, c) = detect_and_take(&env, "git-sourced");
+ let c = c.unwrap();
+ let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
+ do_approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap();
+ let id = aur_build::pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
+ aur_build::pipeline::run(&env.cfg, &env.db, &id).unwrap();
+
+ fx.upstream_commit("upstream: a real release\n");
+ env.exec("UPDATE stage_b_cache SET resolved_at='2000-01-01T00:00:00Z'", &[]);
+ // The tool now computes a different version from the new tip.
+ let mut m = fx.manifest_json();
+ m["vercheck"] = serde_json::json!("r2.0-1");
+ std::fs::write(&fx.manifest, serde_json::to_string(&m).unwrap()).unwrap();
+
+ let (outcome, c2) = detect_and_take(&env, "git-sourced");
+ assert!(matches!(outcome, detect::Outcome::Candidate { .. } | detect::Outcome::Collapsed { .. }), "{outcome:?}");
+ let c2 = c2.unwrap();
+ assert_eq!(c2.pkgver, "r2.0");
+ let pins: Vec<UpstreamPin> = serde_json::from_str(&c2.upstream_pins_json).unwrap();
+ assert_eq!(pins[0].commit, fx.upstream_head(), "the new candidate pins the new tip");
+}
+
+#[test]
+fn the_vcs_cache_is_reused_across_polls() {
+ let (env, fx) = env_with_fixture("git-sourced");
+ detect_and_take(&env, "git-sourced");
+ let cache = env.cfg.vcs_cache_path(&format!("file://{}", fx.upstream.clone().unwrap().display()));
+ assert!(cache.exists(), "Stage B should leave a persistent clone behind (§5.2 clone reuse)");
+ let mtime_before = std::fs::metadata(&cache).ok().and_then(|m| m.modified().ok());
+ // A second resolution reuses it rather than re-cloning.
+ env.exec("UPDATE stage_b_cache SET resolved_at='2000-01-01T00:00:00Z'", &[]);
+ fx.upstream_commit("upstream: again\n");
+ detect::check_pkgbase(&env.cfg, &env.db, "git-sourced").unwrap();
+ assert!(cache.exists());
+ let _ = mtime_before;
+}