summaryrefslogtreecommitdiffstats
path: root/tools/gen-vercmp-oracle.py
blob: 992e10c16fe8dbf78013908e490dc4ff92ff92ae (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
#!/usr/bin/env python3
"""Regenerate testcorpus/vercmp-oracle.tsv from pacman's `vercmp`.

The suite used to shell out to `vercmp` 1,090 times per run to differential-test
pkgver::vercmp_string. The verdicts are a function of the two strings and the
pacman version that produced them, so they are recorded once here and compared
in process instead. The live differential still exists — tests/versions.rs runs
it when AUR_LIVE_VERCMP is set — this is the part that runs on every run.

Usage: tools/gen-vercmp-oracle.py [path]
"""

import os
import subprocess
import sys

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",
]

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUT = sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "testcorpus", "vercmp-oracle.tsv")


def main():
    pacman = subprocess.run(["pacman", "-Q", "pacman"], capture_output=True, text=True).stdout.strip()
    rows = []
    for a in FRAGMENTS:
        for b in FRAGMENTS:
            verdict = int(subprocess.run(["vercmp", a, b], capture_output=True, text=True).stdout.strip())
            rows.append(f"{a}\t{b}\t{verdict}")

    with open(OUT, "w") as handle:
        handle.write("# pkgver::vercmp_string differential, generated by tools/gen-vercmp-oracle.py\n")
        handle.write(f"# oracle: {pacman or 'vercmp not owned by pacman'}\n")
        handle.write("# columns: a <TAB> b <TAB> sign(vercmp a b)\n")
        handle.write("\n".join(rows) + "\n")
    print(f"{OUT}: {len(rows)} rows")


if __name__ == "__main__":
    main()