From 4b6188b3b15cc8b257518ce8d5d73321a3c993a6 Mon Sep 17 00:00:00 2001 From: Rahul Kumaresan Date: Fri, 7 May 2021 00:45:52 +0530 Subject: [PATCH 1/4] add support for precision bit in LEVEL_NAME_FMT regex --- src/_pytest/logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index e0d71c7eb..642000831 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -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) From 1e3fcece6d2523f4671ff312c9b108333850d294 Mon Sep 17 00:00:00 2001 From: Rahul Kumaresan Date: Fri, 7 May 2021 16:16:40 +0530 Subject: [PATCH 2/4] enhance support for precision bit in LEVELNAME_FMT_REGEX regex --- changelog/8548.bugfix.rst | 1 + src/_pytest/logging.py | 2 +- testing/logging/test_formatter.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 changelog/8548.bugfix.rst diff --git a/changelog/8548.bugfix.rst b/changelog/8548.bugfix.rst new file mode 100644 index 000000000..9201169fc --- /dev/null +++ b/changelog/8548.bugfix.rst @@ -0,0 +1 @@ +Introduce fix to handle precision width in ``log-cli-format`` in turn to fix output coloring for certain formats. diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 642000831..da1de013d 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -59,7 +59,7 @@ class ColoredLevelFormatter(logging.Formatter): logging.DEBUG: {"purple"}, logging.NOTSET: set(), } - LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*[.]?\d*s)") + LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*(?:\.\d+)?s)") def __init__(self, terminalwriter: TerminalWriter, *args, **kwargs) -> None: super().__init__(*args, **kwargs) diff --git a/testing/logging/test_formatter.py b/testing/logging/test_formatter.py index 335166caa..ccc7bfbeb 100644 --- a/testing/logging/test_formatter.py +++ b/testing/logging/test_formatter.py @@ -36,6 +36,37 @@ def test_coloredlogformatter() -> None: assert output == ("dummypath 10 INFO Test Message") +def test_coloredlogformatter_with_width_precision() -> None: + logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.2s %(message)s" + + record = logging.LogRecord( + name="dummy", + level=logging.INFO, + pathname="dummypath", + lineno=10, + msg="Test Message", + args=(), + exc_info=None, + ) + + class ColorConfig: + 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 From 80acc0ed6f4f1cfaad2de2679089d8376583f6b0 Mon Sep 17 00:00:00 2001 From: Rahul Kumaresan Date: Fri, 7 May 2021 16:25:19 +0530 Subject: [PATCH 3/4] fix test_coloredlogformatter_with_width_precision test --- testing/logging/test_formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/logging/test_formatter.py b/testing/logging/test_formatter.py index ccc7bfbeb..78596f049 100644 --- a/testing/logging/test_formatter.py +++ b/testing/logging/test_formatter.py @@ -37,7 +37,7 @@ def test_coloredlogformatter() -> None: def test_coloredlogformatter_with_width_precision() -> None: - logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.2s %(message)s" + logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.8s %(message)s" record = logging.LogRecord( name="dummy", From 9e11d645b1cadec995ccee902b9763abf49b32bf Mon Sep 17 00:00:00 2001 From: Rahul Kumaresan Date: Fri, 7 May 2021 17:56:35 +0530 Subject: [PATCH 4/4] cleanup tests by removal of unused code elements --- testing/logging/test_formatter.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/testing/logging/test_formatter.py b/testing/logging/test_formatter.py index 78596f049..379712937 100644 --- a/testing/logging/test_formatter.py +++ b/testing/logging/test_formatter.py @@ -18,10 +18,6 @@ def test_coloredlogformatter() -> None: exc_info=None, ) - class ColorConfig: - class option: - pass - tw = TerminalWriter() tw.hasmarkup = True formatter = ColoredLevelFormatter(tw, logfmt) @@ -49,10 +45,6 @@ def test_coloredlogformatter_with_width_precision() -> None: exc_info=None, ) - class ColorConfig: - class option: - pass - tw = TerminalWriter() tw.hasmarkup = True formatter = ColoredLevelFormatter(tw, logfmt)