Skip to content

Commit ce758ce

Browse files
gdhameejagdhameeja
authored andcommitted
Fix-6906: Added code-highlight option to disable highlighting optionally
code-highlight has default value to True, this value is used in `TerminalWriter` in order to control highlighting of the code.
1 parent 194b521 commit ce758ce

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

src/_pytest/_io/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ def _highlight(self, source):
3636
except ImportError:
3737
return source
3838
else:
39-
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))
39+
if self.code_highlight:
40+
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))
41+
return source

src/_pytest/config/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,11 @@ def create_terminal_writer(config: Config, *args, **kwargs) -> TerminalWriter:
12151215
tw.hasmarkup = True
12161216
if config.option.color == "no":
12171217
tw.hasmarkup = False
1218+
1219+
if config.option.code_highlight == "no":
1220+
tw.code_highlight = False
1221+
else:
1222+
tw.code_highlight = True
12181223
return tw
12191224

12201225

src/_pytest/terminal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ def pytest_addoption(parser):
161161
choices=["yes", "no", "auto"],
162162
help="color terminal output (yes/no/auto).",
163163
)
164+
group._addoption(
165+
"--code-highlight",
166+
action="store",
167+
dest="code_highlight",
168+
default="yes",
169+
choices=["yes", "no"],
170+
help="Whether code should be highlighted on the terminal or not",
171+
)
164172

165173
parser.addini(
166174
"console_output_style",

testing/code/test_terminal_writer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_code_highlight(has_markup, expected, color_mapping):
1818
f = StringIO()
1919
tw = TerminalWriter(f)
2020
tw.hasmarkup = has_markup
21+
tw.code_highlight = True
2122
tw._write_source(["assert 0"])
2223
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])
2324

testing/test_terminal.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,3 +2138,24 @@ def test_foo():
21382138
]
21392139
)
21402140
)
2141+
2142+
def test_code_highlight_false(self, testdir: Testdir, color_mapping) -> None:
2143+
testdir.makepyfile(
2144+
"""
2145+
def test_foo():
2146+
print('''
2147+
'''); assert 0
2148+
"""
2149+
)
2150+
result = testdir.runpytest("--code-highlight=no")
2151+
color_mapping.requires_ordered_markup(result)
2152+
result.stdout.fnmatch_lines(
2153+
color_mapping.format_for_fnmatch(
2154+
[
2155+
" {kw}def{hl-reset} {function}test_foo{hl-reset}():",
2156+
" {print}print{hl-reset}({str}'''{hl-reset}{str}{hl-reset}",
2157+
"> {str} {hl-reset}{str}'''{hl-reset}); {kw}assert{hl-reset} {number}0{hl-reset}",
2158+
"assert 0{reset}",
2159+
]
2160+
)
2161+
)

0 commit comments

Comments
 (0)