Skip to content

Commit

Permalink
Merge pull request #7154 from kchmck/feature/color-controls
Browse files Browse the repository at this point in the history
Add support for CLICOLOR/CLICOLOR_FORCE/NO_COLOR output controls
  • Loading branch information
czoido authored Jun 30, 2020
2 parents 64599cf + e67c01a commit abc1be3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
23 changes: 20 additions & 3 deletions conans/client/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@


def colorama_initialize():
if "NO_COLOR" in os.environ:
return False

clicolor_force = get_env("CLICOLOR_FORCE")
if clicolor_force is not None and clicolor_force != "0":
import colorama
colorama.init(convert=False, strip=False)
return True

isatty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()

clicolor = get_env("CLICOLOR")
if clicolor is not None:
if clicolor == "0" or not isatty:
return False
import colorama
colorama.init()
return True

# Respect color env setting or check tty if unset
color_set = "CONAN_COLOR_DISPLAY" in os.environ
if ((color_set and get_env("CONAN_COLOR_DISPLAY", 1))
or (not color_set
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty())):
or (not color_set and isatty)):
import colorama
if get_env("PYCHARM_HOSTED"): # in PyCharm disable convert/strip
colorama.init(convert=False, strip=False)
Expand Down
47 changes: 46 additions & 1 deletion conans/test/unittests/client/conan_output_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import unittest
from types import MethodType

from parameterized import parameterized
from six import StringIO

from conans.client.output import ConanOutput
from conans.client.output import ConanOutput, colorama_initialize
from mock import mock


Expand All @@ -28,3 +29,47 @@ def write_raise(self, data):
out.write("Hello world")
sleep.assert_any_call(0.02)
self.assertEqual("Hello world", stream.getvalue())

@parameterized.expand([(False, {}),
(False, {"CONAN_COLOR_DISPLAY": "0"}),
(True, {"CONAN_COLOR_DISPLAY": "0"}),
(False, {"PYCHARM_HOSTED": "1"}),
(True, {"PYCHARM_HOSTED": "1", "CONAN_COLOR_DISPLAY": "0"}),
(True, {"NO_COLOR": ""}),
(True, {"CLICOLOR": "0"}),
(True, {"CLICOLOR": "0", "CONAN_COLOR_DISPLAY": "1"}),
(False, {"CLICOLOR": "1"}),
(False, {"CLICOLOR_FORCE": "0"}),
(True,
{"CLICOLOR": "1", "CLICOLOR_FORCE": "1", "CONAN_COLOR_DISPLAY": "1",
"PYCHARM_HOSTED": "1", "NO_COLOR": "1"})])
def test_output_no_color(self, isatty, env):
with mock.patch("colorama.init") as init:
with mock.patch("sys.stdout.isatty", return_value=isatty), \
mock.patch.dict("os.environ", env, clear=True):
assert not colorama_initialize()
init.assert_not_called()

@parameterized.expand([(True, {}),
(False, {"CONAN_COLOR_DISPLAY": "1"}),
(True, {"CONAN_COLOR_DISPLAY": "1"}),
(True, {"CLICOLOR": "1"}),
(True, {"CLICOLOR_FORCE": "0"})])
def test_output_color(self, isatty, env):
with mock.patch("colorama.init") as init:
with mock.patch("sys.stdout.isatty", return_value=isatty), \
mock.patch.dict("os.environ", env, clear=True):
assert colorama_initialize()
init.assert_called_once_with()

@parameterized.expand([(False, {"PYCHARM_HOSTED": "1", "CONAN_COLOR_DISPLAY": "1"}),
(True, {"PYCHARM_HOSTED": "1"}),
(False, {"CLICOLOR_FORCE": "1"}),
(True, {"CLICOLOR_FORCE": "1", "CLICOLOR": "0"}),
(True, {"CLICOLOR_FORCE": "1", "CONAN_COLOR_DISPLAY": "0"})])
def test_output_color_prevent_strip(self, isatty, env):
with mock.patch("colorama.init") as init:
with mock.patch("sys.stdout.isatty", return_value=isatty), \
mock.patch.dict("os.environ", env, clear=True):
assert colorama_initialize()
init.assert_called_once_with(convert=False, strip=False)

0 comments on commit abc1be3

Please sign in to comment.