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
|
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");
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);
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");
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<UpstreamPin> = 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<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");
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;
}
|