Skip to content

Commit 2d5a1af

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

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

changelog/6906.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `--code-highlight` command line option to enable/disable code highlighting in terminal output.

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 = config.option.code_highlight == "yes" and tw.hasmarkup
12631264
if config.option.color == "no":
12641265
tw.hasmarkup = False
12651266
return tw

src/_pytest/terminal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ def pytest_addoption(parser):
164164
choices=["yes", "no", "auto"],
165165
help="color terminal output (yes/no/auto).",
166166
)
167+
group._addoption(
168+
"--code-highlight",
169+
default="yes",
170+
choices=["yes", "no"],
171+
help="Whether code should be highlighted on the terminal or not",
172+
)
167173

168174
parser.addini(
169175
"console_output_style",

testing/io/test_terminalwriter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,19 @@ 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(
222+
False,
223+
"\x1b[94massert\x1b[39;49;00m \x1b[94m0\x1b[39;49;00m\n",
224+
id="no markup",
225+
),
222226
],
223227
)
224228
def test_code_highlight(has_markup, expected, color_mapping):
225229
f = io.StringIO()
226230
tw = terminalwriter.TerminalWriter(f)
227231
tw.hasmarkup = has_markup
228232
tw._write_source(["assert 0"])
233+
229234
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])
230235

231236
with pytest.raises(

0 commit comments

Comments
 (0)