-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathgen_nix_version_cmp.py
More file actions
132 lines (120 loc) · 3.17 KB
/
Copy pathgen_nix_version_cmp.py
File metadata and controls
132 lines (120 loc) · 3.17 KB
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
#
# Copyright (c) nexB Inc. and others.
# SPDX-License-Identifier: Apache-2.0
#
# Visit https://aboutcode.org and https://github.com/aboutcode-org/univers for support and download.
"""
Regenerate tests/data/schema/nix_version_cmp.json from the Nix reference
implementation. Requires the ``nix-instantiate`` command: every expected
output is the answer of the real ``builtins.compareVersions``, evaluated over
all pairs from a corpus of version strings chosen to exercise every branch of
the comparison.
Run from the repository root:
python etc/scripts/gen_nix_version_cmp.py
"""
import itertools
import json
import subprocess
from pathlib import Path
CORPUS = [
".",
"-",
"1",
"007",
"1.0",
"1.0.0",
"1-0",
"1.0.1",
"1.9",
"1.10",
"1.1.1k",
"2.3",
"2.3.1",
"2.3a",
"2.3b",
"2.3pre",
"2.3pre1",
"2.3.pre1",
"2.3-pre.1",
"2.3preX",
"pre",
"pre1",
"a",
"abc",
"openssl",
"2147483647",
"2147483648",
"9223372036854775808",
"1.2147483648",
"1.2147483648.0",
"0.0.0",
"10.0.0",
"1..2",
"1.-2",
"1.0rc1",
"1.0.rc1",
"unstable-2024-01-01",
"0-unstable-2024-01-01",
"v1.0",
"V2",
"ABC",
"152.0.7977.60",
"152.0.7977.82",
"9.8p1",
"3.0.2-r10",
]
EXPR = (
"{pairs}: map"
" (p: builtins.compareVersions (builtins.elemAt p 0) (builtins.elemAt p 1))"
" (builtins.fromJSON pairs)"
)
OUTPUT = Path(__file__).parent.parent.parent / "tests" / "data" / "schema" / "nix_version_cmp.json"
def main():
pairs = list(itertools.product(CORPUS, repeat=2))
out = subprocess.run(
[
"nix-instantiate",
"--eval",
"--strict",
"--json",
"--expr",
EXPR,
"--argstr",
"pairs",
json.dumps(pairs),
],
capture_output=True,
text=True,
check=True,
).stdout
results = json.loads(out)
entries = []
for (version1, version2), result in zip(pairs, results):
if result == 0:
entries.append(
{
"description": "Equality test for Nix version, "
"generated from builtins.compareVersions.",
"test_group": "basic",
"test_type": "equality",
"input": {"input_scheme": "nix", "versions": [version1, version2]},
"expected_output": True,
}
)
else:
ordered = [version1, version2] if result < 0 else [version2, version1]
entries.append(
{
"description": "Comparison test for Nix version, "
"generated from builtins.compareVersions.",
"test_group": "basic",
"test_type": "comparison",
"input": {"input_scheme": "nix", "versions": [version1, version2]},
"expected_output": ordered,
}
)
with open(OUTPUT, "w") as f:
json.dump(entries, f, indent=1)
print(f"wrote {len(entries)} entries to {OUTPUT}")
if __name__ == "__main__":
main()