|
1 | 1 | # ruff: noqa |
2 | 2 | import sys |
| 3 | +from pathlib import Path |
3 | 4 | from typing import Any |
| 5 | +import pickle |
| 6 | + |
4 | 7 |
|
5 | 8 | # This script should not have any relation to the codeflash package, be careful with imports |
6 | 9 | cwd = sys.argv[1] |
|
11 | 14 | sys.path.insert(1, str(cwd)) |
12 | 15 |
|
13 | 16 |
|
| 17 | +def parse_pytest_collection_results(pytest_tests: list[Any]) -> list[dict[str, str]]: |
| 18 | + test_results = [] |
| 19 | + for test in pytest_tests: |
| 20 | + test_class = None |
| 21 | + if test.cls: |
| 22 | + test_class = test.parent.name |
| 23 | + test_results.append({"test_file": str(test.path), "test_class": test_class, "test_function": test.name}) |
| 24 | + return test_results |
| 25 | + |
| 26 | + |
14 | 27 | class PytestCollectionPlugin: |
15 | 28 | def pytest_collection_finish(self, session) -> None: |
16 | | - global pytest_rootdir |
| 29 | + global pytest_rootdir, collected_tests |
| 30 | + |
17 | 31 | collected_tests.extend(session.items) |
18 | 32 | pytest_rootdir = session.config.rootdir |
19 | 33 |
|
| 34 | + # Write results immediately since pytest.main() will exit after this callback, not always with a success code |
| 35 | + tests = parse_pytest_collection_results(collected_tests) |
| 36 | + exit_code = getattr(session.config, "exitstatus", 0) |
| 37 | + with Path(pickle_path).open("wb") as f: |
| 38 | + pickle.dump((exit_code, tests, pytest_rootdir), f, protocol=pickle.HIGHEST_PROTOCOL) |
| 39 | + |
20 | 40 | def pytest_collection_modifyitems(self, items) -> None: |
21 | 41 | skip_benchmark = pytest.mark.skip(reason="Skipping benchmark tests") |
22 | 42 | for item in items: |
23 | 43 | if "benchmark" in item.fixturenames: |
24 | 44 | item.add_marker(skip_benchmark) |
25 | 45 |
|
26 | 46 |
|
27 | | -def parse_pytest_collection_results(pytest_tests: list[Any]) -> list[dict[str, str]]: |
28 | | - test_results = [] |
29 | | - for test in pytest_tests: |
30 | | - test_class = None |
31 | | - if test.cls: |
32 | | - test_class = test.parent.name |
33 | | - test_results.append({"test_file": str(test.path), "test_class": test_class, "test_function": test.name}) |
34 | | - return test_results |
35 | | - |
36 | | - |
37 | 47 | if __name__ == "__main__": |
38 | | - from pathlib import Path |
39 | | - |
40 | 48 | import pytest |
41 | 49 |
|
42 | 50 | try: |
43 | | - exitcode = pytest.main( |
44 | | - [tests_root, "-p no:logging", "--collect-only", "-m", "not skip", "-p", "no:codeflash-benchmark"], |
| 51 | + pytest.main( |
| 52 | + [tests_root, "-p", "no:logging", "--collect-only", "-m", "not skip", "-p", "no:codeflash-benchmark"], |
45 | 53 | plugins=[PytestCollectionPlugin()], |
46 | 54 | ) |
47 | 55 | except Exception as e: |
48 | 56 | print(f"Failed to collect tests: {e!s}") |
49 | | - exitcode = -1 |
50 | | - tests = parse_pytest_collection_results(collected_tests) |
51 | | - import pickle |
52 | | - |
53 | | - with Path(pickle_path).open("wb") as f: |
54 | | - pickle.dump((exitcode, tests, pytest_rootdir), f, protocol=pickle.HIGHEST_PROTOCOL) |
| 57 | + try: |
| 58 | + with Path(pickle_path).open("wb") as f: |
| 59 | + pickle.dump((-1, [], None), f, protocol=pickle.HIGHEST_PROTOCOL) |
| 60 | + except Exception as pickle_error: |
| 61 | + print(f"Failed to write failure pickle: {pickle_error!s}", file=sys.stderr) |
0 commit comments