Skip to content

Commit fa5d581

Browse files
authored
Merge pull request #37 from wimglenn/structlog-settings-report
Allow displaying/hiding the structlog settings reporting
2 parents 4aa7642 + fa1ada1 commit fa5d581

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

.github/dependabot.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: monthly
7+
groups:
8+
actions-infrastructure:
9+
patterns:
10+
- actions/*

.github/workflows/tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
python-version:
19-
- "3.7"
2019
- "3.8"
2120
- "3.9"
2221
- "3.10"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,4 @@ You may only use "keep" or "evict" mode. It is an error to specify both.
150150
For complete control over which processors should be used in testing, the best way would be to add a `structlog.configure()` call directly in your `conftest.py` file and use `--structlog-explicit` (or set `structlog_explicit = true`) when running pytest to disable automatic processors selection entirely.
151151
152152
Using `pytest -v` or `pytest -vv` you can see more details about which processors `pytest-structlog` has included or excluded during the test startup.
153+
The reporting of pytest-structlog's own settings can also be explicitly enabled/disabled independently of verbosity level by specifying `--structlog-settings-report always/never` (cmdline) or `structlog_settings_report` (ini).

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pytest-structlog"
7-
version = "1.0"
7+
version = "1.1"
88
description = "Structured logging assertions"
99
readme = "README.md"
1010
classifiers = [
1111
"Framework :: Pytest",
1212
"Programming Language :: Python",
1313
"Programming Language :: Python :: 3",
1414
]
15-
requires-python = ">= 3.7"
15+
requires-python = ">= 3.8"
1616
dependencies = [
1717
"pytest",
1818
"structlog>=22.2.0",

pytest_structlog/__init__.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
from typing import Any
77
from typing import Callable
8-
from typing import cast
98
from typing import Generator
109
from typing import List
1110
from typing import NoReturn
@@ -49,7 +48,7 @@ def level_to_name(level: Union[str, int]) -> str:
4948
"""Given the name or number for a log-level, return the lower-case level name."""
5049
if isinstance(level, str):
5150
return level.lower()
52-
return cast(str, logging.getLevelName(level)).lower()
51+
return logging.getLevelName(level).lower()
5352

5453

5554
def is_submap(d1: EventDict, d2: EventDict) -> bool:
@@ -164,6 +163,7 @@ def __init__(self) -> None:
164163
"ProcessorFormatter.wrap_for_formatter",
165164
},
166165
}
166+
self.report: str = "auto"
167167

168168
def use_processor(self, name: str) -> tuple[bool, str]:
169169
"""Should processor be used during test, according to plugin configuration?"""
@@ -186,6 +186,7 @@ def reset(self) -> None:
186186
self.keep["cmdline-arg"].clear()
187187
self.evict["cmdline-arg"].clear()
188188
self.mode = "keep"
189+
self.report = "auto"
189190

190191

191192
settings: Settings = Settings()
@@ -268,12 +269,38 @@ def pytest_addoption(parser: pytest.Parser) -> None:
268269
help=explicit_setting_help,
269270
type="bool",
270271
)
272+
settings_report_help = (
273+
"Display the configured pytest-structlog settings after test collection."
274+
"Default (auto) will display the report when pytest is running with increased "
275+
"verbosity (-v)."
276+
)
277+
group.addoption(
278+
"--structlog-settings-report",
279+
help=settings_report_help,
280+
choices=["always", "never", "auto"],
281+
)
282+
parser.addini(
283+
name="structlog_settings_report",
284+
help=settings_report_help,
285+
type="string",
286+
default="auto",
287+
)
271288

272289

273290
def pytest_configure(config: pytest.Config) -> None:
274291
"""Perform initial plugin configuration."""
275292
user_keep = config.getoption("structlog_keep") or config.getini("structlog_keep")
276293
user_evict = config.getoption("structlog_evict") or config.getini("structlog_evict")
294+
settings_report = config.getoption("structlog_settings_report")
295+
if settings_report is None:
296+
settings_report = config.getini("structlog_settings_report")
297+
if settings_report not in ("always", "never", "auto"):
298+
raise pytest.UsageError(
299+
f"structlog_settings_report configuration value must be one of "
300+
f"'always', 'never', or 'auto' (got: {settings_report!r})"
301+
)
302+
settings.report = settings_report
303+
assert settings_report in ("always", "never", "auto"), settings_report
277304
if user_evict and user_keep:
278305
raise pytest.UsageError(
279306
"--structlog-keep and --structlog-evict settings are mutually "
@@ -299,8 +326,10 @@ def pytest_report_collectionfinish(config: pytest.Config) -> list[str]:
299326
"""Add post-collection information about which pre-configured structlog processors
300327
are being used. These only show if verbosity is non-zero, i.e. the user passed -v
301328
or -vv when running pytest."""
329+
if settings.report == "never":
330+
return []
302331
verbosity = config.getoption("verbose", default=0)
303-
if not verbosity:
332+
if settings.report == "auto" and not verbosity:
304333
return []
305334
tw = config.get_terminal_writer()
306335
lines = [" pytest-structlog settings ".center(tw.fullwidth, "=")]

0 commit comments

Comments
 (0)