From 9667a32159048c138685ceb393b10c88bd37f78d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 11:15:57 +0300 Subject: [PATCH 1/5] Enable colours on GitHub Actions --- src/_pytest/_io/terminalwriter.py | 2 +- testing/io/test_terminalwriter.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 70bb2e2dcd6..275f1116262 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -32,7 +32,7 @@ def should_do_markup(file: TextIO) -> bool: and file.isatty() and os.environ.get("TERM") != "dumb" and not (sys.platform.startswith("java") and os._name == "nt") - ) + ) or os.environ.get("GITHUB_ACTIONS") == "true" class TerminalWriter: diff --git a/testing/io/test_terminalwriter.py b/testing/io/test_terminalwriter.py index 94cff307fcd..46bf8363bc9 100644 --- a/testing/io/test_terminalwriter.py +++ b/testing/io/test_terminalwriter.py @@ -177,6 +177,18 @@ def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: assert s == "hello\n" +def test_should_do_markup_GITHUB_ACTIONS_eq_true(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "GITHUB_ACTIONS", "true") + file = io.StringIO() + tw = terminalwriter.TerminalWriter(file) + assert tw.hasmarkup + tw.line("hello", bold=True) + s = file.getvalue() + assert len(s) > len("hello\n") + assert "\x1b[1m" in s + assert "\x1b[0m" in s + + class TestTerminalWriterLineWidth: def test_init(self) -> None: tw = terminalwriter.TerminalWriter() From cc9cbff8fed4e7639c1da6c9f1e326e929dff134 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 11:52:12 +0300 Subject: [PATCH 2/5] Add changelog --- changelog/7443.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/7443.improvement.rst 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. From 36eaf42189cc256b6a8c38e7df329a100b1d8d09 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 22:15:59 +0300 Subject: [PATCH 3/5] Remove redundant check; Jython is no longer supported --- src/_pytest/_io/terminalwriter.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 275f1116262..ff39197dfba 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -28,10 +28,7 @@ def should_do_markup(file: TextIO) -> bool: if os.environ.get("PY_COLORS") == "0": return False 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" ) or os.environ.get("GITHUB_ACTIONS") == "true" From 62b6581eefb986f34609828f98bdebfc7bed7bd4 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 22:17:13 +0300 Subject: [PATCH 4/5] Refactor: handle special cases before 'ordinary' one --- src/_pytest/_io/terminalwriter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index ff39197dfba..1823d5c1e0d 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -27,9 +27,11 @@ 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" - ) or os.environ.get("GITHUB_ACTIONS") == "true" + ) class TerminalWriter: From 6f9d6a38d3c02d5b92b949da4671831bd1826e0e Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 22:56:38 +0300 Subject: [PATCH 5/5] Refactor to remove duplication --- testing/io/test_terminalwriter.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/testing/io/test_terminalwriter.py b/testing/io/test_terminalwriter.py index 46bf8363bc9..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,16 +175,19 @@ 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") - file = io.StringIO() - tw = terminalwriter.TerminalWriter(file) - assert tw.hasmarkup - tw.line("hello", bold=True) - s = file.getvalue() - assert len(s) > len("hello\n") - assert "\x1b[1m" in s - assert "\x1b[0m" in s + assert_color_set() class TestTerminalWriterLineWidth: