From 0b3cb443e70b46ec61455255c3ddfc2a26997613 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 07:16:36 +0000 Subject: =?UTF-8?q?watch:=20two-stage=20VCS=20watcher=20=E2=80=94=20ls-rem?= =?UTF-8?q?ote=20probe,=20pkgver()=20resolution,=20pin=20capture=20(=C2=A7?= =?UTF-8?q?5.2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- tests/vcs.rs | 263 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 tests/vcs.rs (limited to 'tests/vcs.rs') 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) { + 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 = 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 = 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 = 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 = 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; +} -- cgit v1.2.3