mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1092 from geieredgar/respect-no-color-environment-variable
Don't use color if NO_COLOR environment variable is set
This commit is contained in:
commit
1874cea63e
2 changed files with 19 additions and 0 deletions
|
|
@ -48,6 +48,9 @@ def use_color(setting):
|
||||||
if setting not in COLOR_CHOICES:
|
if setting not in COLOR_CHOICES:
|
||||||
raise InvalidColorSetting(setting)
|
raise InvalidColorSetting(setting)
|
||||||
|
|
||||||
|
if os.environ.get('NO_COLOR'):
|
||||||
|
return False
|
||||||
|
|
||||||
return (
|
return (
|
||||||
setting == 'always' or
|
setting == 'always' or
|
||||||
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
|
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
@ -50,3 +51,18 @@ def test_use_color_tty_without_color_support():
|
||||||
def test_use_color_raises_if_given_shenanigans():
|
def test_use_color_raises_if_given_shenanigans():
|
||||||
with pytest.raises(InvalidColorSetting):
|
with pytest.raises(InvalidColorSetting):
|
||||||
use_color('herpaderp')
|
use_color('herpaderp')
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_color_env_unset():
|
||||||
|
with mock.patch.dict(os.environ, clear=True):
|
||||||
|
assert use_color('always') is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_color_env_empty():
|
||||||
|
with mock.patch.dict(os.environ, NO_COLOR=''):
|
||||||
|
assert use_color('always') is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_color_env_non_empty():
|
||||||
|
with mock.patch.dict(os.environ, NO_COLOR=' '):
|
||||||
|
assert use_color('always') is False
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue