Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 63 additions & 63 deletions packages/testing/src/execution_testing/logging/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,66 +211,66 @@ def test_pytest_configure(self, monkeypatch: pytest.MonkeyPatch) -> None:
)

# Create logs directory if it doesn't exist
log_dir = Path("logs")
if not log_dir.exists():
log_dir.mkdir()

# Save the original handlers to restore later
original_handlers = logging.getLogger().handlers.copy()

try:
# Remove existing handlers to start clean
for handler in logging.getLogger().handlers[:]:
logging.getLogger().removeHandler(handler)

# Create a mock pytest config
class MockConfig:
def __init__(self) -> None:
self.option = MagicMock()
self.option.eest_log_level = logging.INFO
self.workerinput: dict[str, Any] = {}

def getoption(self, name: str) -> Any:
if name == "eest_log_level":
return logging.INFO

# Set up environment
monkeypatch.setattr("sys.argv", ["pytest"])
monkeypatch.setenv("PYTEST_XDIST_WORKER", "worker1")

# Call pytest_configure
config = MockConfig()
pytest_configure(config) # type: ignore[arg-type]

# Check that logging is configured
assert hasattr(config.option, "eest_log_file_path")

# Check that a file handler was added to the root logger
file_handlers = [
h
for h in logging.getLogger().handlers
if isinstance(h, logging.FileHandler)
]
assert len(file_handlers) > 0

# Find the log file handler's file
log_file = Path(file_handlers[0].baseFilename)

# Check that the log file was created
assert log_file.exists()

# Verify the file is in the logs directory
assert log_file.parent.resolve() == log_dir.resolve()

# Clean up the test log file
log_file.unlink()

finally:
# Clean up: Remove any handlers we added
for handler in logging.getLogger().handlers[:]:
handler.close()
logging.getLogger().removeHandler(handler)

# Restore original handlers
for handler in original_handlers:
logging.getLogger().addHandler(handler)
with tempfile.TemporaryDirectory() as temp_dir:
log_dir = Path(temp_dir)

# Save the original handlers to restore later
original_handlers = logging.getLogger().handlers.copy()

try:
# Remove existing handlers to start clean
for handler in logging.getLogger().handlers[:]:
logging.getLogger().removeHandler(handler)

# Create a mock pytest config
class MockConfig:
def __init__(self) -> None:
self.option = MagicMock()
self.option.eest_log_level = logging.INFO
self.option.eest_log_dir = temp_dir
self.workerinput: dict[str, Any] = {}

def getoption(self, name: str) -> Any:
if name == "eest_log_level":
return logging.INFO

# Set up environment
monkeypatch.setattr("sys.argv", ["pytest"])
monkeypatch.setenv("PYTEST_XDIST_WORKER", "worker1")

# Call pytest_configure
config = MockConfig()
pytest_configure(config) # type: ignore[arg-type]

# Check that logging is configured
assert hasattr(config.option, "eest_log_file_path")

# Check that a file handler was added to the root logger
file_handlers = [
h
for h in logging.getLogger().handlers
if isinstance(h, logging.FileHandler)
]
assert len(file_handlers) > 0

# Find the log file handler's file
log_file = Path(file_handlers[0].baseFilename)

# Check that the log file was created
assert log_file.exists()

# Verify the file is in the logs directory
assert log_file.parent.resolve() == log_dir.resolve()

# Clean up the test log file
log_file.unlink()

finally:
# Clean up: Remove any handlers we added
for handler in logging.getLogger().handlers[:]:
handler.close()
logging.getLogger().removeHandler(handler)

# Restore original handlers
for handler in original_handlers:
logging.getLogger().addHandler(handler)
Loading