1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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.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");
// 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().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");
// 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;
}
|