Skip to content

Commit

Permalink
test: ignore color in tracebacks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nedbat committed Jan 25, 2024
1 parent b7c41a2 commit 75b22f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions tests/helpers.py
Expand Up @@ -397,3 +397,8 @@ 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: 2 additions & 1 deletion tests/test_execfile.py
Expand Up @@ -23,6 +23,7 @@
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 @@ -188,7 +189,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 = self.stderr()
stderr = without_color(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: 8 additions & 3 deletions tests/test_process.py
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
from tests.helpers import re_line, re_lines, re_lines_text, without_color


class ProcessTest(CoverageTest):
Expand Down Expand Up @@ -318,6 +318,7 @@ 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 @@ -969,8 +970,11 @@ 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 py_out
assert cov_out == py_out
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)


class AliasedCommandTest(CoverageTest):
Expand Down Expand Up @@ -1177,6 +1181,7 @@ 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: 13 additions & 1 deletion tests/test_testing.py
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,
re_lines, re_lines_text, re_line, without_color,
)


Expand Down Expand Up @@ -472,3 +472,15 @@ 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

0 comments on commit 75b22f0

Please sign in to comment.