Skip to content

Commit b1cb9eb

Browse files
authored
fix(test-logging): don't store empty logs (ethereum#2547)
1 parent cf5d85f commit b1cb9eb

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

packages/testing/src/execution_testing/cli/tests/test_pytest_fill_command.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ def test_fill_no_html_option(
209209
run_fill(*fill_args)
210210
assert not default_html_path.exists()
211211

212+
def test_fill_pytest_help_does_not_create_empty_log_file(
213+
self, pytester: Pytester
214+
) -> None:
215+
"""Help-mode startup should not leave behind an empty log file."""
216+
log_dir = pytester.path / "logs"
217+
pytester.copy_example(
218+
name="src/execution_testing/cli/pytest_commands/pytest_ini_files/pytest-fill.ini"
219+
)
220+
221+
result = pytester.runpytest(
222+
"-c",
223+
"pytest-fill.ini",
224+
"--help",
225+
"--log-to",
226+
str(log_dir),
227+
)
228+
229+
assert result.ret == pytest.ExitCode.OK
230+
assert not list(log_dir.glob("*.log"))
231+
212232
def test_generate_pre_alloc_groups_preserves_chain_id_for_valid_from(
213233
self,
214234
pytester: Pytester,

packages/testing/src/execution_testing/logging/logger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ def configure_logging(
215215
log_path = Path(log_file)
216216
log_path.parent.mkdir(exist_ok=True, parents=True)
217217

218-
file_handler_instance = logging.FileHandler(log_path, mode="w")
218+
file_handler_instance = logging.FileHandler(
219+
log_path, mode="w", delay=True
220+
)
219221
file_handler_instance.setFormatter(UTCFormatter(fmt=log_format))
220222
root_logger.addHandler(file_handler_instance)
221223

packages/testing/src/execution_testing/logging/tests/test_logging.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,14 @@ def test_configure_logging_with_file(self) -> None:
169169
# Should return a file handler
170170
assert isinstance(handler, logging.FileHandler)
171171

172-
# Should create the log file
173-
assert log_file.exists()
172+
# Delay file creation until the first record is emitted.
173+
assert not log_file.exists()
174174

175175
# Log a message and check it appears in the file
176176
logger = get_logger("test_config")
177177
logger.info("Test log message")
178178

179+
assert log_file.exists()
179180
with open(log_file, "r") as f:
180181
log_content = f.read()
181182
assert "Test log message" in log_content
@@ -258,12 +259,15 @@ def getoption(self, name: str) -> Any:
258259
# Find the log file handler's file
259260
log_file = Path(file_handlers[0].baseFilename)
260261

261-
# Check that the log file was created
262-
assert log_file.exists()
263-
264262
# Verify the file is in the logs directory
265263
assert log_file.parent.resolve() == log_dir.resolve()
266264

265+
# Delay file creation until the first record is emitted.
266+
assert not log_file.exists()
267+
268+
get_logger("test_pytest_configure").info("Test log message")
269+
assert log_file.exists()
270+
267271
# Clean up the test log file
268272
log_file.unlink()
269273

0 commit comments

Comments
 (0)