mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 20:40:08 -06:00
Merge pull request #819 from jeffreyrack/fix-windows-reporting
Don't print bogus characters on windows terminals that don't support colors
This commit is contained in:
@@ -3,12 +3,13 @@ from __future__ import unicode_literals
|
||||
import os
|
||||
import sys
|
||||
|
||||
terminal_supports_color = True
|
||||
if os.name == 'nt': # pragma: no cover (windows)
|
||||
from pre_commit.color_windows import enable_virtual_terminal_processing
|
||||
try:
|
||||
enable_virtual_terminal_processing()
|
||||
except WindowsError:
|
||||
pass
|
||||
terminal_supports_color = False
|
||||
|
||||
RED = '\033[41m'
|
||||
GREEN = '\033[42m'
|
||||
@@ -47,4 +48,7 @@ def use_color(setting):
|
||||
if setting not in COLOR_CHOICES:
|
||||
raise InvalidColorSetting(setting)
|
||||
|
||||
return setting == 'always' or (setting == 'auto' and sys.stdout.isatty())
|
||||
return (
|
||||
setting == 'always' or
|
||||
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
|
||||
)
|
||||
|
||||
@@ -35,9 +35,16 @@ def test_use_color_no_tty():
|
||||
assert use_color('auto') is False
|
||||
|
||||
|
||||
def test_use_color_tty():
|
||||
def test_use_color_tty_with_color_support():
|
||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||
assert use_color('auto') is True
|
||||
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
||||
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
|
||||
|
||||
|
||||
def test_use_color_raises_if_given_shenanigans():
|
||||
|
||||
Reference in New Issue
Block a user