Skip to content

Commit 03230b4

Browse files
gdhameejabluetech
authored andcommitted
Fix-6906: Added code-highlight option to disable highlighting optionally
Co-authored-by: Ran Benita <[email protected]>
1 parent d9546ff commit 03230b4

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
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/_io/terminalwriter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ 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+
self.code_highlight = True
7778

7879
@property
7980
def fullwidth(self) -> int:
@@ -180,7 +181,7 @@ def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> No
180181

181182
def _highlight(self, source: str) -> str:
182183
"""Highlight the given source code if we have markup support."""
183-
if not self.hasmarkup:
184+
if not self.hasmarkup or not self.code_highlight:
184185
return source
185186
try:
186187
from pygments.formatters.terminal import TerminalFormatter

src/_pytest/config/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,8 +1389,13 @@ def create_terminal_writer(
13891389
tw = TerminalWriter(file=file)
13901390
if config.option.color == "yes":
13911391
tw.hasmarkup = True
1392-
if config.option.color == "no":
1392+
elif config.option.color == "no":
13931393
tw.hasmarkup = False
1394+
1395+
if config.option.code_highlight == "yes":
1396+
tw.code_highlight = True
1397+
elif config.option.code_highlight == "no":
1398+
tw.code_highlight = False
13941399
return tw
13951400

13961401

src/_pytest/terminal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ def pytest_addoption(parser: Parser) -> None:
208208
choices=["yes", "no", "auto"],
209209
help="color terminal output (yes/no/auto).",
210210
)
211+
group._addoption(
212+
"--code-highlight",
213+
default="yes",
214+
choices=["yes", "no"],
215+
help="Whether code should be highlighted (only if --color is also enabled)",
216+
)
211217

212218
parser.addini(
213219
"console_output_style",

testing/io/test_terminalwriter.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,32 @@ def test_combining(self) -> None:
213213

214214

215215
@pytest.mark.parametrize(
216-
"has_markup, expected",
216+
("has_markup", "code_highlight", "expected"),
217217
[
218218
pytest.param(
219-
True, "{kw}assert{hl-reset} {number}0{hl-reset}\n", id="with markup"
219+
True,
220+
True,
221+
"{kw}assert{hl-reset} {number}0{hl-reset}\n",
222+
id="with markup and code_highlight",
223+
),
224+
pytest.param(
225+
True, False, "assert 0\n", id="with markup but no code_highlight",
226+
),
227+
pytest.param(
228+
False, True, "assert 0\n", id="without markup but with code_highlight",
229+
),
230+
pytest.param(
231+
False, False, "assert 0\n", id="neither markup nor code_highlight",
220232
),
221-
pytest.param(False, "assert 0\n", id="no markup"),
222233
],
223234
)
224-
def test_code_highlight(has_markup, expected, color_mapping):
235+
def test_code_highlight(has_markup, code_highlight, expected, color_mapping):
225236
f = io.StringIO()
226237
tw = terminalwriter.TerminalWriter(f)
227238
tw.hasmarkup = has_markup
239+
tw.code_highlight = code_highlight
228240
tw._write_source(["assert 0"])
241+
229242
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])
230243

231244
with pytest.raises(

0 commit comments

Comments
 (0)