Skip to content

Commit 63466b1

Browse files
authored
Merge pull request #798 from codeflash-ai/temporal-python--updated
rework new process discovery
2 parents c4a19d7 + 18df53f commit 63466b1

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# ruff: noqa
22
import sys
3+
from pathlib import Path
34
from typing import Any
5+
import pickle
6+
47

58
# This script should not have any relation to the codeflash package, be careful with imports
69
cwd = sys.argv[1]
@@ -11,44 +14,48 @@
1114
sys.path.insert(1, str(cwd))
1215

1316

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+
1427
class PytestCollectionPlugin:
1528
def pytest_collection_finish(self, session) -> None:
16-
global pytest_rootdir
29+
global pytest_rootdir, collected_tests
30+
1731
collected_tests.extend(session.items)
1832
pytest_rootdir = session.config.rootdir
1933

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+
2040
def pytest_collection_modifyitems(self, items) -> None:
2141
skip_benchmark = pytest.mark.skip(reason="Skipping benchmark tests")
2242
for item in items:
2343
if "benchmark" in item.fixturenames:
2444
item.add_marker(skip_benchmark)
2545

2646

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-
3747
if __name__ == "__main__":
38-
from pathlib import Path
39-
4048
import pytest
4149

4250
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"],
4553
plugins=[PytestCollectionPlugin()],
4654
)
4755
except Exception as e:
4856
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

Comments
 (0)