|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import json |
| 10 | +from pathlib import Path |
10 | 11 |
|
11 | 12 | import pytest |
12 | 13 |
|
@@ -63,3 +64,114 @@ def test_file_result_logger_writes_jsonl(tmp_path): |
63 | 64 | assert obj["task_id"] == report["task_id"] |
64 | 65 | assert obj["repeat_idx"] == report["repeat_idx"] |
65 | 66 | assert "traces" in obj and "config" in obj and "eval" in obj |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.core |
| 70 | +def test_file_result_logger_accepts_pathlib_path(tmp_path): |
| 71 | + """Test that FileResultLogger accepts pathlib.Path for output_dir. |
| 72 | +
|
| 73 | + Verifies that the logger works correctly when output_dir is specified |
| 74 | + as a Path object instead of a string. |
| 75 | + """ |
| 76 | + out_dir = tmp_path / "results" |
| 77 | + out_dir.mkdir() |
| 78 | + |
| 79 | + # Pass Path object directly instead of string |
| 80 | + logger = FileResultLogger(output_dir=out_dir, filename_pattern="test_results.jsonl") |
| 81 | + |
| 82 | + benchmark = MockBenchmark(n_tasks=1, n_repeats=1) |
| 83 | + logger.on_run_start(benchmark) # type: ignore[arg-type] |
| 84 | + |
| 85 | + report = { |
| 86 | + "task_id": benchmark.task_ids[0], |
| 87 | + "repeat_idx": 0, |
| 88 | + "traces": {"agent": "trace"}, |
| 89 | + "config": {"model": "gpt"}, |
| 90 | + "eval": {"score": 1.0}, |
| 91 | + } |
| 92 | + logger.on_task_repeat_end(benchmark, report) # type: ignore[arg-type] |
| 93 | + logger.on_run_end(benchmark, [report]) # type: ignore[arg-type] |
| 94 | + |
| 95 | + # Verify file was created |
| 96 | + out_file = out_dir / "test_results.jsonl" |
| 97 | + assert out_file.exists() |
| 98 | + assert isinstance(logger.output_dir, Path) |
| 99 | + |
| 100 | + lines = out_file.read_text().strip().splitlines() |
| 101 | + assert len(lines) == 1 |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.core |
| 105 | +def test_file_result_logger_overwrite_false_prevents_overwriting(tmp_path): |
| 106 | + """Test that FileResultLogger raises error when file exists and overwrite=False. |
| 107 | +
|
| 108 | + Verifies that when overwrite is False (default), attempting to write to |
| 109 | + an existing file raises FileExistsError. |
| 110 | + """ |
| 111 | + out_dir = tmp_path / "results" |
| 112 | + out_dir.mkdir() |
| 113 | + |
| 114 | + # Create an existing file |
| 115 | + existing_file = out_dir / "test_results.jsonl" |
| 116 | + existing_file.write_text("existing content\n") |
| 117 | + |
| 118 | + # Try to create logger with overwrite=False (default) |
| 119 | + logger = FileResultLogger(output_dir=out_dir, filename_pattern="test_results.jsonl", overwrite=False) |
| 120 | + |
| 121 | + benchmark = MockBenchmark(n_tasks=1, n_repeats=1) |
| 122 | + logger.on_run_start(benchmark) # type: ignore[arg-type] |
| 123 | + |
| 124 | + report = { |
| 125 | + "task_id": benchmark.task_ids[0], |
| 126 | + "repeat_idx": 0, |
| 127 | + "traces": {"agent": "trace"}, |
| 128 | + "config": {"model": "gpt"}, |
| 129 | + "eval": {"score": 1.0}, |
| 130 | + } |
| 131 | + |
| 132 | + # Should raise FileExistsError when trying to log first iteration |
| 133 | + with pytest.raises(FileExistsError, match="Output file already exists.*Set overwrite=True"): |
| 134 | + logger.on_task_repeat_end(benchmark, report) # type: ignore[arg-type] |
| 135 | + |
| 136 | + # Verify original file is unchanged |
| 137 | + assert existing_file.read_text() == "existing content\n" |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.core |
| 141 | +def test_file_result_logger_overwrite_true_allows_overwriting(tmp_path): |
| 142 | + """Test that FileResultLogger overwrites existing file when overwrite=True. |
| 143 | +
|
| 144 | + Verifies that when overwrite is True, the logger successfully overwrites |
| 145 | + an existing file with the same name. |
| 146 | + """ |
| 147 | + out_dir = tmp_path / "results" |
| 148 | + out_dir.mkdir() |
| 149 | + |
| 150 | + # Create an existing file |
| 151 | + existing_file = out_dir / "test_results.jsonl" |
| 152 | + existing_file.write_text("existing content\n") |
| 153 | + |
| 154 | + # Create logger with overwrite=True |
| 155 | + logger = FileResultLogger(output_dir=out_dir, filename_pattern="test_results.jsonl", overwrite=True) |
| 156 | + |
| 157 | + benchmark = MockBenchmark(n_tasks=1, n_repeats=1) |
| 158 | + logger.on_run_start(benchmark) # type: ignore[arg-type] |
| 159 | + |
| 160 | + report = { |
| 161 | + "task_id": benchmark.task_ids[0], |
| 162 | + "repeat_idx": 0, |
| 163 | + "traces": {"agent": "trace"}, |
| 164 | + "config": {"model": "gpt"}, |
| 165 | + "eval": {"score": 1.0}, |
| 166 | + } |
| 167 | + logger.on_task_repeat_end(benchmark, report) # type: ignore[arg-type] |
| 168 | + logger.on_run_end(benchmark, [report]) # type: ignore[arg-type] |
| 169 | + |
| 170 | + # Verify file was overwritten with new content |
| 171 | + lines = existing_file.read_text().strip().splitlines() |
| 172 | + assert len(lines) == 1 |
| 173 | + assert "existing content" not in existing_file.read_text() |
| 174 | + |
| 175 | + obj = json.loads(lines[0]) |
| 176 | + assert obj["task_id"] == report["task_id"] |
| 177 | + assert obj["repeat_idx"] == report["repeat_idx"] |
0 commit comments