summaryrefslogtreecommitdiffstats
path: root/tests/versions.rs
blob: ae6e877ca628d169541d9c2e6d00b1bfc68fa670 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
mod common;

use std::cmp::Ordering;

use aur_build::db::model::*;
use aur_build::pipeline;
use aur_build::pkgver::{self, Dep, DepOp};
use aur_build::review::{candidates, diff};
use common::*;

/// Ordering pairs, each `a` strictly older than `b`. Every pair was checked
/// against pacman's own `vercmp`, because the interesting cases are exactly the
/// ones that are easy to misremember.
const ORDERED: &[(&str, &str)] = &[
    ("v1.2.3", "v1.2.4"),
    ("1.0", "1.1"),
    ("1.1", "1.2"),
    ("1.2", "2.0"),
    ("2.0", "2.1.0"),
    ("2.1.0", "2.1.1"),
    ("1.0rc1", "1.0"),
    ("1.0", "1.0.0"),
    ("1.0.0", "1.0.1"),
    ("1.0.1", "1.1.0"),
    ("1.1", "1.1.0"),
    ("1", "1.0"),
    ("1.0", "1.0.1"),
    ("1.0.1", "1.1"),
    ("1.1", "1.1.1"),
    ("1.1.1", "1.2"),
    ("1.2", "1.2.2"),
    ("1.2.2", "1.3"),
    ("1.3", "1.3.1"),
    ("1.3.1", "1.4"),
    ("1.4", "1.4.1"),
    ("1.4.1", "2.0"),
    ("2.0", "2.0.1"),
    ("2.0.1", "2.1.0"),
    ("2.1.0", "2.1.1"),
    ("2.1.1", "3.0"),
    ("3.0", "3.0.1"),
    ("3.0.1", "4.0"),
    ("1.0.a", "1.0.1"),
    ("1.0.b", "1.0.2"),
    ("1.0.2", "1.0.2.a"),
    ("1.0.2.a", "1.0.2.aa"),
    ("1.0.2.aa", "1.0.2.b"),
    ("1.0.2.b", "1.0.2.bb"),
    ("1.0.2.bb", "1.0.3"),
    ("1.0.3a", "1.0.3b"),
    ("1.0.3b", "1.1"),
    ("1.1a", "1.2"),
    ("1:0.8", "2:0.1"),
    ("2:0.1", "3:0.1"),
    ("1.0-1", "1.0-2"),
    ("1.0-2", "1.0-3"),
    ("1.0-3", "1.1-1"),
    ("1:1.0-1", "1:1.0-2"),
    ("1:1.0-2", "2:0.1-1"),
    ("2.5", "2.24"),
    ("2.0", "2.0.01"),
];

#[test]
fn vercmp_orders_the_canonical_vectors() {
    for (a, b) in ORDERED {
        assert_eq!(
            pkgver::vercmp(a, b),
            Ordering::Less,
            "vercmp({a:?}, {b:?}) should be Less"
        );
        assert_eq!(
            pkgver::vercmp(b, a),
            Ordering::Greater,
            "vercmp({b:?}, {a:?}) should be Greater (comparison must be antisymmetric)"
        );
    }
}

/// The strongest version of this test: compare the hand-written implementation
/// against pacman's own `vercmp` over a cross-product of the shapes that appear in
/// real recipes. Skipped with a note when the binary is absent.
#[test]
fn vercmp_matches_the_system_oracle() {
    if std::process::Command::new("vercmp").arg("1").arg("2").output().is_err() {
        eprintln!("note: pacman's `vercmp` is not on PATH; skipping the differential check");
        return;
    }
    // No `-` here: pacman's `vercmp` binary compares two raw version strings and
    // does not split an epoch/pkgrel triple, so `vercmp_string` is the right side.
    let fragments = [
        "1", "1.0", "1.0.0", "1.0.1", "1.1", "2", "2.0", "1.0a", "1.0rc1", "1.0.2a", "1.0.2.1",
        "1.0.2.a", "1.0.2.aa", "1.0.2.b", "1.0.2z", "1.0z", "1.0.z", "0.9", "10", "2.24", "2.5",
        "1.02", "1.0_2", "1.0.p", "1.0.3a", "1.0.3b", "1.a", "1a", "1.0+git", "1.0~beta", "2.0_1",
        "1.0.x", "2024.01",
    ];
    let mut checked = 0;
    for a in fragments {
        for b in fragments {
            let mine = pkgver::vercmp_string(a, b) as i32;
            let theirs = std::process::Command::new("vercmp")
                .arg(a)
                .arg(b)
                .output()
                .expect("vercmp");
            let theirs: i32 = String::from_utf8_lossy(&theirs.stdout).trim().parse().expect("vercmp output");
            let mine = match mine {
                x if x < 0 => -1,
                0 => 0,
                _ => 1,
            };
            assert_eq!(mine, theirs, "vercmp({a:?}, {b:?}) disagrees with pacman");
            checked += 1;
        }
    }
    assert!(checked > 1000, "the cross-product should be substantial, checked {checked}");
}

#[test]
fn equal_versions_compare_equal_and_leading_zeros_do_not_matter() {
    for (a, b) in [
        ("1.0-1", "1.0-1"),
        ("1:1.0-1", "1:1.0-1"),
        ("1.02-1", "1.2-1"),
        ("01.0-1", "1.0-1"),
        ("1.0-01", "1.0-1"),
    ] {
        assert_eq!(pkgver::vercmp(a, b), Ordering::Equal, "{a} vs {b}");
    }
}

#[test]
fn an_epoch_always_beats_a_larger_version_without_one() {
    // The rule that catches a maintainer who resets a version and adds an epoch.
    assert_eq!(pkgver::vercmp("1:0.1-1", "9.9.9-1"), Ordering::Greater);
    assert_eq!(pkgver::vercmp("9.9.9-1", "1:0.1-1"), Ordering::Less);
    // No epoch means epoch 0, on both sides.
    assert_eq!(pkgver::vercmp("0:1.0-1", "1.0-1"), Ordering::Equal);
    assert_eq!(pkgver::vercmp("1:1.0-1", "1.0-1"), Ordering::Greater);
}

#[test]
fn a_pkgrel_reset_under_a_newer_pkgver_is_still_an_upgrade() {
    // The shape AUR produces constantly: 1.2.3-4 -> 1.2.4-1. Comparing the
    // pkgrel alone would call this a downgrade.
    assert_eq!(pkgver::vercmp("1.2.3-4", "1.2.4-1"), Ordering::Less);
    assert!(!pkgver::is_downgrade("1.2.3-4", "1.2.4-1"));
    // And the reverse really is a downgrade even though pkgrel went up.
    assert!(pkgver::is_downgrade("1.2.4-1", "1.2.3-9"));
}

#[test]
fn alpha_numeric_boundaries_follow_the_chunk_rule() {
    // A numeric chunk is always newer than an alphabetic one at the same position.
    assert_eq!(pkgver::vercmp("1.0.1", "1.0.a"), Ordering::Greater);
    assert_eq!(pkgver::vercmp("1.0.a", "1.0.1"), Ordering::Less);
    // A shorter version is newer when the extra chunk is numeric, older when it is
    // alphabetic.
    assert_eq!(pkgver::vercmp("1.0", "1.0.1"), Ordering::Less);
    assert_eq!(pkgver::vercmp("1.0", "1.0a"), Ordering::Greater);
    assert_eq!(pkgver::vercmp("1.0rc1", "1.0"), Ordering::Less);
    assert_eq!(pkgver::vercmp("1.0", "1.0rc1"), Ordering::Greater);
}

#[test]
fn dependency_strings_parse_with_their_constraints() {
    let cases = [
        ("gcc", "gcc", DepOp::Any, None),
        ("python>=3.11", "python", DepOp::Ge, Some("3.11")),
        ("openssl<3", "openssl", DepOp::Lt, Some("3")),
        ("glibc>=2.39-1", "glibc", DepOp::Ge, Some("2.39-1")),
        ("linux-api-headers=6.0", "linux-api-headers", DepOp::Eq, Some("6.0")),
        ("gcc-libs<=13.2.1-2", "gcc-libs", DepOp::Le, Some("13.2.1-2")),
        // An epoch inside a constraint is part of the version, not the name.
        ("foo>=1:2.3-4", "foo", DepOp::Ge, Some("1:2.3-4")),
        // optdepends carry a description after a colon; it is not a version.
        ("python-pytest: for tests", "python-pytest", DepOp::Any, None),
    ];
    for (src, name, op, ver) in cases {
        let d = Dep::parse(src).unwrap_or_else(|| panic!("failed to parse {src:?}"));
        assert_eq!(d.name(), name, "{src:?} name");
        assert_eq!(d.op, op, "{src:?} operator");
        assert_eq!(d.version.as_deref(), ver, "{src:?} version");
    }
    // Malformed input is dropped, not fatal.
    assert!(Dep::parse("").is_none());
    assert!(Dep::parse(">=1.0").is_none());
    assert_eq!(pkgver::dep_names(&["python>=3.11".into(), "gcc".into()]), vec!["python", "gcc"]);
}

#[test]
fn version_constraints_are_satisfied_by_the_right_versions() {
    let d = Dep::parse("glibc>=2.39-1").unwrap();
    assert!(d.satisfied_by("2.39-1"));
    assert!(d.satisfied_by("2.40-1"));
    assert!(!d.satisfied_by("2.38-1"));
    // An epoch on the installed side satisfies a constraint that has none.
    assert!(Dep::parse("foo>=1.0").unwrap().satisfied_by("1:0.1-1"));
    // `=` means exactly equal in pacman's sense, not string-equal.
    assert!(Dep::parse("foo=1.0-1").unwrap().satisfied_by("1.0-1"));
    assert!(!Dep::parse("foo=1.0-1").unwrap().satisfied_by("1.0-2"));
    // A bare name is satisfied by anything.
    assert!(Dep::parse("foo").unwrap().satisfied_by("0"));
}

#[test]
fn provides_and_conflicts_use_the_same_grammar() {
    // provides/conflicts entries carry versions with the same syntax.
    for src in ["aur-build", "aur-build=0.1-1", "virtual-thing>=1.0"] {
        let d = Dep::parse(src).expect(src);
        assert!(!d.name().is_empty());
        assert!(!d.name().contains(['>', '<', '=']));
    }
    assert_eq!(
        pkgver::dep_names(&["aur-build=0.1-1".into(), "other".into()]),
        vec!["aur-build", "other"]
    );
}

#[test]
fn a_downgrade_in_the_aur_is_flagged_not_silently_served() {
    // A maintainer reverting a version is a mistake far more often than a
    // deliberate rollback.
    let (env, fx) = env_with_fixture("plain-make");
    publish(&env, "plain-make");
    fx.bump(&[("pkgver", "0.9")]);
    let c = detect(&env, "plain-make");
    let base = base_release(&env, &c);
    let panels = diff::panels(&env.cfg, &env.db, &c, base.as_ref()).unwrap();
    let version = panel(&panels, "version");
    assert!(
        version.body.contains("DOWNGRADE"),
        "the version panel must call out a downgrade: {:?}",
        version.body
    );
    assert!(version.body.contains("1.0-1 -> 0.9-1"), "{:?}", version.body);
    assert!(pkgver::is_downgrade("1.0-1", "0.9-1"));
    // A downgrade is a decision, not an automatic rebuild.
    assert_eq!(aur_build::db::index_version(&env.db, "plain-make").unwrap().unwrap(), "1.0-1");
}

#[test]
fn an_epoch_bump_is_an_upgrade_even_though_the_version_went_down() {
    // The classic "reset the version, add an epoch" move.
    let (env, fx) = env_with_fixture("epoch-bump");
    publish(&env, "epoch-bump");
    append_line(&fx, "epoch=1");
    fx.bump(&[("pkgver", "0.9")]);
    let c = detect(&env, "epoch-bump");
    assert_eq!(c.epoch, "1", "the recipe's epoch must be parsed out of the PKGBUILD");
    let full = c.version();
    assert!(full.starts_with("1:"), "epoch belongs to the version: {full}");
    assert_eq!(pkgver::vercmp(&full, "1.0-1"), Ordering::Greater, "1:0.9-1 beats 1.0-1");
    assert!(!pkgver::is_downgrade("1.0-1", &full), "an epoch bump is never a downgrade");

    let base = base_release(&env, &c);
    let panels = diff::panels(&env.cfg, &env.db, &c, base.as_ref()).unwrap();
    let version = panel(&panels, "version");
    assert!(version.body.contains("epoch bump"), "the panel should name the epoch change: {:?}", version.body);
    assert!(!version.body.contains("DOWNGRADE"), "an epoch bump must not read as a downgrade: {:?}", version.body);
}

#[test]
fn full_version_round_trips_through_split() {
    for (epoch, pkgver, pkgrel, full) in [
        ("0", "1.2.3", "4", "1.2.3-4".to_string()),
        ("1", "1.2.3", "4", "1:1.2.3-4".to_string()),
        ("2", "1.0", "1", "2:1.0-1".to_string()),
    ] {
        assert_eq!(pkgver::full_version(epoch, pkgver, pkgrel), full);
        let (e, v, r) = pkgver::split_full_version(&full);
        assert_eq!((e.as_str(), v.as_str(), r.as_str()), (epoch, pkgver, pkgrel), "round trip of {full}");
    }
}

// --- helpers -------------------------------------------------------------

/// Detect, render the diff, approve and build, so the next candidate has a real
/// accepted release behind it (I1).
fn publish(env: &Env, name: &str) {
    let c = detect_raw(env, name);
    let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
    let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
    aur_build::review::approve::approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap();
    let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
    let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
    pipeline::run(&env.cfg, &env.db, &id).unwrap();
}

/// Detect and render, returning the candidate ready for a review page.
fn detect(env: &Env, name: &str) -> CandidateRow {
    detect_raw(env, name)
}

fn detect_raw(env: &Env, name: &str) -> CandidateRow {
    aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap();
    let c = candidates::pending_candidates(&env.db).unwrap().remove(0);
    diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
    candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap()
}

fn base_release(env: &Env, c: &CandidateRow) -> Option<ReleaseRow> {
    match c.base_release_id {
        Some(id) => aur_build::db::release_by_id(&env.db, id).unwrap(),
        None => None,
    }
}

fn panel<'a>(panels: &'a [diff::Panel], name: &str) -> &'a diff::Panel {
    panels.iter().find(|p| p.name == name).unwrap_or_else(|| panic!("no {name} panel"))
}

/// Add a top-level declaration the fixture did not ship with (an `epoch=` line)
/// and commit it. It has to go with the other top-level assignments: after a
/// `package_*()` function it would be inside the function body and mean nothing.
fn append_line(fx: &Fixture, line: &str) {
    let path = fx.aur.join("PKGBUILD");
    let text = std::fs::read_to_string(&path).unwrap();
    let out: String = text
        .lines()
        .flat_map(|l| {
            if l.starts_with("arch=") {
                [line.to_string(), l.to_string()]
            } else {
                [String::new(), l.to_string()]
            }
        })
        .map(|l| format!("{l}\n"))
        .collect();
    let out = out.replace("\n\n", "\n");
    std::fs::write(&path, out).unwrap();
    git(&fx.aur, &["add", "-A"]);
    git(&fx.aur, &["commit", "-q", "-m", "append"]);
}