-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support
once
parameter in logging methods, allowing to log a …
…message only once with a given logger This will be useful when issuing warning messages in templates, for example when deprecating things, as we don't want to show the message dozens of time (each time the template is used), but rather just once.
- Loading branch information
Showing
2 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Tests for the loggers module.""" | ||
|
||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from mkdocstrings.loggers import get_logger, get_template_logger | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
{}, | ||
{"once": False}, | ||
{"once": True}, | ||
], | ||
) | ||
def test_logger(kwargs: dict, caplog: pytest.LogCaptureFixture) -> None: | ||
"""Test logger methods. | ||
Parameters: | ||
kwargs: Keyword arguments passed to the logger methods. | ||
""" | ||
logger = get_logger("mkdocstrings.test") | ||
caplog.set_level(0) | ||
for _ in range(2): | ||
logger.debug("Debug message", **kwargs) | ||
logger.info("Info message", **kwargs) | ||
logger.warning("Warning message", **kwargs) | ||
logger.error("Error message", **kwargs) | ||
logger.critical("Critical message", **kwargs) | ||
if kwargs.get("once", False): | ||
assert len(caplog.records) == 5 | ||
else: | ||
assert len(caplog.records) == 10 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
{}, | ||
{"once": False}, | ||
{"once": True}, | ||
], | ||
) | ||
def test_template_logger(kwargs: dict, caplog: pytest.LogCaptureFixture) -> None: | ||
"""Test template logger methods. | ||
Parameters: | ||
kwargs: Keyword arguments passed to the template logger methods. | ||
""" | ||
logger = get_template_logger() | ||
mock = MagicMock() | ||
caplog.set_level(0) | ||
for _ in range(2): | ||
logger.debug(mock, "Debug message", **kwargs) | ||
logger.info(mock, "Info message", **kwargs) | ||
logger.warning(mock, "Warning message", **kwargs) | ||
logger.error(mock, "Error message", **kwargs) | ||
logger.critical(mock, "Critical message", **kwargs) | ||
if kwargs.get("once", False): | ||
assert len(caplog.records) == 5 | ||
else: | ||
assert len(caplog.records) == 10 |