From 1bf9ff74939d899fe18a2325a29b7a59f953d214 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 22 Jul 2019 19:18:36 +0200 Subject: [PATCH 1/5] Don't use color if NO_COLOR environment variable is set --- pre_commit/color.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pre_commit/color.py b/pre_commit/color.py index c785e2c9..831d50bf 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -48,6 +48,9 @@ def use_color(setting): if setting not in COLOR_CHOICES: raise InvalidColorSetting(setting) + if 'NO_COLOR' in os.environ: + return False + return ( setting == 'always' or (setting == 'auto' and sys.stdout.isatty() and terminal_supports_color) From 01d3a72a0ed1e3a43a45b9908a5b9200593dee32 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 22 Jul 2019 19:35:39 +0200 Subject: [PATCH 2/5] Require NO_COLOR environment variable to be non-empty to disable colors --- pre_commit/color.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit/color.py b/pre_commit/color.py index 831d50bf..102639ad 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -48,7 +48,7 @@ def use_color(setting): if setting not in COLOR_CHOICES: raise InvalidColorSetting(setting) - if 'NO_COLOR' in os.environ: + if 'NO_COLOR' in os.environ and os.environ['NO_COLOR']: return False return ( From 85204550425b69990c0c3b28e66296b013901a33 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 22 Jul 2019 20:07:16 +0200 Subject: [PATCH 3/5] Add tests for NO_COLOR support --- tests/color_test.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/color_test.py b/tests/color_test.py index 6e11765c..fb311c85 100644 --- a/tests/color_test.py +++ b/tests/color_test.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import os import sys import mock @@ -50,3 +51,20 @@ def test_use_color_tty_without_color_support(): def test_use_color_raises_if_given_shenanigans(): with pytest.raises(InvalidColorSetting): use_color('herpaderp') + + +def test_no_color_env_unset(): + with mock.patch.dict(os.environ): + if 'NO_COLOR' in os.environ: + del os.environ['NO_COLOR'] + 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 From e9ff1be96c831a1f77708b782dca19fe6d7250da Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 22 Jul 2019 20:09:32 +0200 Subject: [PATCH 4/5] Simplify NO_COLOR check --- pre_commit/color.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit/color.py b/pre_commit/color.py index 102639ad..2ede410a 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -48,7 +48,7 @@ def use_color(setting): if setting not in COLOR_CHOICES: raise InvalidColorSetting(setting) - if 'NO_COLOR' in os.environ and os.environ['NO_COLOR']: + if os.environ.get('NO_COLOR'): return False return ( From 84dcb911196783cde209b095acc26d4089a24200 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 22 Jul 2019 20:23:59 +0200 Subject: [PATCH 5/5] Change test to remove missed branch --- tests/color_test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/color_test.py b/tests/color_test.py index fb311c85..4ba3f327 100644 --- a/tests/color_test.py +++ b/tests/color_test.py @@ -54,9 +54,7 @@ def test_use_color_raises_if_given_shenanigans(): def test_no_color_env_unset(): - with mock.patch.dict(os.environ): - if 'NO_COLOR' in os.environ: - del os.environ['NO_COLOR'] + with mock.patch.dict(os.environ, clear=True): assert use_color('always') is True