Skip to content

Commit a928ad7

Browse files
committed
fix(tests): test broken by moving logs
1 parent 9563a51 commit a928ad7

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

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

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -211,66 +211,66 @@ def test_pytest_configure(self, monkeypatch: pytest.MonkeyPatch) -> None:
211211
)
212212

213213
# Create logs directory if it doesn't exist
214-
log_dir = Path("logs")
215-
if not log_dir.exists():
216-
log_dir.mkdir()
217-
218-
# Save the original handlers to restore later
219-
original_handlers = logging.getLogger().handlers.copy()
220-
221-
try:
222-
# Remove existing handlers to start clean
223-
for handler in logging.getLogger().handlers[:]:
224-
logging.getLogger().removeHandler(handler)
225-
226-
# Create a mock pytest config
227-
class MockConfig:
228-
def __init__(self) -> None:
229-
self.option = MagicMock()
230-
self.option.eest_log_level = logging.INFO
231-
self.workerinput: dict[str, Any] = {}
232-
233-
def getoption(self, name: str) -> Any:
234-
if name == "eest_log_level":
235-
return logging.INFO
236-
237-
# Set up environment
238-
monkeypatch.setattr("sys.argv", ["pytest"])
239-
monkeypatch.setenv("PYTEST_XDIST_WORKER", "worker1")
240-
241-
# Call pytest_configure
242-
config = MockConfig()
243-
pytest_configure(config) # type: ignore[arg-type]
244-
245-
# Check that logging is configured
246-
assert hasattr(config.option, "eest_log_file_path")
247-
248-
# Check that a file handler was added to the root logger
249-
file_handlers = [
250-
h
251-
for h in logging.getLogger().handlers
252-
if isinstance(h, logging.FileHandler)
253-
]
254-
assert len(file_handlers) > 0
255-
256-
# Find the log file handler's file
257-
log_file = Path(file_handlers[0].baseFilename)
258-
259-
# Check that the log file was created
260-
assert log_file.exists()
261-
262-
# Verify the file is in the logs directory
263-
assert log_file.parent.resolve() == log_dir.resolve()
264-
265-
# Clean up the test log file
266-
log_file.unlink()
267-
268-
finally:
269-
# Clean up: Remove any handlers we added
270-
for handler in logging.getLogger().handlers[:]:
271-
handler.close()
272-
logging.getLogger().removeHandler(handler)
273-
274-
# Restore original handlers
275-
for handler in original_handlers:
276-
logging.getLogger().addHandler(handler)
214+
with tempfile.TemporaryDirectory() as temp_dir:
215+
log_dir = Path(temp_dir)
216+
217+
# Save the original handlers to restore later
218+
original_handlers = logging.getLogger().handlers.copy()
219+
220+
try:
221+
# Remove existing handlers to start clean
222+
for handler in logging.getLogger().handlers[:]:
223+
logging.getLogger().removeHandler(handler)
224+
225+
# Create a mock pytest config
226+
class MockConfig:
227+
def __init__(self) -> None:
228+
self.option = MagicMock()
229+
self.option.eest_log_level = logging.INFO
230+
self.option.eest_log_dir = temp_dir
231+
self.workerinput: dict[str, Any] = {}
232+
233+
def getoption(self, name: str) -> Any:
234+
if name == "eest_log_level":
235+
return logging.INFO
236+
237+
# Set up environment
238+
monkeypatch.setattr("sys.argv", ["pytest"])
239+
monkeypatch.setenv("PYTEST_XDIST_WORKER", "worker1")
240+
241+
# Call pytest_configure
242+
config = MockConfig()
243+
pytest_configure(config) # type: ignore[arg-type]
244+
245+
# Check that logging is configured
246+
assert hasattr(config.option, "eest_log_file_path")
247+
248+
# Check that a file handler was added to the root logger
249+
file_handlers = [
250+
h
251+
for h in logging.getLogger().handlers
252+
if isinstance(h, logging.FileHandler)
253+
]
254+
assert len(file_handlers) > 0
255+
256+
# Find the log file handler's file
257+
log_file = Path(file_handlers[0].baseFilename)
258+
259+
# Check that the log file was created
260+
assert log_file.exists()
261+
262+
# Verify the file is in the logs directory
263+
assert log_file.parent.resolve() == log_dir.resolve()
264+
265+
# Clean up the test log file
266+
log_file.unlink()
267+
268+
finally:
269+
# Clean up: Remove any handlers we added
270+
for handler in logging.getLogger().handlers[:]:
271+
handler.close()
272+
logging.getLogger().removeHandler(handler)
273+
274+
# Restore original handlers
275+
for handler in original_handlers:
276+
logging.getLogger().addHandler(handler)

0 commit comments

Comments
 (0)