"""U02-1 / U11-2: the triton_fa2 gate must not be written when no GPU test ran.""" import importlib.util import os import subprocess import pytest REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def _load(): spec = importlib.util.spec_from_file_location( "gate_flash_attention_under_test", os.path.join(REPO, "scripts", "gate_flash_attention.py") ) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def _junit(passed=0, skipped=0, failed=0): cases = ( "".join(f'' for i in range(passed)) + "".join(f'' for i in range(skipped)) + "".join(f'' for i in range(failed)) ) return f'{cases}' @pytest.fixture def harness(monkeypatch): mod = _load() written = [] monkeypatch.setattr(mod.gate_mod, "write_equivalence_gate", lambda **kw: written.append(kw) or "/x") monkeypatch.setattr(mod.gate_mod, "gate_status", lambda *a, **k: (True, "ok")) monkeypatch.setattr(mod.gate_mod, "kernel_source_hash", lambda: "h") def run_with(xml, cvd="0"): monkeypatch.setenv("CUDA_VISIBLE_DEVICES", cvd) def fake_run(cmd, **kw): arg = next(a for a in cmd if a.startswith("--junitxml=")) with open(arg.split("=", 1)[1], "w") as fh: fh.write(xml) return subprocess.CompletedProcess(cmd, 0) monkeypatch.setattr(mod.subprocess, "run", fake_run) return mod.main() return run_with, written def test_all_skipped_suite_does_not_write_gate(harness): run, written = harness assert run(_junit(skipped=35)) != 0 assert written == [] def test_any_skip_does_not_write_gate(harness): run, written = harness assert run(_junit(passed=34, skipped=1)) != 0 assert written == [] def test_fully_executed_suite_writes_gate(harness): run, written = harness assert run(_junit(passed=35)) == 0 assert len(written) == 1 @pytest.mark.parametrize("cvd", ["0,1", "0,3", ",", "0,"]) def test_multiple_or_malformed_device_list_refused(harness, cvd): run, written = harness assert run(_junit(passed=35), cvd=cvd) == 2 assert written == [] def test_unparseable_junit_report_does_not_write_gate(harness): run, written = harness assert run("not xml at all") != 0 assert written == []