Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for precision bit in LEVEL_NAME_FMT regex #8641

Merged
merged 4 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/8548.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce fix to handle precision width in ``log-cli-format`` in turn to fix output coloring for certain formats.
2 changes: 1 addition & 1 deletion src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ColoredLevelFormatter(logging.Formatter):
logging.DEBUG: {"purple"},
logging.NOTSET: set(),
}
LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*s)")
LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*(?:\.\d+)?s)")

def __init__(self, terminalwriter: TerminalWriter, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
Expand Down
31 changes: 31 additions & 0 deletions testing/logging/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ class option:
assert output == ("dummypath 10 INFO Test Message")


def test_coloredlogformatter_with_width_precision() -> None:
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.8s %(message)s"

record = logging.LogRecord(
name="dummy",
level=logging.INFO,
pathname="dummypath",
lineno=10,
msg="Test Message",
args=(),
exc_info=None,
)

class ColorConfig:
nicoddemus marked this conversation as resolved.
Show resolved Hide resolved
class option:
pass

tw = TerminalWriter()
tw.hasmarkup = True
formatter = ColoredLevelFormatter(tw, logfmt)
output = formatter.format(record)
assert output == (
"dummypath 10 \x1b[32mINFO \x1b[0m Test Message"
)

tw.hasmarkup = False
formatter = ColoredLevelFormatter(tw, logfmt)
output = formatter.format(record)
assert output == ("dummypath 10 INFO Test Message")


def test_multiline_message() -> None:
from _pytest.logging import PercentStyleMultiline

Expand Down