| 24 | |
| 25 | |
| 26 | class Context: |
| 27 | def __init__(self, args, output): |
| 28 | self.output = output |
| 29 | self.collector_identity = identity(args.collector) |
| 30 | self.runner_identity = identity(args.runner, executable=True) |
| 31 | self.tool_identity = identity(args.llvm_profdata, executable=True) |
| 32 | self.collector = self.collector_identity["path"] |
| 33 | self.runner = self.runner_identity["path"] |
| 34 | self.tool = self.tool_identity["path"] |
| 35 | self.processes = Processes(output / "commands") |
| 36 | self.universe = None |
| 37 | self.owned_scripts = [identity(Path(__file__).with_name(name)) for name in |
| 38 | ("test_coverage_live_v2.py", "coverage_live_v2_support.py", |
| 39 | "coverage_live_v2_native.py", "coverage_live_v2_admin.py", |
| 40 | "coverage_live_v2_fault.py")] |
| 41 | |
| 42 | def registry(self, case, tests=None, image=None): |
| 43 | path = case / "registry.json" |
| 44 | write_json(path, {"format": "cbm.coverage.registry.v0", |
| 45 | "image_sha256": image or self.runner_identity["sha256"], |
| 46 | "suites": [{"name": SUITE, "tests": tests if tests is not None else TESTS}]}) |
| 47 | return path |
| 48 | |
| 49 | def collect(self, case, registry, runner=None, destination=None, llvm_bin=None, |
| 50 | extra=None, poison=False, fault=False): |
| 51 | destination = destination or (case / "bundle") |
| 52 | runner = str(runner or self.runner) |
| 53 | arguments = ["--format", "2", "--runner", runner, "--registry", str(registry), |
| 54 | "--out", str(destination), "--llvm-bin", str(llvm_bin or Path(self.tool).parent), |
| 55 | "--timeout", "70"] + (extra or []) |
| 56 | argv = [sys.executable, self.collector] + arguments |
| 57 | if fault: |
| 58 | adapter = str(Path(__file__).with_name("coverage_live_v2_fault.py")) |
| 59 | argv = [sys.executable, adapter, self.collector, self.runner, SUITE, |
| 60 | str(case / "native-fault.json")] + arguments |
| 61 | env = clean_env() |
| 62 | env["PYTHONDONTWRITEBYTECODE"] = "1" |
| 63 | env["LLVM_PROFILE_FILE"] = os.devnull |
| 64 | sentinel = None |
| 65 | if poison: |
| 66 | poison_root = case / "caller-coverage" |
| 67 | poison_root.mkdir() |
| 68 | sentinel = poison_root / "sentinel" |
| 69 | sentinel.write_bytes(b"caller coverage is not the collection destination\n") |
| 70 | only_file = case / "caller-selection.txt" |
| 71 | only_file.write_text("nonexistent_suite:nonexistent_test\n", encoding="utf-8") |
| 72 | env.update(CBM_TEST_COVERAGE_DIR=str(poison_root), |
| 73 | CBM_TEST_ONLY="nonexistent_suite:nonexistent_test", CBM_TEST_ONLY_FILE=str(only_file)) |
| 74 | process = self.processes.run(case.name, argv, env, timeout=600) |
| 75 | if sentinel is not None: |
| 76 | require(list(sentinel.parent.iterdir()) == [sentinel] |
| 77 | and sentinel.read_bytes() == b"caller coverage is not the collection destination\n", |
| 78 | "collector used or changed inherited caller coverage directory") |
| 79 | return process, destination |
| 80 | |
| 81 | |
| 82 | def fresh_output(value): |