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.hooks.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"); 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); 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"); 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(); 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(); assert_eq!(c.pkgver, "r1.0"); assert_eq!(c.pkgrel, "1"); let pins: Vec = serde_json::from_str(&c.upstream_pins_json).unwrap(); assert_eq!(pins[0].commit, head); 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().unwrap(), without.hash().unwrap(), "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"); 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); 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"); 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() { let (env, _fx) = env_with_fixture("git-sourced"); use std::sync::atomic::{AtomicUsize, Ordering}; let inside = std::sync::Arc::new(AtomicUsize::new(0)); let attempted = std::sync::Arc::new(AtomicUsize::new(0)); let held = upstream::StageBSlot::acquire(1); let racers: Vec<_> = (0..3) .map(|_| { let inside = inside.clone(); let attempted = attempted.clone(); std::thread::spawn(move || { attempted.fetch_add(1, Ordering::SeqCst); let _slot = upstream::StageBSlot::acquire(1); let n = inside.fetch_add(1, Ordering::SeqCst) + 1; assert_eq!(n, 1, "two Stage B runs held the only slot at once"); inside.fetch_sub(1, Ordering::SeqCst); }) }) .collect(); // Every racer is queued on the slot before it is released, so a cap that // leaked would let them all in at once rather than by luck of scheduling. while attempted.load(Ordering::SeqCst) < 3 { std::thread::yield_now(); } assert_eq!(inside.load(Ordering::SeqCst), 0, "a second Stage B entered while the only slot was held"); drop(held); for r in racers { r.join().unwrap(); } assert_eq!(inside.load(Ordering::SeqCst), 0); let _ = env; } #[test] fn an_unprobeable_source_forces_stage_b_instead_of_claiming_no_change() { 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() { 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(); 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"); 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}"); 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'", &[]); 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"); let mtime_before = std::fs::metadata(&cache).ok().and_then(|m| m.modified().ok()); 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; }