From 8e4d4835947736899684dcfad0886d7809bf9f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sat, 25 Nov 2023 11:58:58 +0100 Subject: [PATCH] Reset color-related envvars for testing Reset color-related environment variables in a fixture to prevent them from affecting test results. Otherwise, some of the tests fail e.g. if NO_COLOR is set in the calling environment. --- changelog/11638.trivial.rst | 1 + testing/conftest.py | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 changelog/11638.trivial.rst diff --git a/changelog/11638.trivial.rst b/changelog/11638.trivial.rst new file mode 100644 index 00000000000..374960b8925 --- /dev/null +++ b/changelog/11638.trivial.rst @@ -0,0 +1 @@ +Fixed the selftests to pass correctly if ``FORCE_COLOR``, ``NO_COLOR`` or ``PY_COLORS`` is set in the calling environment. diff --git a/testing/conftest.py b/testing/conftest.py index 6a50e810fbd..bcb05339b62 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -31,6 +31,17 @@ def set_column_width(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("COLUMNS", "80") +@pytest.fixture(autouse=True) +def reset_colors(monkeypatch: pytest.MonkeyPatch) -> None: + """ + Reset all color-related variables to prevent them from affecting internal pytest output + in tests that depend on it. + """ + monkeypatch.delenv("PY_COLORS", raising=False) + monkeypatch.delenv("NO_COLOR", raising=False) + monkeypatch.delenv("FORCE_COLOR", raising=False) + + @pytest.hookimpl(wrapper=True, tryfirst=True) def pytest_collection_modifyitems(items) -> Generator[None, None, None]: """Prefer faster tests.