Skip to content

Commit 4ff681f

Browse files
authored
fix: ignore forcecolor falsy (#1073)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 5a90136 commit 4ff681f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

nox/_options.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ def _tag_completer(
655655
"--forcecolor",
656656
"--force-color",
657657
group=options.groups["reporting"],
658-
default=lambda: "FORCE_COLOR" in os.environ,
658+
default=lambda: (
659+
os.environ.get("FORCE_COLOR", "").lower()
660+
not in {"", "0", "false", "no", "off"}
661+
),
659662
action="store_true",
660663
help="Force color output, even if stdout is not an interactive terminal.",
661664
),

tests/test_main.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,43 @@ def test_main_color_options(
861861
assert config.color == expected
862862

863863

864+
@pytest.mark.parametrize(
865+
"force_color_value",
866+
["0", "false", "False", "FALSE", "no", "No", "NO", "off", "Off", "OFF", ""],
867+
)
868+
def test_main_force_color_falsy(
869+
monkeypatch: pytest.MonkeyPatch, force_color_value: str
870+
) -> None:
871+
"""FORCE_COLOR set to a falsey value should not enable force color."""
872+
monkeypatch.setenv("FORCE_COLOR", force_color_value)
873+
monkeypatch.delenv("NO_COLOR", raising=False)
874+
monkeypatch.setattr(sys, "argv", [sys.executable])
875+
with mock.patch("nox.workflow.execute") as execute:
876+
execute.return_value = 0
877+
with (
878+
mock.patch("sys.stdout.isatty", return_value=False),
879+
mock.patch.object(sys, "exit"),
880+
):
881+
nox.main()
882+
config = execute.call_args[1]["global_config"]
883+
assert config.color is False
884+
885+
886+
def test_main_no_color_and_force_color_zero(
887+
monkeypatch: pytest.MonkeyPatch,
888+
) -> None:
889+
"""NO_COLOR=1 and FORCE_COLOR=0 should not conflict (issue #1069)."""
890+
monkeypatch.setenv("NO_COLOR", "1")
891+
monkeypatch.setenv("FORCE_COLOR", "0")
892+
monkeypatch.setattr(sys, "argv", [sys.executable])
893+
with mock.patch("nox.workflow.execute") as execute:
894+
execute.return_value = 0
895+
with mock.patch.object(sys, "exit"):
896+
nox.main()
897+
config = execute.call_args[1]["global_config"]
898+
assert config.color is False
899+
900+
864901
def test_main_color_conflict(
865902
capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch
866903
) -> None:

0 commit comments

Comments
 (0)