From 8c0239ea48ae3321892d5f1224b9ee88c05e7475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 14 Dec 2022 18:53:22 -0800 Subject: [PATCH] NO_COLOR follows no-color.org logic instead of tox boolean (#2727) Co-authored-by: ptmcg --- docs/changelog/2719.feature.rst | 4 ++++ src/tox/config/cli/parser.py | 2 +- tests/config/cli/test_parser.py | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 docs/changelog/2719.feature.rst diff --git a/docs/changelog/2719.feature.rst b/docs/changelog/2719.feature.rst new file mode 100644 index 000000000..b80cace79 --- /dev/null +++ b/docs/changelog/2719.feature.rst @@ -0,0 +1,4 @@ +Modified handling of ``NO_COLOR`` environment variable, consistent with +`de facto conventions `_: any non-empty string will enable ``NO_COLOR`` (disable colorized +output); no ``NO_COLOR`` variable or ``NO_COLOR`` with an empty string will disable ``NO_COLOR`` (enable colorized +output) - by :user:`ptmcg`. diff --git a/src/tox/config/cli/parser.py b/src/tox/config/cli/parser.py index 234fce2a3..b7daeb154 100644 --- a/src/tox/config/cli/parser.py +++ b/src/tox/config/cli/parser.py @@ -290,7 +290,7 @@ def add_verbosity_flags(parser: ArgumentParser) -> None: def add_color_flags(parser: ArgumentParser) -> None: converter = StrConvert() - if converter.to_bool(os.environ.get("NO_COLOR", "")): + if os.environ.get("NO_COLOR", "") != "": color = "no" elif converter.to_bool(os.environ.get("FORCE_COLOR", "")): color = "yes" diff --git a/tests/config/cli/test_parser.py b/tests/config/cli/test_parser.py index 2cbf21d73..1029b25ac 100644 --- a/tests/config/cli/test_parser.py +++ b/tests/config/cli/test_parser.py @@ -28,7 +28,7 @@ def test_parser_const_with_default_none(monkeypatch: MonkeyPatch) -> None: @pytest.mark.parametrize("is_atty", [True, False]) -@pytest.mark.parametrize("no_color", [None, "0", "1"]) +@pytest.mark.parametrize("no_color", [None, "0", "1", "", "\t", " ", "false", "true"]) @pytest.mark.parametrize("force_color", [None, "0", "1"]) @pytest.mark.parametrize("tox_color", [None, "bad", "no", "yes"]) @pytest.mark.parametrize("term", [None, "xterm", "dumb"]) @@ -56,7 +56,7 @@ def test_parser_color( if tox_color in ("yes", "no"): expected = tox_color == "yes" - elif no_color == "1": + elif bool(no_color): expected = False elif force_color == "1": expected = True