From 1f2bd20b0ecd8792548f163544897a8bd7cdf9df Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sun, 20 Nov 2022 23:42:22 +0000 Subject: [PATCH] Disable coloring with TERM=dumb or NO_COLOR Fixes: #1290 Related: https://no-color.org/ --- src/tox/config/cli/parser.py | 4 +++- tests/config/cli/test_parser.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/tox/config/cli/parser.py b/src/tox/config/cli/parser.py index 235e3a28b..de88b6134 100644 --- a/src/tox/config/cli/parser.py +++ b/src/tox/config/cli/parser.py @@ -290,6 +290,8 @@ def add_color_flags(parser: ArgumentParser) -> None: converter = StrConvert() if converter.to_bool(os.environ.get("NO_COLOR", "")): color = "no" + elif os.environ.get("TERM", "") == "dumb": + color = "no" elif converter.to_bool(os.environ.get("FORCE_COLOR", "")): color = "yes" else: @@ -299,7 +301,7 @@ def add_color_flags(parser: ArgumentParser) -> None: "--colored", default=color, choices=["yes", "no"], - help="should output be enriched with colors", + help="should output be enriched with colors, default is yes unless TERM=dumb or NO_COLOR is defined.", ) diff --git a/tests/config/cli/test_parser.py b/tests/config/cli/test_parser.py index 3b965d0d2..2be394def 100644 --- a/tests/config/cli/test_parser.py +++ b/tests/config/cli/test_parser.py @@ -31,6 +31,7 @@ def test_parser_const_with_default_none(monkeypatch: MonkeyPatch) -> None: @pytest.mark.parametrize("no_color", [None, "0", "1"]) @pytest.mark.parametrize("force_color", [None, "0", "1"]) @pytest.mark.parametrize("tox_color", [None, "bad", "no", "yes"]) +@pytest.mark.parametrize("term", [None, None, "xterm", "dumb"]) def test_parser_color( monkeypatch: MonkeyPatch, mocker: MockerFixture, @@ -38,8 +39,9 @@ def test_parser_color( force_color: str | None, tox_color: str | None, is_atty: bool, + term: str | None, ) -> None: - for key, value in {"NO_COLOR": no_color, "TOX_COLORED": tox_color, "FORCE_COLOR": force_color}.items(): + for key, value in {"NO_COLOR": no_color, "TOX_COLORED": tox_color, "FORCE_COLOR": force_color, "TERM": term}.items(): if value is None: monkeypatch.delenv(key, raising=False) else: @@ -47,7 +49,9 @@ def test_parser_color( stdout_mock = mocker.patch("tox.config.cli.parser.sys.stdout") stdout_mock.isatty.return_value = is_atty - if tox_color in ("yes", "no"): + if term == 'dumb': + expected = False + elif tox_color in ("yes", "no"): expected = tox_color == "yes" elif no_color == "1": expected = False