From 49d233a0086a7cb642f6c95d51d30a1f168027fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sat, 25 Nov 2023 07:36:59 +0100 Subject: [PATCH 1/3] Fix tests when NO_COLOR is set in the calling environment Add `FORCE_COLOR` and `NO_COLOR` variables to the `isolate()` fixture to ensure that these two variables do not affect internal test output. This fixes the following two test failures when pytest is called with `NO_COLOR` set: ``` FAILED tests/unit/test_exceptions.py::TestDiagnosticPipErrorPresentation_ASCII::test_complete_color - AssertionError: assert '\x1b[1merror...ing harder.\n' == '\x1b[1;31mer...ing harder.\n' FAILED tests/unit/test_exceptions.py::TestDiagnosticPipErrorPresentation_Unicode::test_complete_color - AssertionError: assert '\x1b[1merror...ing harder.\n' == '\x1b[1;31mer...ing harder.\n' ``` --- news/7ae28a10-04c4-4a1f-a276-4c9e04f2e0c1.trivial.rst | 0 tests/conftest.py | 4 ++++ 2 files changed, 4 insertions(+) create mode 100644 news/7ae28a10-04c4-4a1f-a276-4c9e04f2e0c1.trivial.rst diff --git a/news/7ae28a10-04c4-4a1f-a276-4c9e04f2e0c1.trivial.rst b/news/7ae28a10-04c4-4a1f-a276-4c9e04f2e0c1.trivial.rst new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/conftest.py b/tests/conftest.py index c5bf4bb9567..8d9eb029c79 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -318,6 +318,10 @@ def isolate(tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -> None: # Make sure tests don't share a requirements tracker. monkeypatch.delenv("PIP_BUILD_TRACKER", False) + # Make sure color control variables don't affect internal output. + monkeypatch.delenv("FORCE_COLOR", False) + monkeypatch.delenv("NO_COLOR", False) + # FIXME: Windows... os.makedirs(os.path.join(home_dir, ".config", "git")) with open(os.path.join(home_dir, ".config", "git", "config"), "wb") as fp: From f84d782f8d823571212238a24eefd0a53f5690c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sun, 4 Feb 2024 16:49:54 +0100 Subject: [PATCH 2/3] Revert "Stop forcing color in CI" This reverts commit 1b33f4b944b46f9d8bd6380201ef1e6c33fd944a. Now the test suite strips `FORCE_COLOR` internally, so it should be fine to run with colors enabled for nox. --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 70a09b6a88f..2071f400b10 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,11 @@ on: schedule: - cron: 0 0 * * MON # Run every Monday at 00:00 UTC +env: + # The "FORCE_COLOR" variable, when set to 1, + # tells Nox to colorize itself. + FORCE_COLOR: "1" + concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} cancel-in-progress: true From 1836651d3402dcbe81f52517248952816d778f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Mon, 5 Feb 2024 14:13:18 +0100 Subject: [PATCH 3/3] Lower the scope of `simple_script` fixture to apply `isolate` to it Lower the scope of `simple_script` fixture used in `test_inspect` from `session` to (implicit) `function`, to make it match the `isolate` fixture and therefore make the fixture run in isolation. Per the documentation [1], pytest initializes higher-scope fixtures first, and lower-scope fixtures cannot be used in them. Since within the same scope, autouse fixtures are always loaded before regular fixtures, no further adjustment is necessary. This should have no ill side effects, as the fixture is used by a single test only. [1] https://docs.pytest.org/en/stable/reference/fixtures.html#fixture-instantiation-order --- tests/functional/test_inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/test_inspect.py b/tests/functional/test_inspect.py index f6690fb1fb1..fc3aab5d495 100644 --- a/tests/functional/test_inspect.py +++ b/tests/functional/test_inspect.py @@ -5,7 +5,7 @@ from tests.lib import PipTestEnvironment, ScriptFactory, TestData -@pytest.fixture(scope="session") +@pytest.fixture def simple_script( tmpdir_factory: pytest.TempPathFactory, script_factory: ScriptFactory,