Skip to content

Commit

Permalink
Fix selftests with Pygments >= 2.19.0
Browse files Browse the repository at this point in the history
With Pygments 2.19, the Python lexer now emits
Text.Whitespace (rather than Text) tokens after "def",
which get highlighted as "bright black".

See pygments/pygments#1905
Fixes #13112
  • Loading branch information
The-Compiler committed Jan 8, 2025
1 parent 5d58b1f commit 888cf3f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/13112.contrib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed selftest failures in ``test_terminal.py`` with Pygments >= 2.19.0
8 changes: 8 additions & 0 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from __future__ import annotations

from collections.abc import Generator
import importlib.metadata
import dataclasses
import re
import sys

import pygments
from packaging.version import Version

from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import Pytester
import pytest
Expand Down Expand Up @@ -168,6 +172,9 @@ def color_mapping():
Used by tests which check the actual colors output by pytest.
"""
# https://github.com/pygments/pygments/commit/d24e272894a56a98b1b718d9ac5fabc20124882a
pygments_version = Version(importlib.metadata.version("pygments"))
pygments_has_kwspace_hl = pygments_version >= Version("2.19")

class ColorMapping:
COLORS = {
Expand All @@ -180,6 +187,7 @@ class ColorMapping:
"bold": "\x1b[1m",
"reset": "\x1b[0m",
"kw": "\x1b[94m",
"kwspace": "\x1b[90m \x1b[39;49;00m" if pygments_has_kwspace_hl else " ",
"hl-reset": "\x1b[39;49;00m",
"function": "\x1b[92m",
"number": "\x1b[94m",
Expand Down
10 changes: 5 additions & 5 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,13 +1294,13 @@ def test_this():
"=*= FAILURES =*=",
"{red}{bold}_*_ test_this _*_{reset}",
"",
" {reset}{kw}def{hl-reset} {function}test_this{hl-reset}():{endline}",
" {reset}{kw}def{hl-reset}{kwspace}{function}test_this{hl-reset}():{endline}",
"> fail(){endline}",
"",
"{bold}{red}test_color_yes.py{reset}:5: ",
"_ _ * _ _*",
"",
" {reset}{kw}def{hl-reset} {function}fail{hl-reset}():{endline}",
" {reset}{kw}def{hl-reset}{kwspace}{function}fail{hl-reset}():{endline}",
"> {kw}assert{hl-reset} {number}0{hl-reset}{endline}",
"{bold}{red}E assert 0{reset}",
"",
Expand Down Expand Up @@ -2580,7 +2580,7 @@ def test_foo():
result.stdout.fnmatch_lines(
color_mapping.format_for_fnmatch(
[
" {reset}{kw}def{hl-reset} {function}test_foo{hl-reset}():{endline}",
" {reset}{kw}def{hl-reset}{kwspace}{function}test_foo{hl-reset}():{endline}",
"> {kw}assert{hl-reset} {number}1{hl-reset} == {number}10{hl-reset}{endline}",
"{bold}{red}E assert 1 == 10{reset}",
]
Expand All @@ -2602,7 +2602,7 @@ def test_foo():
result.stdout.fnmatch_lines(
color_mapping.format_for_fnmatch(
[
" {reset}{kw}def{hl-reset} {function}test_foo{hl-reset}():{endline}",
" {reset}{kw}def{hl-reset}{kwspace}{function}test_foo{hl-reset}():{endline}",
" {print}print{hl-reset}({str}'''{hl-reset}{str}{hl-reset}",
"> {str} {hl-reset}{str}'''{hl-reset}); {kw}assert{hl-reset} {number}0{hl-reset}{endline}",
"{bold}{red}E assert 0{reset}",
Expand All @@ -2625,7 +2625,7 @@ def test_foo():
result.stdout.fnmatch_lines(
color_mapping.format_for_fnmatch(
[
" {reset}{kw}def{hl-reset} {function}test_foo{hl-reset}():{endline}",
" {reset}{kw}def{hl-reset}{kwspace}{function}test_foo{hl-reset}():{endline}",
"> {kw}assert{hl-reset} {number}1{hl-reset} == {number}10{hl-reset}{endline}",
"{bold}{red}E assert 1 == 10{reset}",
]
Expand Down

0 comments on commit 888cf3f

Please sign in to comment.