-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use code highlighting if pygments is installed (#6658)
* Use code highlighting if pygments is installed * Use colorama constants instead of bare ascii codes Could not find the exact equivalent of 'hl-reset' code using colorama constants though. * Refactor ASCII color handling into a fixture * Revert back to using explicit color codes * In Python 3.5 skip rest of tests that require ordered markup in colored output
- Loading branch information
1 parent
3b58285
commit 4209ad6
Showing
7 changed files
with
252 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Code is now highlighted in tracebacks when ``pygments`` is installed. | ||
|
||
Users are encouraged to install ``pygments`` into their environment and provide feedback, because | ||
the plan is to make ``pygments`` a regular dependency in the future. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,39 @@ | ||
# Reexport TerminalWriter from here instead of py, to make it easier to | ||
# extend or swap our own implementation in the future. | ||
from py.io import TerminalWriter as TerminalWriter # noqa: F401 | ||
from typing import List | ||
from typing import Sequence | ||
|
||
from py.io import TerminalWriter as BaseTerminalWriter # noqa: F401 | ||
|
||
|
||
class TerminalWriter(BaseTerminalWriter): | ||
def _write_source(self, lines: List[str], indents: Sequence[str] = ()) -> None: | ||
"""Write lines of source code possibly highlighted. | ||
Keeping this private for now because the API is clunky. We should discuss how | ||
to evolve the terminal writer so we can have more precise color support, for example | ||
being able to write part of a line in one color and the rest in another, and so on. | ||
""" | ||
if indents and len(indents) != len(lines): | ||
raise ValueError( | ||
"indents size ({}) should have same size as lines ({})".format( | ||
len(indents), len(lines) | ||
) | ||
) | ||
if not indents: | ||
indents = [""] * len(lines) | ||
source = "\n".join(lines) | ||
new_lines = self._highlight(source).splitlines() | ||
for indent, new_line in zip(indents, new_lines): | ||
self.line(indent + new_line) | ||
|
||
def _highlight(self, source): | ||
"""Highlight the given source code according to the "code_highlight" option""" | ||
if not self.hasmarkup: | ||
return source | ||
try: | ||
from pygments.formatters.terminal import TerminalFormatter | ||
from pygments.lexers.python import PythonLexer | ||
from pygments import highlight | ||
except ImportError: | ||
return source | ||
else: | ||
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import re | ||
from io import StringIO | ||
|
||
import pytest | ||
from _pytest._io import TerminalWriter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"has_markup, expected", | ||
[ | ||
pytest.param( | ||
True, "{kw}assert{hl-reset} {number}0{hl-reset}\n", id="with markup" | ||
), | ||
pytest.param(False, "assert 0\n", id="no markup"), | ||
], | ||
) | ||
def test_code_highlight(has_markup, expected, color_mapping): | ||
f = StringIO() | ||
tw = TerminalWriter(f) | ||
tw.hasmarkup = has_markup | ||
tw._write_source(["assert 0"]) | ||
assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected]) | ||
|
||
with pytest.raises( | ||
ValueError, | ||
match=re.escape("indents size (2) should have same size as lines (1)"), | ||
): | ||
tw._write_source(["assert 0"], [" ", " "]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.