Skip to content

Commit

Permalink
Disable color if TERM=dumb is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jul 27, 2019
1 parent 376d283 commit 120cae9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions pre_commit/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def use_color(setting):
raise InvalidColorSetting(setting)

return (
setting == 'always' or
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
setting == 'always' or (
setting == 'auto' and
sys.stdout.isatty() and
terminal_supports_color and
os.getenv('TERM') != 'dumb'
)
)
14 changes: 12 additions & 2 deletions tests/color_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mock
import pytest

from pre_commit import envcontext
from pre_commit.color import format_color
from pre_commit.color import GREEN
from pre_commit.color import InvalidColorSetting
Expand Down Expand Up @@ -38,13 +39,22 @@ def test_use_color_no_tty():
def test_use_color_tty_with_color_support():
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
with mock.patch('pre_commit.color.terminal_supports_color', True):
assert use_color('auto') is True
with envcontext.envcontext([('TERM', envcontext.UNSET)]):
assert use_color('auto') is True


def test_use_color_tty_without_color_support():
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
with mock.patch('pre_commit.color.terminal_supports_color', False):
assert use_color('auto') is False
with envcontext.envcontext([('TERM', envcontext.UNSET)]):
assert use_color('auto') is False


def test_use_color_dumb_term():
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
with mock.patch('pre_commit.color.terminal_supports_color', True):
with envcontext.envcontext([('TERM', 'dumb')]):
assert use_color('auto') is False


def test_use_color_raises_if_given_shenanigans():
Expand Down

0 comments on commit 120cae9

Please sign in to comment.