Skip to content

Commit

Permalink
Fix CLI fixture persisting structlog configuration (#53)
Browse files Browse the repository at this point in the history
* Add fixture to reset structlog configuration after using the CLI fixture

* Remove xfail that is no longer necessary
  • Loading branch information
SRv6d authored Feb 28, 2024
1 parent d858618 commit 4be226d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 18 additions & 2 deletions tests/cli/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
from collections.abc import Callable
from collections.abc import Callable, Iterator

import pytest
import structlog
from anycastd._cli.main import app
from click.testing import Result
from typer.testing import CliRunner


@pytest.fixture
def anycastd_cli() -> Callable[..., Result]:
def reset_structlog_config() -> Iterator[None]:
"""Reset structlog configuration after running a test.
This is required for tests that invoke the anycastd CLI, as running it
initializes the structlog configuration, adding filters that could affect
other tests.
"""
yield
structlog.reset_defaults()


@pytest.fixture
def anycastd_cli(reset_structlog_config) -> Callable[..., Result]:
"""A callable that runs anycastd CLI commands.
To make sure that this fixture does not affect other tests, state that is
initialized when running the CLI needs to be reset after using this fixture.
Example:
```python
>>> result = anycastd_cli("run", "--help")
Expand Down
3 changes: 0 additions & 3 deletions tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ def test_log_format_configures_structlog(self, anycastd_cli, arg: str, processor
assert isinstance(last_processor, processor)


@pytest.mark.xfail(
reason="Above tests configure a persistent log level filter.", strict=False
)
def test_reading_configuration_is_logged(mocker):
"""Reading the configuration is logged."""
path = Path("/path/to/config.toml")
Expand Down

0 comments on commit 4be226d

Please sign in to comment.