Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def can_colorize(*, file=None) -> bool:
return False
if os.environ.get("PYTHON_COLORS") == "1":
return True
if "NO_COLOR" in os.environ:
if os.environ.get("NO_COLOR"):
return False
if not COLORIZE:
return False
if "FORCE_COLOR" in os.environ:
if os.environ.get("FORCE_COLOR"):
return True
if os.environ.get("TERM") == "dumb":
return False
Expand Down
50 changes: 31 additions & 19 deletions Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,40 @@ def test_colorized_detection_checks_for_environment_variables(self):
stdout_mock.isatty.return_value = True
stderr_mock.fileno.return_value = 2
stderr_mock.isatty.return_value = True
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ",
{'NO_COLOR': '1', "PYTHON_COLORS": '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(_colorize.can_colorize(), False)

for env_vars, expected in [
({"TERM": "dumb"}, False),
({"PYTHON_COLORS": "1"}, True),
({"PYTHON_COLORS": "0"}, False),
({"NO_COLOR": "1"}, False),
({"NO_COLOR": "0"}, False),
({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True),
({"FORCE_COLOR": "1"}, True),
({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False),
({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False),
({"FORCE_COLOR": "1", "NO_COLOR": ""}, True),
({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False),
({"FORCE_COLOR": "", "NO_COLOR": "1"}, False),
({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False),
({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False),
]:
with self.subTest(env_vars=env_vars, expected=expected):
with unittest.mock.patch("os.environ", env_vars):
self.assertEqual(_colorize.can_colorize(), expected)

with unittest.mock.patch("os.environ", {"NO_COLOR": ""}):
if sys.platform == "win32":
vt_mock.return_value = False
self.assertEqual(_colorize.can_colorize(), False)

vt_mock.return_value = True
self.assertEqual(_colorize.can_colorize(), True)
else:
self.assertEqual(_colorize.can_colorize(), True)

with unittest.mock.patch("os.environ", {}):
if sys.platform == "win32":
vt_mock.return_value = False
self.assertEqual(_colorize.can_colorize(), False)

vt_mock.return_value = True
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix FORCE_COLOR and NO_COLOR when empty strings. Patch by Hugo van Kemenade.
Loading