Skip to content

Commit 4616c7e

Browse files
authored
Make test cleanup robust (#4526)
Refactors fixture which was added in #4476 so that it is better cleaned up.
1 parent 8a8c07a commit 4616c7e

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

app/tests/forge_tests/test_evaluate_helpers.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,22 @@ def helpers():
1818
Fixture that renders the helpers.py.template to a real Python module
1919
and makes it importable for both parent and child processes.
2020
"""
21-
# Create a temporary directory and copy the template as a real Python module
22-
temp_dir = tempfile.mkdtemp()
23-
template_path = Path(
24-
"grandchallenge/forge/templates/forge/partials/example_evaluation_method/helpers.py.template"
25-
)
26-
helpers_path = Path(temp_dir) / "helpers.py"
27-
shutil.copy(template_path, helpers_path)
21+
with tempfile.TemporaryDirectory() as temp_dir:
22+
template_path = Path(
23+
"grandchallenge/forge/templates/forge/partials/example_evaluation_method/helpers.py.template"
24+
)
25+
helpers_path = Path(temp_dir) / "helpers.py"
2826

29-
# Add temp directory to sys.path so child processes can import it
30-
sys.path.insert(0, temp_dir)
27+
shutil.copy(template_path, helpers_path)
3128

32-
# Now import it normally
33-
import helpers as helpers_module
29+
try:
30+
sys.path.insert(0, temp_dir)
3431

35-
yield helpers_module
32+
import helpers as helpers_module
3633

37-
# Cleanup: remove from sys.path and delete temp directory
38-
sys.path.remove(temp_dir)
39-
shutil.rmtree(temp_dir, ignore_errors=True)
34+
yield helpers_module
35+
finally:
36+
sys.path.remove(temp_dir)
4037

4138

4239
def working_process(p):

0 commit comments

Comments
 (0)