Skip to content

Commit 991ac4d

Browse files
Disable coloring with TERM=dumb or NO_COLOR (#2541)
* Disable coloring with TERM=dumb or NO_COLOR Fixes: #1290 Related: https://no-color.org/ * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add changelog fragment * Fixed logic Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f99073c commit 991ac4d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

docs/changelog/1290.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Recognize ``TERM=dumb`` or ``NO_COLOR`` environment variables. - by :user:`ssbarnea`.

src/tox/config/cli/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,16 @@ def add_color_flags(parser: ArgumentParser) -> None:
292292
color = "no"
293293
elif converter.to_bool(os.environ.get("FORCE_COLOR", "")):
294294
color = "yes"
295+
elif os.environ.get("TERM", "") == "dumb":
296+
color = "no"
295297
else:
296298
color = "yes" if sys.stdout.isatty() else "no"
297299

298300
parser.add_argument(
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ 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, "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 {
45+
"NO_COLOR": no_color,
46+
"TOX_COLORED": tox_color,
47+
"FORCE_COLOR": force_color,
48+
"TERM": term,
49+
}.items():
4350
if value is None:
4451
monkeypatch.delenv(key, raising=False)
4552
else:
@@ -53,6 +60,8 @@ def test_parser_color(
5360
expected = False
5461
elif force_color == "1":
5562
expected = True
63+
elif term == "dumb":
64+
expected = False
5665
else:
5766
expected = is_atty
5867

0 commit comments

Comments
 (0)