8000 gh-128595: Add test class helper to force no terminal colour by hugovk · Pull Request #128687 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-128595: Add test class helper to force no terminal colour #128687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 13, 2025
Merged
Prev Previous commit
Refactor
  • Loading branch information
hugovk committed Jan 10, 2025
commit 0554e911e3078dbb389f20f52059bf74e79163fd
43 changes: 18 additions & 25 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2833,23 +2833,27 @@ def is_slot_wrapper(name, value):
yield name, True


@contextlib.contextmanager
def no_color():
import _colorize
from .os_helper import EnvironmentVarGuard

with (
swap_attr(_colorize, "can_colorize", lambda: False),
EnvironmentVarGuard() as env,
):
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
env.unset(var)
env.set("NO_COLOR", "1")
yield


def force_not_colorized(func):
"""Force the terminal not to be colorized."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
import _colorize
from .os_helper import EnvironmentVarGuard

with (
swap_attr(_colorize, "can_colorize", lambda: False),
EnvironmentVarGuard() as env,
):
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
env.unset(var)
env.set("NO_COLOR", "1")

with no_color():
return func(*args, **kwargs)

return wrapper


Expand All @@ -2860,19 +2864,8 @@ def force_not_colorized_test_class(cls):
@classmethod
@functools.wraps(cls.setUpClass)
def new_setUpClass(cls):
import _colorize
from .os_helper import Env 6F0D ironmentVarGuard

cls.enterClassContext(
swap_attr(_colorize, "can_colorize", lambda: False)
)
env = cls.enterClassContext(EnvironmentVarGuard())
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
env.unset(var)
env.set("NO_COLOR", "1")

if original_setUpClass:
original_setUpClass()
cls.enterClassContext(no_color())
original_setUpClass()

cls.setUpClass = new_setUpClass
return cls
Expand Down
Loading
0