Skip to content

Commit

Permalink
Disable coloring with TERM=dumb or NO_COLOR
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Nov 21, 2022
1 parent f99073c commit 1f2bd20
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/tox/config/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.",
)


Expand Down
8 changes: 6 additions & 2 deletions tests/config/cli/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,27 @@ 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,
no_color: str | None,
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:
monkeypatch.setenv(key, value)
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
Expand Down

0 comments on commit 1f2bd20

Please sign in to comment.