返回 CodeWhale
nextest-shard-count.py
根目录 / scripts / ci / nextest-shard-count.py
1 #!/usr/bin/env python3
2 """Count nextest tests so a sharded run can prove it ran every test once.
3
4 list LISTING_JSON tests the archive selects (nextest list --message-format json)
5 junit JUNIT_XML tests one nextest invocation ran
6 check DIRECTORY per OS, the shard files must cover the expected total exactly
7
8 The sharded CI suite splits one archive across jobs. A shard that is skipped,
9 runs a different filter, or double-counts a partition changes the sum, so the
10 suite gate fails instead of reporting a smaller run as a pass.
11 """
12 from __future__ import annotations
13
14 import json
15 import sys
16 import xml.etree.ElementTree as ET
17 from pathlib import Path
18
19
20 def listed(path: str) -> int:
21 suites = json.loads(Path(path).read_text(encoding="utf-8"))["rust-suites"]
22 return sum(
23 1
24 for suite in suites.values()
25 for case in suite["testcases"].values()
26 if case["filter-match"]["status"] == "matches"
27 )
28
29
30 def ran(path: str) -> int:
31 return int(ET.parse(path).getroot().get("tests", "0"))
32
33
34 def check(directory: str) -> int:
35 files = sorted(Path(directory).rglob("*.json"))
36 expected = {}
37 shards: dict[str, dict[int, int]] = {}
38 for file in files:
39 record = json.loads(file.read_text(encoding="utf-8"))
40 if file.name.startswith("expected-"):
41 expected[record["os"]] = record
42 elif file.name.startswith("shard-"):
43 slot = shards.setdefault(record["os"], {})
44 if record["shard"] in slot:
45 print(f"{record['os']}: shard {record['shard']} reported twice")
46 return 1
47 slot[record["shard"]] = record["tests"]
48 if not expected:
49 print("no expected-<os>.json found")
50 return 1
51 failed = False
52 for os_name, record in sorted(expected.items()):
53 got = shards.get(os_name, {})
54 want = set(range(1, record["shards"] + 1))
55 total = sum(got.values())
56 print(f"{os_name}: archive lists {record['tests']}, shards ran {total} {dict(sorted(got.items()))}")
57 if set(got) != want:
58 print(f"{os_name}: expected shards {sorted(want)}, found {sorted(got)}")
59 failed = True
60 if total != record["tests"]:
61 print(f"{os_name}: shard total {total} differs from the archive's {record['tests']}")
62 failed = True
63 if any(count <= 0 for count in got.values()):
64 print(f"{os_name}: a shard ran no tests")
65 failed = True
66 return 1 if failed else 0
67
68
69 def main(argv: list[str]) -> int:
70 if len(argv) == 3 and argv[1] == "list":
71 print(listed(argv[2]))
72 return 0
73 if len(argv) == 3 and argv[1] == "junit":
74 print(ran(argv[2]))
75 return 0
76 if len(argv) == 3 and argv[1] == "check":
77 return check(argv[2])
78 print(__doc__, file=sys.stderr)
79 return 2
80
81
82 if __name__ == "__main__":
83 raise SystemExit(main(sys.argv))
84
84 lines PYTHON