|
| 1 | +import os |
1 | 2 | import sys |
| 3 | +import tempfile |
2 | 4 | from pathlib import Path |
3 | 5 |
|
4 | 6 | import pytest |
|
15 | 17 |
|
16 | 18 | # Decorator to skip E2B tests when E2B is not available |
17 | 19 | skip_e2b = pytest.mark.skipif(not _HAS_E2B, reason="E2B not installed") |
| 20 | + |
| 21 | + |
| 22 | +# ============================================================================ |
| 23 | +# Test isolation for TinyDB storage |
| 24 | +# ============================================================================ |
| 25 | +# Each test session gets an isolated .eval_protocol directory to prevent |
| 26 | +# concurrent test workers from corrupting the shared logs.json file. |
| 27 | +# This is especially important in CI where pytest-xdist runs tests in parallel. |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="session", autouse=True) |
| 31 | +def isolated_eval_protocol_dir(tmp_path_factory): |
| 32 | + """ |
| 33 | + Create an isolated .eval_protocol directory for the test session. |
| 34 | +
|
| 35 | + This prevents concurrent test workers from corrupting the shared |
| 36 | + ~/.eval_protocol/logs.json file when using TinyDB storage. |
| 37 | + """ |
| 38 | + # Create a unique temp directory for this test session/worker |
| 39 | + isolated_dir = tmp_path_factory.mktemp("eval_protocol") |
| 40 | + |
| 41 | + # Monkeypatch the find_eval_protocol_dir function to return our isolated dir |
| 42 | + import eval_protocol.directory_utils as dir_utils |
| 43 | + |
| 44 | + original_find_eval_protocol_dir = dir_utils.find_eval_protocol_dir |
| 45 | + |
| 46 | + def isolated_find_eval_protocol_dir() -> str: |
| 47 | + os.makedirs(str(isolated_dir), exist_ok=True) |
| 48 | + return str(isolated_dir) |
| 49 | + |
| 50 | + dir_utils.find_eval_protocol_dir = isolated_find_eval_protocol_dir |
| 51 | + |
| 52 | + yield isolated_dir |
| 53 | + |
| 54 | + # Restore original function after tests |
| 55 | + dir_utils.find_eval_protocol_dir = original_find_eval_protocol_dir |
0 commit comments