#!/usr/bin/env python3
"""Run first the tests a checkout's changes most likely affect.
Reads `fossil changes` and `fossil extras` in the current checkout and maps
each changed file to the tests worth running first:
- an end-to-end test file (one src/e2e.zig imports, such as src/e2e/pull.zig
or src/core/checkin_e2e.zig) runs only its own tests, through the filter
`-De2e-filter=e2e.pull.test.`, which matches every test of that file;
- a verb, src/cmd/X.zig, runs src/e2e/X.zig's tests the same way, and
`zig build test` as well when the verb carries inline tests;
- src/e2e.zig or src/e2e/support.zig, which every end-to-end test uses, runs
the whole of `zig build e2e`;
- anything else under src/, and build.zig, cannot be mapped to a few tests
(a library file is reached by most of them), so it runs `zig build test`
and says so. Files outside src/ (docs, scripts) run nothing.
All the filters go to one build: `-De2e-filter` repeats, and a test runs
when it matches any of them.
Usage: scripts/affected-tests.py [--print]
--print print the commands without running them
This is a first look, not a verdict: the full battery is still the gate.
"""
import os
import re
import subprocess
import sys
BATTERY = "python3 scripts/check-citations.py; zig fmt --check build.zig src; zig build test; zig build e2e; zig build lanes"
def changed_files():
paths = []
for args in (["fossil", "changes"], ["fossil", "extras"]):
out = subprocess.run(args, capture_output=True, text=True, check=True).stdout
for line in out.splitlines():
words = line.split()
if not words:
continue
# `fossil changes` prints `STATUS PATH`, a rename `STATUS OLD -> NEW`;
# `fossil extras` prints the bare path.
paths.append(words[-1])
return sorted(set(paths))
def e2e_files():
"""The files src/e2e.zig's test block imports, as paths under src/."""
with open("src/e2e.zig") as f:
return set(re.findall(r'_ = @import\("([^"]+\.zig)"\);', f.read()))
def filter_for(src_relative):
# A test's name starts with its file's module path: e2e/pull.zig's tests
# are `e2e.pull.test.<name>`.
return src_relative[: -len(".zig")].replace("/", ".") + ".test."
def has_inline_tests(path):
with open(path, errors="replace") as f:
return any(line.startswith('test "') for line in f)
def plan(paths):
imported = e2e_files()
filters, unit, whole_e2e, notes = [], False, False, []
for path in paths:
if not path.startswith("src/") and path != "build.zig":
continue
rel = path[len("src/"):]
if path == "build.zig":
unit = True
notes.append(f"{path}: the build itself; running the unit step")
elif rel in ("e2e.zig", "e2e/support.zig"):
whole_e2e = True
notes.append(f"{path}: every end-to-end test uses it; running all of them")
elif rel in imported:
filters.append(filter_for(rel))
elif rel.startswith("cmd/") and rel.endswith(".zig"):
verb_tests = "e2e/" + rel[len("cmd/"):]
if verb_tests in imported:
filters.append(filter_for(verb_tests))
else:
notes.append(f"{path}: no src/{verb_tests}; running the unit step")
unit = True
if os.path.exists(path) and has_inline_tests(path):
unit = True
else:
unit = True
notes.append(f"{path}: not mapped to particular tests; running the unit step")
commands = []
if unit:
commands.append(["zig", "build", "test"])
if whole_e2e:
commands.append(["zig", "build", "e2e"])
elif filters:
commands.append(["zig", "build", "e2e"] + [f"-De2e-filter={f}" for f in sorted(set(filters))])
return commands, notes
def main():
if sys.argv[1:] not in ([], ["--print"]):
sys.exit(__doc__.split("Usage: ")[1].split("\n\n")[0])
commands, notes = plan(changed_files())
for note in notes:
print(note)
if not commands:
print("no change under src/ or build.zig; nothing to run first")
failed = False
for command in commands:
print("$ " + " ".join(command))
if sys.argv[1:] == ["--print"]:
continue
if subprocess.run(command).returncode != 0:
failed = True
print("FAILED: " + " ".join(command))
break
print(f"This is a first look, not the gate: the full battery ({BATTERY}) still decides.")
sys.exit(1 if failed else 0)
if __name__ == "__main__":
main()