From 03230b4002b0cf88a00b4a1fc6f15f77d99cda7e Mon Sep 17 00:00:00 2001 From: gdhameeja Date: Thu, 4 Jun 2020 01:38:11 +0530 Subject: [PATCH] Fix-6906: Added code-highlight option to disable highlighting optionally Co-authored-by: Ran Benita --- changelog/6906.feature.rst | 1 + src/_pytest/_io/terminalwriter.py | 3 ++- src/_pytest/config/__init__.py | 7 ++++++- src/_pytest/terminal.py | 6 ++++++ testing/io/test_terminalwriter.py | 21 +++++++++++++++++---- 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 changelog/6906.feature.rst diff --git a/changelog/6906.feature.rst b/changelog/6906.feature.rst new file mode 100644 index 000000000..3e1fe3ef1 --- /dev/null +++ b/changelog/6906.feature.rst @@ -0,0 +1 @@ +Added `--code-highlight` command line option to enable/disable code highlighting in terminal output. diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index a285cf4fc..70bb2e2dc 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -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 diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 717743e79..bf5b78082 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -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 diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 6a58260e9..9dbd477ea 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -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", diff --git a/testing/io/test_terminalwriter.py b/testing/io/test_terminalwriter.py index 0e9cdb64d..94cff307f 100644 --- a/testing/io/test_terminalwriter.py +++ b/testing/io/test_terminalwriter.py @@ -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(