Skip to content

Commit 120cae9

Browse files
committed
Disable color if TERM=dumb is detected
1 parent 376d283 commit 120cae9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pre_commit/color.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def use_color(setting):
4949
raise InvalidColorSetting(setting)
5050

5151
return (
52-
setting == 'always' or
53-
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
52+
setting == 'always' or (
53+
setting == 'auto' and
54+
sys.stdout.isatty() and
55+
terminal_supports_color and
56+
os.getenv('TERM') != 'dumb'
57+
)
5458
)

tests/color_test.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import mock
66
import pytest
77

8+
from pre_commit import envcontext
89
from pre_commit.color import format_color
910
from pre_commit.color import GREEN
1011
from pre_commit.color import InvalidColorSetting
@@ -38,13 +39,22 @@ def test_use_color_no_tty():
3839
def test_use_color_tty_with_color_support():
3940
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
4041
with mock.patch('pre_commit.color.terminal_supports_color', True):
41-
assert use_color('auto') is True
42+
with envcontext.envcontext([('TERM', envcontext.UNSET)]):
43+
assert use_color('auto') is True
4244

4345

4446
def test_use_color_tty_without_color_support():
4547
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
4648
with mock.patch('pre_commit.color.terminal_supports_color', False):
47-
assert use_color('auto') is False
49+
with envcontext.envcontext([('TERM', envcontext.UNSET)]):
50+
assert use_color('auto') is False
51+
52+
53+
def test_use_color_dumb_term():
54+
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
55+
with mock.patch('pre_commit.color.terminal_supports_color', True):
56+
with envcontext.envcontext([('TERM', 'dumb')]):
57+
assert use_color('auto') is False
4858

4959

5060
def test_use_color_raises_if_given_shenanigans():

0 commit comments

Comments
 (0)