Merge pull request #6934 from gdhameeja/Fix-6906
Fix-6906: Added code-highlight option to disable highlighting optionally
This commit is contained in:
commit
61014c5f94
|
@ -0,0 +1 @@
|
|||
Added `--code-highlight` command line option to enable/disable code highlighting in terminal output.
|
|
@ -74,6 +74,7 @@ class TerminalWriter:
|
|||
self.hasmarkup = should_do_markup(file)
|
||||
self._current_line = ""
|
||||
self._terminal_width = None # type: Optional[int]
|
||||
self.code_highlight = True
|
||||
|
||||
@property
|
||||
def fullwidth(self) -> int:
|
||||
|
@ -180,7 +181,7 @@ class TerminalWriter:
|
|||
|
||||
def _highlight(self, source: str) -> str:
|
||||
"""Highlight the given source code if we have markup support."""
|
||||
if not self.hasmarkup:
|
||||
if not self.hasmarkup or not self.code_highlight:
|
||||
return source
|
||||
try:
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
|
|
|
@ -1389,8 +1389,13 @@ def create_terminal_writer(
|
|||
tw = TerminalWriter(file=file)
|
||||
if config.option.color == "yes":
|
||||
tw.hasmarkup = True
|
||||
if config.option.color == "no":
|
||||
elif config.option.color == "no":
|
||||
tw.hasmarkup = False
|
||||
|
||||
if config.option.code_highlight == "yes":
|
||||
tw.code_highlight = True
|
||||
elif config.option.code_highlight == "no":
|
||||
tw.code_highlight = False
|
||||
return tw
|
||||
|
||||
|
||||
|
|
|
@ -208,6 +208,12 @@ def pytest_addoption(parser: Parser) -> None:
|
|||
choices=["yes", "no", "auto"],
|
||||
help="color terminal output (yes/no/auto).",
|
||||
)
|
||||
group._addoption(
|
||||
"--code-highlight",
|
||||
default="yes",
|
||||
choices=["yes", "no"],
|
||||
help="Whether code should be highlighted (only if --color is also enabled)",
|
||||
)
|
||||
|
||||
parser.addini(
|
||||
"console_output_style",
|
||||
|
|
|
@ -213,19 +213,32 @@ class TestTerminalWriterLineWidth:
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"has_markup, expected",
|
||||
("has_markup", "code_highlight", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
True, "{kw}assert{hl-reset} {number}0{hl-reset}\n", id="with markup"
|
||||
True,
|
||||
True,
|
||||
"{kw}assert{hl-reset} {number}0{hl-reset}\n",
|
||||
id="with markup and code_highlight",
|
||||
),
|
||||
pytest.param(
|
||||
True, False, "assert 0\n", id="with markup but no code_highlight",
|
||||
),
|
||||
pytest.param(
|
||||
False, True, "assert 0\n", id="without markup but with code_highlight",
|
||||
),
|
||||
pytest.param(
|
||||
False, False, "assert 0\n", id="neither markup nor code_highlight",
|
||||
),
|
||||
pytest.param(False, "assert 0\n", id="no markup"),
|
||||
],
|
||||
)
|
||||
def test_code_highlight(has_markup, expected, color_mapping):
|
||||
def test_code_highlight(has_markup, code_highlight, expected, color_mapping):
|
||||
f = io.StringIO()
|
||||
tw = terminalwriter.TerminalWriter(f)
|
||||
tw.hasmarkup = has_markup
|
||||
tw.code_highlight = code_highlight
|
||||
tw._write_source(["assert 0"])
|
||||
|
||||
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])
|
||||
|
||||
with pytest.raises(
|
||||
|
|
Loading…
Reference in New Issue