Skip to content

Commit 1f2bd20

Browse files
committed
Disable coloring with TERM=dumb or NO_COLOR
Fixes: #1290 Related: https://no-color.org/
1 parent f99073c commit 1f2bd20

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/tox/config/cli/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ def add_color_flags(parser: ArgumentParser) -> None:
290290
converter = StrConvert()
291291
if converter.to_bool(os.environ.get("NO_COLOR", "")):
292292
color = "no"
293+
elif os.environ.get("TERM", "") == "dumb":
294+
color = "no"
293295
elif converter.to_bool(os.environ.get("FORCE_COLOR", "")):
294296
color = "yes"
295297
else:
@@ -299,7 +301,7 @@ def add_color_flags(parser: ArgumentParser) -> None:
299301
"--colored",
300302
default=color,
301303
choices=["yes", "no"],
302-
help="should output be enriched with colors",
304+
help="should output be enriched with colors, default is yes unless TERM=dumb or NO_COLOR is defined.",
303305
)
304306

305307

tests/config/cli/test_parser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,27 @@ def test_parser_const_with_default_none(monkeypatch: MonkeyPatch) -> None:
3131
@pytest.mark.parametrize("no_color", [None, "0", "1"])
3232
@pytest.mark.parametrize("force_color", [None, "0", "1"])
3333
@pytest.mark.parametrize("tox_color", [None, "bad", "no", "yes"])
34+
@pytest.mark.parametrize("term", [None, None, "xterm", "dumb"])
3435
def test_parser_color(
3536
monkeypatch: MonkeyPatch,
3637
mocker: MockerFixture,
3738
no_color: str | None,
3839
force_color: str | None,
3940
tox_color: str | None,
4041
is_atty: bool,
42+
term: str | None,
4143
) -> None:
42-
for key, value in {"NO_COLOR": no_color, "TOX_COLORED": tox_color, "FORCE_COLOR": force_color}.items():
44+
for key, value in {"NO_COLOR": no_color, "TOX_COLORED": tox_color, "FORCE_COLOR": force_color, "TERM": term}.items():
4345
if value is None:
4446
monkeypatch.delenv(key, raising=False)
4547
else:
4648
monkeypatch.setenv(key, value)
4749
stdout_mock = mocker.patch("tox.config.cli.parser.sys.stdout")
4850
stdout_mock.isatty.return_value = is_atty
4951

50-
if tox_color in ("yes", "no"):
52+
if term == 'dumb':
53+
expected = False
54+
elif tox_color in ("yes", "no"):
5155
expected = tox_color == "yes"
5256
elif no_color == "1":
5357
expected = False

0 commit comments

Comments
 (0)