Skip to content

Commit 4a18fc3

Browse files
committed
Fix handling empty values of NO_COLOR and FORCE_COLOR
Fix handling NO_COLOR and FORCE_COLOR environment variables to correctly be ignored when they are set to an empty value, as defined in the specification: > Command-line software which adds ANSI color to its output by default > should check for a NO_COLOR environment variable that, when present > *and not an empty string* (regardless of its value), prevents > the addition of ANSI color. (emphasis mine, https://no-color.org/) The same is true of FORCE_COLOR, https://force-color.org/.
1 parent 047ba83 commit 4a18fc3

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Michael Goerz
266266
Michael Krebs
267267
Michael Seifert
268268
Michal Wajszczuk
269+
Michał Górny
269270
Michał Zięba
270271
Mickey Pashov
271272
Mihai Capotă

changelog/11712.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed handling ``NO_COLOR`` and ``FORCE_COLOR`` to ignore an empty value.

doc/en/reference/reference.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,13 +1146,13 @@ When set to ``0``, pytest will not use color.
11461146

11471147
.. envvar:: NO_COLOR
11481148

1149-
When set (regardless of value), pytest will not use color in terminal output.
1149+
When set to a non-empty string (regardless of value), pytest will not use color in terminal output.
11501150
``PY_COLORS`` takes precedence over ``NO_COLOR``, which takes precedence over ``FORCE_COLOR``.
11511151
See `no-color.org <https://no-color.org/>`__ for other libraries supporting this community standard.
11521152

11531153
.. envvar:: FORCE_COLOR
11541154

1155-
When set (regardless of value), pytest will use color in terminal output.
1155+
When set to a non-empty string (regardless of value), pytest will use color in terminal output.
11561156
``PY_COLORS`` and ``NO_COLOR`` take precedence over ``FORCE_COLOR``.
11571157

11581158
Exceptions

src/_pytest/_io/terminalwriter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def should_do_markup(file: TextIO) -> bool:
2929
return True
3030
if os.environ.get("PY_COLORS") == "0":
3131
return False
32-
if "NO_COLOR" in os.environ:
32+
if os.environ.get("NO_COLOR"):
3333
return False
34-
if "FORCE_COLOR" in os.environ:
34+
if os.environ.get("FORCE_COLOR"):
3535
return True
3636
return (
3737
hasattr(file, "isatty") and file.isatty() and os.environ.get("TERM") != "dumb"

testing/io/test_terminalwriter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ def test_should_not_do_markup_NO_COLOR_and_FORCE_COLOR(
213213
assert_color_not_set()
214214

215215

216+
def test_empty_NO_COLOR_ignored(monkeypatch: MonkeyPatch) -> None:
217+
monkeypatch.setitem(os.environ, "NO_COLOR", "")
218+
monkeypatch.setitem(os.environ, "FORCE_COLOR", "1")
219+
assert_color_set()
220+
221+
216222
class TestTerminalWriterLineWidth:
217223
def test_init(self) -> None:
218224
tw = terminalwriter.TerminalWriter()

0 commit comments

Comments
 (0)