| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from _pytest._io import TerminalWriter |
| 7 |
from _pytest.logging import ColoredLevelFormatter |
| 8 |
|
| 9 |
|
| 10 |
def test_coloredlogformatter() -> None: |
| 11 |
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s" |
| 12 |
|
| 13 |
record = logging.LogRecord( |
| 14 |
name="dummy", |
| 15 |
level=logging.INFO, |
| 16 |
pathname="dummypath", |
| 17 |
lineno=10, |
| 18 |
msg="Test Message", |
| 19 |
args=(), |
| 20 |
exc_info=None, |
| 21 |
) |
| 22 |
|
| 23 |
tw = TerminalWriter() |
| 24 |
tw.hasmarkup = True |
| 25 |
formatter = ColoredLevelFormatter(tw, logfmt) |
| 26 |
output = formatter.format(record) |
| 27 |
assert output == ( |
| 28 |
"dummypath 10 \x1b[32mINFO \x1b[0m Test Message" |
| 29 |
) |
| 30 |
|
| 31 |
tw.hasmarkup = False |
| 32 |
formatter = ColoredLevelFormatter(tw, logfmt) |
| 33 |
output = formatter.format(record) |
| 34 |
assert output == ("dummypath 10 INFO Test Message") |
| 35 |
|
| 36 |
|
| 37 |
def test_coloredlogformatter_with_width_precision() -> None: |
| 38 |
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.8s %(message)s" |
| 39 |
|
| 40 |
record = logging.LogRecord( |
| 41 |
name="dummy", |
| 42 |
level=logging.INFO, |
| 43 |
pathname="dummypath", |
| 44 |
lineno=10, |
| 45 |
msg="Test Message", |
| 46 |
args=(), |
| 47 |
exc_info=None, |
| 48 |
) |
| 49 |
|
| 50 |
tw = TerminalWriter() |
| 51 |
tw.hasmarkup = True |
| 52 |
formatter = ColoredLevelFormatter(tw, logfmt) |
| 53 |
output = formatter.format(record) |
| 54 |
assert output == ( |
| 55 |
"dummypath 10 \x1b[32mINFO \x1b[0m Test Message" |
| 56 |
) |
| 57 |
|
| 58 |
tw.hasmarkup = False |
| 59 |
formatter = ColoredLevelFormatter(tw, logfmt) |
| 60 |
output = formatter.format(record) |
| 61 |
assert output == ("dummypath 10 INFO Test Message") |
| 62 |
|
| 63 |
|
| 64 |
def test_multiline_message() -> None: |
| 65 |
from _pytest.logging import PercentStyleMultiline |
| 66 |
|
| 67 |
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s" |
| 68 |
|
| 69 |
record: Any = logging.LogRecord( |
| 70 |
name="dummy", |
| 71 |
level=logging.INFO, |
| 72 |
pathname="dummypath", |
| 73 |
lineno=10, |
| 74 |
msg="Test Message line1\nline2", |
| 75 |
args=(), |
| 76 |
exc_info=None, |
| 77 |
) |
| 78 |
# this is called by logging.Formatter.format |
| 79 |
record.message = record.getMessage() |
| 80 |
|
| 81 |
ai_on_style = PercentStyleMultiline(logfmt, True) |
| 82 |
output = ai_on_style.format(record) |
| 83 |
assert output == ( |
| 84 |
"dummypath 10 INFO Test Message line1\n" |
| 85 |
" line2" |
| 86 |
) |
| 87 |
|
| 88 |
ai_off_style = PercentStyleMultiline(logfmt, False) |
| 89 |
output = ai_off_style.format(record) |
| 90 |
assert output == ( |
| 91 |
"dummypath 10 INFO Test Message line1\nline2" |
| 92 |
) |
| 93 |
|
| 94 |
ai_none_style = PercentStyleMultiline(logfmt, None) |
| 95 |
output = ai_none_style.format(record) |
| 96 |
assert output == ( |
| 97 |
"dummypath 10 INFO Test Message line1\nline2" |
| 98 |
) |
| 99 |
|
| 100 |
record.auto_indent = False |
| 101 |
output = ai_on_style.format(record) |
| 102 |
assert output == ( |
| 103 |
"dummypath 10 INFO Test Message line1\nline2" |
| 104 |
) |
| 105 |
|
| 106 |
record.auto_indent = True |
| 107 |
output = ai_off_style.format(record) |
| 108 |
assert output == ( |
| 109 |
"dummypath 10 INFO Test Message line1\n" |
| 110 |
" line2" |
| 111 |
) |
| 112 |
|
| 113 |
record.auto_indent = "False" |
| 114 |
output = ai_on_style.format(record) |
| 115 |
assert output == ( |
| 116 |
"dummypath 10 INFO Test Message line1\nline2" |
| 117 |
) |
| 118 |
|
| 119 |
record.auto_indent = "True" |
| 120 |
output = ai_off_style.format(record) |
| 121 |
assert output == ( |
| 122 |
"dummypath 10 INFO Test Message line1\n" |
| 123 |
" line2" |
| 124 |
) |
| 125 |
|
| 126 |
# bad string values default to False |
| 127 |
record.auto_indent = "junk" |
| 128 |
output = ai_off_style.format(record) |
| 129 |
assert output == ( |
| 130 |
"dummypath 10 INFO Test Message line1\nline2" |
| 131 |
) |
| 132 |
|
| 133 |
# anything other than string or int will default to False |
| 134 |
record.auto_indent = dict() |
| 135 |
output = ai_off_style.format(record) |
| 136 |
assert output == ( |
| 137 |
"dummypath 10 INFO Test Message line1\nline2" |
| 138 |
) |
| 139 |
|
| 140 |
record.auto_indent = "5" |
| 141 |
output = ai_off_style.format(record) |
| 142 |
assert output == ( |
| 143 |
"dummypath 10 INFO Test Message line1\n line2" |
| 144 |
) |
| 145 |
|
| 146 |
record.auto_indent = 5 |
| 147 |
output = ai_off_style.format(record) |
| 148 |
assert output == ( |
| 149 |
"dummypath 10 INFO Test Message line1\n line2" |
| 150 |
) |
| 151 |
|
| 152 |
|
| 153 |
def test_colored_short_level() -> None: |
| 154 |
logfmt = "%(levelname).1s %(message)s" |
| 155 |
|
| 156 |
record = logging.LogRecord( |
| 157 |
name="dummy", |
| 158 |
level=logging.INFO, |
| 159 |
pathname="dummypath", |
| 160 |
lineno=10, |
| 161 |
msg="Test Message", |
| 162 |
args=(), |
| 163 |
exc_info=None, |
| 164 |
) |
| 165 |
|
| 166 |
class ColorConfig: |
| 167 |
class option: |
| 168 |
pass |
| 169 |
|
| 170 |
tw = TerminalWriter() |
| 171 |
tw.hasmarkup = True |
| 172 |
formatter = ColoredLevelFormatter(tw, logfmt) |
| 173 |
output = formatter.format(record) |
| 174 |
# the I (of INFO) is colored |
| 175 |
assert output == ("\x1b[32mI\x1b[0m Test Message") |