Skip to content

Commit 75b22f0

Browse files
committed
test: ignore color in tracebacks
Python 3.13 adds color escape sequences to tracebacks. GitHub Actions sets FORCE_COLOR=1, so they appear in color there, but not locally. We don't care about that difference, so strip the escape sequences.
1 parent b7c41a2 commit 75b22f0

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

tests/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,8 @@ def __init__(self, options: Iterable[str]) -> None:
397397
def get_output(self) -> str:
398398
"""Get the output text from the `DebugControl`."""
399399
return self.io.getvalue()
400+
401+
402+
def without_color(text: str) -> str:
403+
"""Remove ANSI color-setting escape sequences."""
404+
return re.sub(r"\033\[[\d;]+m", "", text)

tests/test_execfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from coverage.files import python_reported_file
2424

2525
from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
26+
from tests.helpers import without_color
2627

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

@@ -188,7 +189,7 @@ def excepthook(*args):
188189
run_python_file(["excepthook_throw.py"])
189190
# The _ExceptionDuringRun exception has the RuntimeError as its argument.
190191
assert exc_info.value.args[1].args[0] == "Error Outside"
191-
stderr = self.stderr()
192+
stderr = without_color(self.stderr())
192193
assert "in excepthook\n" in stderr
193194
assert "Error in sys.excepthook:\n" in stderr
194195
assert "RuntimeError: Error Inside" in stderr

tests/test_process.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from tests import testenv
2929
from tests.coveragetest import CoverageTest, TESTS_DIR
30-
from tests.helpers import re_line, re_lines, re_lines_text
30+
from tests.helpers import re_line, re_lines, re_lines_text, without_color
3131

3232

3333
class ProcessTest(CoverageTest):
@@ -318,6 +318,7 @@ def f2():
318318
assert out == out2
319319

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

975979

976980
class AliasedCommandTest(CoverageTest):
@@ -1177,6 +1181,7 @@ def test_removing_directory(self) -> None:
11771181
def test_removing_directory_with_error(self) -> None:
11781182
self.make_file("bug806.py", self.BUG_806)
11791183
out = self.run_command("coverage run bug806.py")
1184+
out = without_color(out)
11801185
path = python_reported_file('bug806.py')
11811186
# Python 3.11 adds an extra line to the traceback.
11821187
# Check that the lines we expect are there.

tests/test_testing.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from tests.helpers import (
2525
CheckUniqueFilenames, FailingProxy,
2626
arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal, assert_coverage_warnings,
27-
re_lines, re_lines_text, re_line,
27+
re_lines, re_lines_text, re_line, without_color,
2828
)
2929

3030

@@ -472,3 +472,15 @@ def subtract(self, a, b): # type: ignore[no-untyped-def]
472472
proxy.add(3, 4)
473473
# then add starts working
474474
assert proxy.add(5, 6) == 11
475+
476+
477+
@pytest.mark.parametrize("text, result", [
478+
("", ""),
479+
("Nothing to see here", "Nothing to see here"),
480+
("Oh no! \x1b[1;35mRuntimeError\x1b[0m. Fix it.", "Oh no! RuntimeError. Fix it."),
481+
("Fancy: \x1b[48;5;95mBkgd\x1b[38;2;100;200;25mRGB\x1b[0m", "Fancy: BkgdRGB"),
482+
# Other escape sequences are unaffected.
483+
("X\x1b[2J\x1b[1mBold\x1b[22m\x1b[=3hZ", "X\x1b[2JBold\x1b[=3hZ"),
484+
])
485+
def test_without_color(text: str, result: str) -> None:
486+
assert without_color(text) == result

0 commit comments

Comments
 (0)