From 888cf3fd76adf5a9bfabc04736b96f9be3f55dbc Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 7 Jan 2025 11:25:55 +0100 Subject: [PATCH 1/2] Fix selftests with Pygments >= 2.19.0 With Pygments 2.19, the Python lexer now emits Text.Whitespace (rather than Text) tokens after "def", which get highlighted as "bright black". See https://github.com/pygments/pygments/issues/1905 Fixes #13112 --- changelog/13112.contrib.rst | 1 + testing/conftest.py | 8 ++++++++ testing/test_terminal.py | 10 +++++----- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 changelog/13112.contrib.rst diff --git a/changelog/13112.contrib.rst b/changelog/13112.contrib.rst new file mode 100644 index 00000000000..5e59a736edb --- /dev/null +++ b/changelog/13112.contrib.rst @@ -0,0 +1 @@ +Fixed selftest failures in ``test_terminal.py`` with Pygments >= 2.19.0 diff --git a/testing/conftest.py b/testing/conftest.py index 110ad0d9b35..c9ffd33835b 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -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 @@ -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 = { @@ -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", diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 6fa04be28b1..32206364125 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -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}", "", @@ -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}", ] @@ -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}", @@ -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}", ] From ecf05486c8e72b7e49e547bf1124ddd02f7aacb3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:02:19 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/conftest.py b/testing/conftest.py index c9ffd33835b..45a47cbdbaa 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -2,12 +2,11 @@ from __future__ import annotations from collections.abc import Generator -import importlib.metadata import dataclasses +import importlib.metadata import re import sys -import pygments from packaging.version import Version from _pytest.monkeypatch import MonkeyPatch