Skip to content

Commit c7b5419

Browse files
committed
Fix FORCE_COLOR and NO_COLOR when empty strings
1 parent 96c2e76 commit c7b5419

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Diff for: Lib/_colorize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def can_colorize(*, file=None) -> bool:
4242
return False
4343
if os.environ.get("PYTHON_COLORS") == "1":
4444
return True
45-
if "NO_COLOR" in os.environ:
45+
if os.environ.get("NO_COLOR"):
4646
return False
4747
if not COLORIZE:
4848
return False
49-
if "FORCE_COLOR" in os.environ:
49+
if os.environ.get("FORCE_COLOR"):
5050
return True
5151
if os.environ.get("TERM") == "dumb":
5252
return False

Diff for: Lib/test/test__colorize.py

+17
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,34 @@ def test_colorized_detection_checks_for_environment_variables(self):
4848
({"PYTHON_COLORS": "1"}, True),
4949
({"PYTHON_COLORS": "0"}, False),
5050
({"NO_COLOR": "1"}, False),
51+
({"NO_COLOR": "0"}, False),
5152
({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True),
5253
({"FORCE_COLOR": "1"}, True),
5354
({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False),
55+
({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False),
56+
({"FORCE_COLOR": "1", "NO_COLOR": ""}, True),
57+
({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False),
58+
({"FORCE_COLOR": "", "NO_COLOR": "1"}, False),
5459
({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False),
60+
({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False),
5561
]:
5662
with self.subTest(env_vars=env_vars, expected=expected):
5763
with unittest.mock.patch("os.environ", env_vars):
5864
self.assertEqual(_colorize.can_colorize(), expected)
5965

66+
with unittest.mock.patch("os.environ", {"NO_COLOR": ""}):
67+
if sys.platform == "win32":
68+
vt_mock.return_value = False
69+
self.assertEqual(_colorize.can_colorize(), False)
70+
71+
vt_mock.return_value = True
72+
self.assertEqual(_colorize.can_colorize(), True)
73+
else:
74+
self.assertEqual(_colorize.can_colorize(), True)
75+
6076
with unittest.mock.patch("os.environ", {}):
6177
if sys.platform == "win32":
78+
vt_mock.return_value = False
6279
self.assertEqual(_colorize.can_colorize(), False)
6380

6481
vt_mock.return_value = True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix FORCE_COLOR and NO_COLOR when empty strings. Patch by Hugo van Kemenade.

0 commit comments

Comments
 (0)