Skip to content

Commit dafe4f5

Browse files
author
gdhameeja
committed
Fix-6906: Added code-highlight option to disable highlighting optionally
1 parent e755317 commit dafe4f5

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/_pytest/_code/code.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ class ReprTraceback(TerminalRepr):
984984
entrysep = "_ "
985985

986986
def toterminal(self, tw: TerminalWriter) -> None:
987+
tw.code_highlight = False
987988
# the entries might have different styles
988989
for i, entry in enumerate(self.reprentries):
989990
if entry.style == "long":

src/_pytest/_io/terminalwriter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def __init__(self, file: Optional[TextIO] = None) -> None:
7474
self.hasmarkup = should_do_markup(file)
7575
self._current_line = ""
7676
self._terminal_width = None # type: Optional[int]
77+
# default to true
78+
self.code_highlight = True
7779

7880
@property
7981
def fullwidth(self) -> int:
@@ -180,7 +182,7 @@ def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> No
180182

181183
def _highlight(self, source: str) -> str:
182184
"""Highlight the given source code if we have markup support."""
183-
if not self.hasmarkup:
185+
if not self.hasmarkup and not self.code_highlight:
184186
return source
185187
try:
186188
from pygments.formatters.terminal import TerminalFormatter

src/_pytest/config/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,7 @@ def create_terminal_writer(config: Config, *args, **kwargs) -> TerminalWriter:
12601260
tw = TerminalWriter(*args, **kwargs)
12611261
if config.option.color == "yes":
12621262
tw.hasmarkup = True
1263+
tw.code_highlight = False if config.option.code_highlight == "yes" else True
12631264
if config.option.color == "no":
12641265
tw.hasmarkup = False
12651266
return tw

src/_pytest/terminal.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,15 @@ def pytest_addoption(parser):
164164
choices=["yes", "no", "auto"],
165165
help="color terminal output (yes/no/auto).",
166166
)
167-
167+
group._addoption(
168+
"--code-highlight",
169+
action="store",
170+
dest="code_highlight",
171+
default="yes",
172+
choices=["yes", "no"],
173+
help="Whether code should be highlighted on the terminal or not"
174+
)
175+
168176
parser.addini(
169177
"console_output_style",
170178
help='console output: "classic", or with additional progress information ("progress" (percentage) | "count").',

testing/io/test_terminalwriter.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,13 @@ def test_combining(self) -> None:
218218
pytest.param(
219219
True, "{kw}assert{hl-reset} {number}0{hl-reset}\n", id="with markup"
220220
),
221-
pytest.param(False, "assert 0\n", id="no markup"),
221+
pytest.param(False, "\x1b[94massert\x1b[39;49;00m \x1b[94m0\x1b[39;49;00m\n", id="no markup"),
222222
],
223223
)
224224
def test_code_highlight(has_markup, expected, color_mapping):
225225
f = io.StringIO()
226226
tw = terminalwriter.TerminalWriter(f)
227227
tw.hasmarkup = has_markup
228228
tw._write_source(["assert 0"])
229-
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])
230229

231-
with pytest.raises(
232-
ValueError,
233-
match=re.escape("indents size (2) should have same size as lines (1)"),
234-
):
235-
tw._write_source(["assert 0"], [" ", " "])
230+
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])

0 commit comments

Comments
 (0)