Streamline testing for FORCE_COLOR and NO_COLOR

Streamline the tests for FORCE_COLOR and NO_COLOR variables, and cover
all possible cases (unset, set to empty, set to "1").  Combine the two
assert functions into one taking boolean parameters.  Mock file.isatty
in all circumstances to ensure that the environment variables take
precedence over the fallback value resulting from isatty check (or that
the fallback is actually used, in the case of both FORCE_COLOR
and NO_COLOR being unset).
This commit is contained in:
Michał Górny 2023-12-17 12:13:23 +01:00
parent 4a18fc3757
commit dbeca32eab
1 changed files with 36 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import shutil
import sys import sys
from pathlib import Path from pathlib import Path
from typing import Generator from typing import Generator
from typing import Optional
from unittest import mock from unittest import mock
import pytest import pytest
@ -164,59 +165,67 @@ def test_attr_hasmarkup() -> None:
assert "\x1b[0m" in s assert "\x1b[0m" in s
def assert_color_set(): def assert_color(expected: bool, default: Optional[bool] = None) -> None:
file = io.StringIO() file = io.StringIO()
tw = terminalwriter.TerminalWriter(file) if default is None:
assert tw.hasmarkup default = not expected
file.isatty = lambda: default # type: ignore
tw = terminalwriter.TerminalWriter(file=file)
assert tw.hasmarkup is expected
tw.line("hello", bold=True) tw.line("hello", bold=True)
s = file.getvalue() s = file.getvalue()
assert len(s) > len("hello\n") if expected:
assert "\x1b[1m" in s assert len(s) > len("hello\n")
assert "\x1b[0m" in s assert "\x1b[1m" in s
assert "\x1b[0m" in s
else:
def assert_color_not_set(): assert s == "hello\n"
f = io.StringIO()
f.isatty = lambda: True # type: ignore
tw = terminalwriter.TerminalWriter(file=f)
assert not tw.hasmarkup
tw.line("hello", bold=True)
s = f.getvalue()
assert s == "hello\n"
def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(os.environ, "PY_COLORS", "1") monkeypatch.setitem(os.environ, "PY_COLORS", "1")
assert_color_set() assert_color(True)
def test_should_not_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: def test_should_not_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(os.environ, "PY_COLORS", "0") monkeypatch.setitem(os.environ, "PY_COLORS", "0")
assert_color_not_set() assert_color(False)
def test_should_not_do_markup_NO_COLOR(monkeypatch: MonkeyPatch) -> None: def test_should_not_do_markup_NO_COLOR(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(os.environ, "NO_COLOR", "1") monkeypatch.setitem(os.environ, "NO_COLOR", "1")
assert_color_not_set() assert_color(False)
def test_should_do_markup_FORCE_COLOR(monkeypatch: MonkeyPatch) -> None: def test_should_do_markup_FORCE_COLOR(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(os.environ, "FORCE_COLOR", "1") monkeypatch.setitem(os.environ, "FORCE_COLOR", "1")
assert_color_set() assert_color(True)
def test_should_not_do_markup_NO_COLOR_and_FORCE_COLOR( @pytest.mark.parametrize(
["NO_COLOR", "FORCE_COLOR", "expected"],
[
("1", "1", False),
("", "1", True),
("1", "", False),
],
)
def test_NO_COLOR_and_FORCE_COLOR(
monkeypatch: MonkeyPatch, monkeypatch: MonkeyPatch,
NO_COLOR: str,
FORCE_COLOR: str,
expected: bool,
) -> None: ) -> None:
monkeypatch.setitem(os.environ, "NO_COLOR", "1") monkeypatch.setitem(os.environ, "NO_COLOR", NO_COLOR)
monkeypatch.setitem(os.environ, "FORCE_COLOR", "1") monkeypatch.setitem(os.environ, "FORCE_COLOR", FORCE_COLOR)
assert_color_not_set() assert_color(expected)
def test_empty_NO_COLOR_ignored(monkeypatch: MonkeyPatch) -> None: def test_empty_NO_COLOR_and_FORCE_COLOR_ignored(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(os.environ, "NO_COLOR", "") monkeypatch.setitem(os.environ, "NO_COLOR", "")
monkeypatch.setitem(os.environ, "FORCE_COLOR", "1") monkeypatch.setitem(os.environ, "FORCE_COLOR", "")
assert_color_set() assert_color(True, True)
assert_color(False, False)
class TestTerminalWriterLineWidth: class TestTerminalWriterLineWidth: