Skip to content

Commit

Permalink
Move root logger fixture out to utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
TheByronHimes authored and TheByronHimes committed Dec 18, 2023
1 parent a4a42cb commit 5194b2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
19 changes: 19 additions & 0 deletions tests/fixtures/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

"""Utils for fixture handling"""

import logging
import os
from pathlib import Path

import pytest
import yaml

TEST_FILE_DIR = Path(__file__).parent.resolve() / "test_files"
Expand All @@ -33,3 +35,20 @@ def read_yaml(path: Path) -> dict:
"""Read yaml file and return content as dict."""
with open(path, encoding="UTF-8") as file:
return yaml.safe_load(file)


@pytest.fixture
def root_logger_reset():
"""Reset root logger level and handlers after modification."""
root = logging.getLogger()
original_level = root.level
root_handlers = root.handlers.copy()

yield

# reset level and remove RecordCompiler handler
root.setLevel(original_level)

for handler in root.handlers:
if handler not in root_handlers:
root.addHandler(handler)
20 changes: 2 additions & 18 deletions tests/unit/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
RecordCompiler,
configure_logging,
)
from tests.fixtures.utils import root_logger_reset # noqa: F401

VALID_SERVICE_NAME = "test"
VALID_INSTANCE_ID = "001"
Expand Down Expand Up @@ -225,24 +226,7 @@ def test_reconfiguration_of_existing_loggers():
assert isinstance(log.handlers[0].formatter, logging.Formatter)


@pytest.fixture
def root_logger_reset():
"""Reset root logger level and handlers after modification."""
root = logging.getLogger()
original_level = root.level
root_handlers = root.handlers.copy()

yield

# reset level and remove RecordCompiler handler
root.setLevel(original_level)

for handler in root.handlers:
if handler not in root_handlers:
root.addHandler(handler)


def test_root_logger_configuration(root_logger_reset):
def test_root_logger_configuration(root_logger_reset): # noqa: F811
"""Test that `configure_logging` configures the root logger by default.
In case of failure, the fixture should prevent leaving root logger in modified state.
Expand Down

0 comments on commit 5194b2f

Please sign in to comment.