MCPcopy Create free account
hub / github.com/pytest-dev/pytest / color_mapping

Function color_mapping

testing/conftest.py:140–178  ·  view source on GitHub ↗

Returns a utility class which can replace keys in strings in the form "{NAME}" by their equivalent ASCII codes in the terminal. Used by tests which check the actual colors output by pytest.

()

Source from the content-addressed store, hash-verified

138
139@pytest.fixture(scope="session")
140def color_mapping():
141 """Returns a utility class which can replace keys in strings in the form "{NAME}"
142 by their equivalent ASCII codes in the terminal.
143
144 Used by tests which check the actual colors output by pytest.
145 """
146
147 class ColorMapping:
148 COLORS = {
149 "red": "\x1b[31m",
150 "green": "\x1b[32m",
151 "yellow": "\x1b[33m",
152 "bold": "\x1b[1m",
153 "reset": "\x1b[0m",
154 "kw": "\x1b[94m",
155 "hl-reset": "\x1b[39;49;00m",
156 "function": "\x1b[92m",
157 "number": "\x1b[94m",
158 "str": "\x1b[33m",
159 "print": "\x1b[96m",
160 }
161 RE_COLORS = {k: re.escape(v) for k, v in COLORS.items()}
162
163 @classmethod
164 def format(cls, lines: List[str]) -> List[str]:
165 """Straightforward replacement of color names to their ASCII codes."""
166 return [line.format(**cls.COLORS) for line in lines]
167
168 @classmethod
169 def format_for_fnmatch(cls, lines: List[str]) -> List[str]:
170 """Replace color names for use with LineMatcher.fnmatch_lines"""
171 return [line.format(**cls.COLORS).replace("[", "[[]") for line in lines]
172
173 @classmethod
174 def format_for_rematch(cls, lines: List[str]) -> List[str]:
175 """Replace color names for use with LineMatcher.re_match_lines"""
176 return [line.format(**cls.RE_COLORS) for line in lines]
177
178 return ColorMapping
179
180
181@pytest.fixture

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected