Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CLICOLOR/CLICOLOR_FORCE/NO_COLOR output controls #7154

Merged
merged 3 commits into from Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions conans/client/output.py
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":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thy not using just get_env() here https://docs.conan.io/en/latest/reference/tools.html#tools-get-env

Suggested change
if clicolor_force is not None and clicolor_force != "0":
if get_env("CLICOLOR_FORCE"):

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
Comment on lines +22 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work as well

Suggested change
clicolor = get_env("CLICOLOR")
if clicolor is not None:
if clicolor == "0" or not isatty:
return False
import colorama
colorama.init()
return True
if not get_env("CLICOLOR", False) or not isatty:
return False
if get_env("CLICOLOR", 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
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)