diff --git a/changelog/7443.improvement.rst b/changelog/7443.improvement.rst new file mode 100644 index 00000000000..2e5d2c981c5 --- /dev/null +++ b/changelog/7443.improvement.rst @@ -0,0 +1 @@ +Detect if running on GitHub Actions and enable colors by default. diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 70bb2e2dcd6..1823d5c1e0d 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -27,11 +27,10 @@ def should_do_markup(file: TextIO) -> bool: return True if os.environ.get("PY_COLORS") == "0": return False + if os.environ.get("GITHUB_ACTIONS") == "true": + return True return ( - hasattr(file, "isatty") - and file.isatty() - and os.environ.get("TERM") != "dumb" - and not (sys.platform.startswith("java") and os._name == "nt") + hasattr(file, "isatty") and file.isatty() and os.environ.get("TERM") != "dumb" ) diff --git a/testing/io/test_terminalwriter.py b/testing/io/test_terminalwriter.py index 94cff307fcd..402e7e881d4 100644 --- a/testing/io/test_terminalwriter.py +++ b/testing/io/test_terminalwriter.py @@ -154,8 +154,7 @@ def test_attr_hasmarkup() -> None: assert "\x1b[0m" in s -def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: - monkeypatch.setitem(os.environ, "PY_COLORS", "1") +def assert_color_set(): file = io.StringIO() tw = terminalwriter.TerminalWriter(file) assert tw.hasmarkup @@ -166,8 +165,7 @@ def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: assert "\x1b[0m" in s -def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: - monkeypatch.setitem(os.environ, "PY_COLORS", "0") +def assert_color_not_set(): f = io.StringIO() f.isatty = lambda: True # type: ignore tw = terminalwriter.TerminalWriter(file=f) @@ -177,6 +175,21 @@ def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: assert s == "hello\n" +def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "PY_COLORS", "1") + assert_color_set() + + +def test_should_not_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "PY_COLORS", "0") + assert_color_not_set() + + +def test_should_do_markup_GITHUB_ACTIONS_eq_true(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "GITHUB_ACTIONS", "true") + assert_color_set() + + class TestTerminalWriterLineWidth: def test_init(self) -> None: tw = terminalwriter.TerminalWriter()