Files
komorebi/e2e/platforms/qemu_windows/cargo_tests.py
T
LGUG2Z 3e21ebd3a4 feat(e2e): windows-nextest runs the Windows unit tests in the guest
The Windows crates' unit tests could only run in the (future) hosted Windows
CI. They run in the existing Windows e2e guest: nix run .#windows-nextest
boots the golden image like a scenario (restoring a saved state when
KOMOREBI_E2E_STATE is set), uploads the test executables that the check
windows-nextest-build cross-compiled, runs every test in a process of its own
(nextest.exe is not needed: komorebi's tests share global state, so one
process per test is what nextest does too) and prints a line per executable
and the failures, with OUT/cargo-tests.json for every result.

The driver e2e/platforms/qemu_windows/cargo_tests.py already existed and was
wired to nothing; it now takes the check output (tests.tsv) as well as cargo
messages, creates the saved state itself, prints the panic message of a
failure, and uploads the data files the tests read. Those are found by the
CARGO_MANIFEST_DIR baked into the executables, a path of the Nix build, so
the check also keeps schemas/ and the tests' fixtures and the driver puts them
at that path under C:. Without them 20 tests failed for the missing files.

docs/dev/build-and-test.md and docs/dev/flake-checks.md describe the check,
the app and the neutral crates in linux-nextest.

Verified: nix run .#windows-nextest on the golden image g8 (scale 1.5, state
~/e2e-w0/state-nextest): 964 tests in 37 executables, 964 passed, 0 failed,
202 s with the state restored (first run 163 s more to save the state).
One run only; not soaked. No e2e scenario, the golden image or the existing
e2e-windows behaviour changed.
2026-11-05 15:03:27 -08:00

126 lines
5.1 KiB
Python

"""Runs the cargo tests of the workspace in the Windows guest.
usage: python -m platforms.qemu_windows.cargo_tests GOLDEN OUT STATE TESTS
TESTS is a directory with a `tests.tsv` (name, tab, file under `tests/`: the
output of the flake check `windows-nextest-build`), or the
`--message-format json` output of `cargo test --workspace --no-run` for the
Windows target, which names every test executable. STATE is a directory to
restore the guest from (made if missing, as `KOMOREBI_E2E_STATE` does for the
scenarios), or `-` to boot. Each is uploaded to the guest, asked for its tests, and every
test is run in a process of its own (as nextest does: komorebi's tests share
global state), with its stdout kept. Prints a line per executable and the
tests that failed; OUT/cargo-tests.json has every result. Exits 1 if any test
failed or an executable could not be run.
"""
import json
import sys
import time
from pathlib import Path
from .guest import Guest
from .state import ensure
GUEST_DIR = "C:\\e2e\\tests"
def executables(messages: Path) -> list[tuple[str, Path]]:
if messages.is_dir():
listing = (messages / "tests.tsv").read_text().splitlines()
return sorted(
(name, messages / "tests" / file)
for name, file in (line.split("\t") for line in listing)
)
found = {}
for line in messages.read_text().splitlines():
try:
message = json.loads(line)
except ValueError:
continue
if message.get("executable") and message.get("profile", {}).get("test"):
kinds = ",".join(message["target"]["kind"])
found[f"{message['target']['name']}-{kinds}"] = Path(message["executable"])
return sorted(found.items())
def tests_of(guest: Guest, exe: str) -> list[str]:
listing = guest.agent.exec(exe, "--list", "--format", "terse")
return [
line[: -len(": test")]
for line in listing["stdout"].splitlines()
if line.endswith(": test")
]
def upload_data(guest: Guest, tests: Path) -> None:
"""Puts the data files the tests read (the schemas, fixtures) in the guest
at the path the build baked into the executables (CARGO_MANIFEST_DIR, the
build directory of the check, as a path on the guest's C: drive)."""
if not (tests.is_dir() and (tests / "srcroot").exists()):
return
root = "C:" + (tests / "srcroot").read_text().strip().replace("/", "\\")
data = tests / "data"
files = sorted(p for p in data.rglob("*") if p.is_file())
made: set[str] = set()
for path in files:
remote = root + "\\" + str(path.relative_to(data)).replace("/", "\\")
directory = remote.rsplit("\\", 1)[0]
if directory not in made:
guest.agent.exec("cmd", "/c", f"mkdir {directory} 2>nul")
made.add(directory)
guest.agent.put_file(path, remote)
print(f"data: {len(files)} files under {root}", flush=True)
def reason(output: str) -> str:
"""The lines that say why a test failed: the panic message, else the end."""
lines = [l for l in output.splitlines() if l.strip()]
for i, line in enumerate(lines):
if "panicked at" in line:
return " | ".join(lines[i : i + 2])
return " | ".join(lines[-3:])
def main(argv: list[str]) -> int:
golden, out, state, messages = (Path(a) for a in argv[:4])
out.mkdir(parents=True, exist_ok=True)
saved = ensure(golden, state) if str(state) != "-" else None
results: dict[str, dict] = {}
with Guest(golden, out / "guest", state=saved) as guest:
guest.agent.exec("cmd", "/c", f"mkdir {GUEST_DIR} 2>nul")
upload_data(guest, messages)
for name, path in executables(messages):
exe = f"{GUEST_DIR}\\{name}.exe"
began = time.monotonic()
guest.agent.put_file(path, exe)
names = tests_of(guest, exe)
passed, failed = 0, []
for test in names:
run = guest.agent.exec(exe, "--exact", test, "--test-threads", "1")
if run["status"] == 0:
passed += 1
else:
failed.append({
"test": test, "status": run["status"],
"output": (run["stdout"] + run["stderr"])[-4000:],
})
results[name] = {
"tests": len(names), "passed": passed, "failed": failed,
"seconds": round(time.monotonic() - began, 1),
}
print(f"{name}: {passed} of {len(names)} passed"
f"{'' if not failed else f', {len(failed)} FAILED'}"
f" ({results[name]['seconds']} s)", flush=True)
for f in failed:
print(f" FAIL {f['test']} (status {f['status']}): {reason(f['output'])}", flush=True)
(out / "cargo-tests.json").write_text(json.dumps(results, indent=2))
total = sum(r["tests"] for r in results.values())
failed = sum(len(r["failed"]) for r in results.values())
print(f"all: {total - failed} of {total} passed, {failed} failed")
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))