Skip to content

Commit

Permalink
CI: Set PYTHON_COLORS=0 to disable coloured CPython tracebacks (#1740)
Browse files Browse the repository at this point in the history
test: prevent Python from using colors instead of removing color from its output

* Revert "test: ignore color in tracebacks"

This reverts commit 75b22f0.

* Set PYTHON_COLORS=0 to disable coloured CPython tracebacks

* Set PYTHON_COLORS=0 to disable coloured CPython tracebacks globally in tox.ini
  • Loading branch information
hugovk committed Jan 27, 2024
1 parent 375b992 commit c24db6d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 28 deletions.
5 changes: 0 additions & 5 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,3 @@ def __init__(self, options: Iterable[str]) -> None:
def get_output(self) -> str:
"""Get the output text from the `DebugControl`."""
return self.io.getvalue()


def without_color(text: str) -> str:
"""Remove ANSI color-setting escape sequences."""
return re.sub(r"\033\[[\d;]+m", "", text)
3 changes: 1 addition & 2 deletions tests/test_execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from coverage.files import python_reported_file

from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
from tests.helpers import without_color

TRY_EXECFILE = os.path.join(TESTS_DIR, "modules/process_test/try_execfile.py")

Expand Down Expand Up @@ -189,7 +188,7 @@ def excepthook(*args):
run_python_file(["excepthook_throw.py"])
# The _ExceptionDuringRun exception has the RuntimeError as its argument.
assert exc_info.value.args[1].args[0] == "Error Outside"
stderr = without_color(self.stderr())
stderr = self.stderr()
assert "in excepthook\n" in stderr
assert "Error in sys.excepthook:\n" in stderr
assert "RuntimeError: Error Inside" in stderr
Expand Down
11 changes: 3 additions & 8 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from tests import testenv
from tests.coveragetest import CoverageTest, TESTS_DIR
from tests.helpers import re_line, re_lines, re_lines_text, without_color
from tests.helpers import re_line, re_lines, re_lines_text


class ProcessTest(CoverageTest):
Expand Down Expand Up @@ -318,7 +318,6 @@ def f2():
assert out == out2

# But also make sure that the output is what we expect.
out = without_color(out)
path = python_reported_file('throw.py')
msg = f'File "{re.escape(path)}", line 8, in f2'
assert re.search(msg, out)
Expand Down Expand Up @@ -970,11 +969,8 @@ def excepthook(*args):
py_st, py_out = self.run_command_status("python excepthook_throw.py")
assert cov_st == py_st
assert cov_st == 1
assert "in excepthook" in without_color(py_out)
# Don't know why: the Python output shows "Error in sys.excepthook" and
# "Original exception" in color. The coverage output has the first in
# color and "Original" without color? Strip all the color.
assert without_color(cov_out) == without_color(py_out)
assert "in excepthook" in py_out
assert cov_out == py_out


class AliasedCommandTest(CoverageTest):
Expand Down Expand Up @@ -1181,7 +1177,6 @@ def test_removing_directory(self) -> None:
def test_removing_directory_with_error(self) -> None:
self.make_file("bug806.py", self.BUG_806)
out = self.run_command("coverage run bug806.py")
out = without_color(out)
path = python_reported_file('bug806.py')
# Python 3.11 adds an extra line to the traceback.
# Check that the lines we expect are there.
Expand Down
14 changes: 1 addition & 13 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from tests.helpers import (
CheckUniqueFilenames, FailingProxy,
arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal, assert_coverage_warnings,
re_lines, re_lines_text, re_line, without_color,
re_lines, re_lines_text, re_line,
)


Expand Down Expand Up @@ -472,15 +472,3 @@ def subtract(self, a, b): # type: ignore[no-untyped-def]
proxy.add(3, 4)
# then add starts working
assert proxy.add(5, 6) == 11


@pytest.mark.parametrize("text, result", [
("", ""),
("Nothing to see here", "Nothing to see here"),
("Oh no! \x1b[1;35mRuntimeError\x1b[0m. Fix it.", "Oh no! RuntimeError. Fix it."),
("Fancy: \x1b[48;5;95mBkgd\x1b[38;2;100;200;25mRGB\x1b[0m", "Fancy: BkgdRGB"),
# Other escape sequences are unaffected.
("X\x1b[2J\x1b[1mBold\x1b[22m\x1b[=3hZ", "X\x1b[2JBold\x1b[=3hZ"),
])
def test_without_color(text: str, result: str) -> None:
assert without_color(text) == result
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ setenv =
PYTHONPYCACHEPREFIX=
# If we ever need a stronger way to suppress warnings:
#PYTHONWARNINGS=ignore:removed in Python 3.14; use ast.Constant:DeprecationWarning
# Disable CPython's color output
PYTHON_COLORS=0

# $set_env.py: COVERAGE_PIP_ARGS - Extra arguments for `pip install`
# `--no-build-isolation` will let tox work with no network.
Expand Down

0 comments on commit c24db6d

Please sign in to comment.