From b159c544fa2dd17024c1ec4ec6365a9b4b5163f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Fri, 15 Aug 2025 11:21:55 +0200 Subject: [PATCH] Follow recommendations for `FORCE_COLOR` When the `FORCE_COLOR` variable is present and not an empty string, it forces ANSI color to be set. Previously, ony `yes`, `true` or `0` were valid values. See https://force-color.org/. Fixes #3579 --- docs/changelog/3579.bugfix.rst | 2 ++ src/tox/config/cli/parser.py | 4 +--- tests/config/cli/test_parser.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 docs/changelog/3579.bugfix.rst diff --git a/docs/changelog/3579.bugfix.rst b/docs/changelog/3579.bugfix.rst new file mode 100644 index 0000000000..34331aa26a --- /dev/null +++ b/docs/changelog/3579.bugfix.rst @@ -0,0 +1,2 @@ +When the ``FORCE_COLOR`` variable is present and not an empty string, it forces ANSI color to be set. +Previously, only ``yes``, ``true`` or ``0`` were valid values. - by :user:`jugmac00` diff --git a/src/tox/config/cli/parser.py b/src/tox/config/cli/parser.py index 0b1133b4ba..6048a09087 100644 --- a/src/tox/config/cli/parser.py +++ b/src/tox/config/cli/parser.py @@ -13,7 +13,6 @@ from colorama import Fore -from tox.config.loader.str_convert import StrConvert from tox.plugin import NAME from tox.util.ci import is_ci @@ -357,10 +356,9 @@ def add_verbosity_flags(parser: ArgumentParser) -> None: def add_color_flags(parser: ArgumentParser) -> None: - converter = StrConvert() if os.environ.get("NO_COLOR", ""): color = "no" - elif converter.to_bool(os.environ.get("FORCE_COLOR", "")): + elif os.environ.get("FORCE_COLOR", ""): color = "yes" elif os.environ.get("TERM", "") == "dumb": color = "no" diff --git a/tests/config/cli/test_parser.py b/tests/config/cli/test_parser.py index 779f08bf25..7cceb11d4e 100644 --- a/tests/config/cli/test_parser.py +++ b/tests/config/cli/test_parser.py @@ -33,10 +33,10 @@ 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", "", "\t", " ", "false", "true"]) -@pytest.mark.parametrize("force_color", [None, "0", "1"]) +@pytest.mark.parametrize("force_color", [None, "", "0", "1", "string"]) @pytest.mark.parametrize("tox_color", [None, "bad", "no", "yes"]) @pytest.mark.parametrize("term", [None, "xterm", "dumb"]) -def test_parser_color( # noqa: PLR0913 +def test_color( # noqa: PLR0913 monkeypatch: MonkeyPatch, mocker: MockerFixture, no_color: str | None, @@ -62,7 +62,7 @@ def test_parser_color( # noqa: PLR0913 expected = tox_color == "yes" elif bool(no_color): expected = False - elif force_color == "1": + elif force_color in {"0", "1", "string"}: expected = True elif term == "dumb": expected = False