terminalwriter: fix crash trying to highlight empty source

For quick checking I don't know how we can reach here with an empty
source, so test just checks the function directly.

Fix #11758.
This commit is contained in:
Ran Benita
2024-01-02 19:31:37 +02:00
parent a53984a55b
commit cb5a42c836
3 changed files with 18 additions and 1 deletions

View File

@@ -200,8 +200,9 @@ class TerminalWriter:
"""Highlight the given source if we have markup support."""
from _pytest.config.exceptions import UsageError
if not self.hasmarkup or not self.code_highlight:
if not source or not self.hasmarkup or not self.code_highlight:
return source
try:
from pygments.formatters.terminal import TerminalFormatter