|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Visit https://aboutcode.org and https://github.com/aboutcode-org/univers for support and download. |
| 6 | + |
| 7 | +""" |
| 8 | +Regenerate tests/data/schema/nix_version_cmp.json from the Nix reference |
| 9 | +implementation. Requires the ``nix-instantiate`` command: every expected |
| 10 | +output is the answer of the real ``builtins.compareVersions``, evaluated over |
| 11 | +all pairs from a corpus of version strings chosen to exercise every branch of |
| 12 | +the comparison. |
| 13 | +
|
| 14 | +Run from the repository root: |
| 15 | + python etc/scripts/gen_nix_version_cmp.py |
| 16 | +""" |
| 17 | + |
| 18 | +import itertools |
| 19 | +import json |
| 20 | +import subprocess |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +CORPUS = [ |
| 24 | + ".", |
| 25 | + "-", |
| 26 | + "1", |
| 27 | + "007", |
| 28 | + "1.0", |
| 29 | + "1.0.0", |
| 30 | + "1-0", |
| 31 | + "1.0.1", |
| 32 | + "1.9", |
| 33 | + "1.10", |
| 34 | + "1.1.1k", |
| 35 | + "2.3", |
| 36 | + "2.3.1", |
| 37 | + "2.3a", |
| 38 | + "2.3b", |
| 39 | + "2.3pre", |
| 40 | + "2.3pre1", |
| 41 | + "2.3.pre1", |
| 42 | + "2.3-pre.1", |
| 43 | + "2.3preX", |
| 44 | + "pre", |
| 45 | + "pre1", |
| 46 | + "a", |
| 47 | + "abc", |
| 48 | + "openssl", |
| 49 | + "2147483647", |
| 50 | + "2147483648", |
| 51 | + "9223372036854775808", |
| 52 | + "1.2147483648", |
| 53 | + "1.2147483648.0", |
| 54 | + "0.0.0", |
| 55 | + "10.0.0", |
| 56 | + "1..2", |
| 57 | + "1.-2", |
| 58 | + "1.0rc1", |
| 59 | + "1.0.rc1", |
| 60 | + "unstable-2024-01-01", |
| 61 | + "0-unstable-2024-01-01", |
| 62 | + "v1.0", |
| 63 | + "V2", |
| 64 | + "ABC", |
| 65 | + "152.0.7977.60", |
| 66 | + "152.0.7977.82", |
| 67 | + "9.8p1", |
| 68 | + "3.0.2-r10", |
| 69 | +] |
| 70 | + |
| 71 | +EXPR = ( |
| 72 | + "{pairs}: map" |
| 73 | + " (p: builtins.compareVersions (builtins.elemAt p 0) (builtins.elemAt p 1))" |
| 74 | + " (builtins.fromJSON pairs)" |
| 75 | +) |
| 76 | + |
| 77 | +OUTPUT = Path(__file__).parent.parent.parent / "tests" / "data" / "schema" / "nix_version_cmp.json" |
| 78 | + |
| 79 | + |
| 80 | +def main(): |
| 81 | + pairs = list(itertools.product(CORPUS, repeat=2)) |
| 82 | + out = subprocess.run( |
| 83 | + [ |
| 84 | + "nix-instantiate", |
| 85 | + "--eval", |
| 86 | + "--strict", |
| 87 | + "--json", |
| 88 | + "--expr", |
| 89 | + EXPR, |
| 90 | + "--argstr", |
| 91 | + "pairs", |
| 92 | + json.dumps(pairs), |
| 93 | + ], |
| 94 | + capture_output=True, |
| 95 | + text=True, |
| 96 | + check=True, |
| 97 | + ).stdout |
| 98 | + results = json.loads(out) |
| 99 | + |
| 100 | + entries = [] |
| 101 | + for (version1, version2), result in zip(pairs, results): |
| 102 | + if result == 0: |
| 103 | + entries.append( |
| 104 | + { |
| 105 | + "description": "Equality test for Nix version, " |
| 106 | + "generated from builtins.compareVersions.", |
| 107 | + "test_group": "basic", |
| 108 | + "test_type": "equality", |
| 109 | + "input": {"input_scheme": "nix", "versions": [version1, version2]}, |
| 110 | + "expected_output": True, |
| 111 | + } |
| 112 | + ) |
| 113 | + else: |
| 114 | + ordered = [version1, version2] if result < 0 else [version2, version1] |
| 115 | + entries.append( |
| 116 | + { |
| 117 | + "description": "Comparison test for Nix version, " |
| 118 | + "generated from builtins.compareVersions.", |
| 119 | + "test_group": "basic", |
| 120 | + "test_type": "comparison", |
| 121 | + "input": {"input_scheme": "nix", "versions": [version1, version2]}, |
| 122 | + "expected_output": ordered, |
| 123 | + } |
| 124 | + ) |
| 125 | + |
| 126 | + with open(OUTPUT, "w") as f: |
| 127 | + json.dump(entries, f, indent=1) |
| 128 | + print(f"wrote {len(entries)} entries to {OUTPUT}") |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + main() |
0 commit comments