Skip to content

Commit

Permalink
Remove PYTHON* env vars to isolate colour tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 21, 2025
1 parent 37e0bbd commit 4be7293
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
11 changes: 11 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"without_optimizer",
"force_not_colorized",
"force_not_colorized_test_class",
"make_clean_env",
"BrokenIter",
"in_systemd_nspawn_sync_suppressed",
"run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
Expand Down Expand Up @@ -2871,6 +2872,16 @@ def new_setUpClass(cls):
return cls


def make_clean_env() -> dict[str, str]:
clean_env = os.environ.copy()
for k in clean_env.copy():
if k.startswith("PYTHON"):
clean_env.pop(k)
clean_env.pop("FORCE_COLOR", None)
clean_env.pop("NO_COLOR", None)
return clean_env


def initialized_with_pyrepl():
"""Detect whether PyREPL was used during Python initialization."""
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.
Expand Down
10 changes: 9 additions & 1 deletion Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
import unittest.mock
import _colorize
from test.support import force_not_colorized
from test.support import force_not_colorized, make_clean_env

ORIGINAL_CAN_COLORIZE = _colorize.can_colorize

Expand All @@ -17,6 +17,14 @@ def tearDownModule():


class TestColorizeFunction(unittest.TestCase):
def setUp(self):
# Remove PYTHON* environment variables to isolate from local user
# settings and simulate running with `-E`. Such variables should be
# added to test methods later to patched os.environ.
patcher = unittest.mock.patch("os.environ", new=make_clean_env())
self.addCleanup(patcher.stop)
patcher.start()

@force_not_colorized
def test_colorized_detection_checks_for_environment_variables(self):
flags = unittest.mock.MagicMock(ignore_environment=False)
Expand Down
10 changes: 0 additions & 10 deletions Lib/test/test_pyrepl/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ def handle_all_events(
)


def make_clean_env() -> dict[str, str]:
clean_env = os.environ.copy()
for k in clean_env.copy():
if k.startswith("PYTHON"):
clean_env.pop(k)
clean_env.pop("FORCE_COLOR", None)
clean_env.pop("NO_COLOR", None)
return clean_env


class FakeConsole(Console):
def __init__(self, events, encoding="utf-8") -> None:
self.events = iter(events)
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import tempfile
from unittest import TestCase, skipUnless, skipIf
from unittest.mock import patch
from test.support import force_not_colorized
from test.support import force_not_colorized, make_clean_env
from test.support import SHORT_TIMEOUT
from test.support.import_helper import import_module
from test.support.os_helper import unlink
Expand All @@ -23,7 +23,6 @@
multiline_input,
code_to_events,
clean_screen,
make_clean_env,
)
from _pyrepl.console import Event
from _pyrepl.readline import (ReadlineAlikeReader, ReadlineConfig,
Expand Down

0 comments on commit 4be7293

Please sign in to comment.