Skip to content

Commit 1874cea

Browse files
authored
Merge pull request #1092 from geieredgar/respect-no-color-environment-variable
Don't use color if NO_COLOR environment variable is set
2 parents 1bd9bfe + 84dcb91 commit 1874cea

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pre_commit/color.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def use_color(setting):
4848
if setting not in COLOR_CHOICES:
4949
raise InvalidColorSetting(setting)
5050

51+
if os.environ.get('NO_COLOR'):
52+
return False
53+
5154
return (
5255
setting == 'always' or
5356
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)

tests/color_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import os
34
import sys
45

56
import mock
@@ -50,3 +51,18 @@ def test_use_color_tty_without_color_support():
5051
def test_use_color_raises_if_given_shenanigans():
5152
with pytest.raises(InvalidColorSetting):
5253
use_color('herpaderp')
54+
55+
56+
def test_no_color_env_unset():
57+
with mock.patch.dict(os.environ, clear=True):
58+
assert use_color('always') is True
59+
60+
61+
def test_no_color_env_empty():
62+
with mock.patch.dict(os.environ, NO_COLOR=''):
63+
assert use_color('always') is True
64+
65+
66+
def test_no_color_env_non_empty():
67+
with mock.patch.dict(os.environ, NO_COLOR=' '):
68+
assert use_color('always') is False

0 commit comments

Comments
 (0)