-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This class will be used across the codebase serving as global configuration for colors, utf-8 support and suggestions on the CLI. Signed-off-by: Renan Rodrigo <[email protected]>
- Loading branch information
1 parent
e3cf260
commit 695b503
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import sys | ||
|
||
from uaclient.config import UAConfig | ||
|
||
|
||
# Class attributes and methods so we don't need singletons or globals for this | ||
class ProOutputFormatterConfig: | ||
use_utf8 = True | ||
use_color = True | ||
show_suggestions = True | ||
|
||
# Initializing the class after the import is useful for unit testing | ||
@classmethod | ||
def init(cls, cfg: UAConfig): | ||
cls.use_utf8 = ( | ||
sys.stdout.encoding is not None | ||
and "UTF-8" in sys.stdout.encoding.upper() | ||
) | ||
|
||
cls.use_color = ( | ||
sys.stdout.isatty() | ||
and os.getenv("NO_COLOR") is None | ||
and cfg.cli_color | ||
) | ||
|
||
cls.show_suggestions = cfg.cli_suggestions | ||
|
||
@classmethod | ||
def disable_color(cls) -> None: | ||
cls.use_color = False | ||
|
||
@classmethod | ||
def disable_suggestions(cls) -> None: | ||
cls.show_suggestions = False | ||
|
||
|
||
ProOutputFormatterConfig.init(cfg=UAConfig()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import mock | ||
import pytest | ||
|
||
from uaclient.cli.formatter import ProOutputFormatterConfig as POFC | ||
|
||
M_PATH = "uaclient.cli.formatter." | ||
|
||
|
||
class TestProFormatterConfig: | ||
@pytest.mark.parametrize( | ||
"system_encoding,expected_use_utf8", | ||
((None, False), ("iso-8859-13", False), ("utf-8", True)), | ||
) | ||
def test_use_utf8(self, system_encoding, expected_use_utf8, FakeConfig): | ||
cfg = FakeConfig() | ||
|
||
with mock.patch(M_PATH + "sys.stdout") as m_stdout: | ||
m_stdout.encoding = system_encoding | ||
POFC.init(cfg) | ||
|
||
assert POFC.use_utf8 is expected_use_utf8 | ||
|
||
@pytest.mark.parametrize("config_value", (True, False)) | ||
@pytest.mark.parametrize("is_tty", (True, False)) | ||
@pytest.mark.parametrize("env_no_color", ("True", None)) | ||
@mock.patch(M_PATH + "sys.stdout.isatty") | ||
@mock.patch(M_PATH + "os.getenv") | ||
def test_color_config( | ||
self, | ||
m_getenv, | ||
m_is_tty, | ||
config_value, | ||
is_tty, | ||
env_no_color, | ||
FakeConfig, | ||
): | ||
cfg = FakeConfig() | ||
cfg.user_config.cli_color = config_value | ||
|
||
m_is_tty.return_value = is_tty | ||
|
||
m_getenv.return_value = env_no_color | ||
|
||
POFC.init(cfg) | ||
|
||
expected_result = True | ||
if any((config_value is False, not is_tty, env_no_color)): | ||
expected_result = False | ||
|
||
assert POFC.use_color is expected_result | ||
|
||
POFC.disable_color() | ||
assert POFC.use_color is False | ||
|
||
@pytest.mark.parametrize("config_value", (True, False)) | ||
def test_suggestions_config(self, config_value, FakeConfig): | ||
cfg = FakeConfig() | ||
cfg.user_config.cli_suggestions = config_value | ||
|
||
POFC.init(cfg) | ||
|
||
assert POFC.show_suggestions is config_value | ||
|
||
POFC.disable_suggestions() | ||
assert POFC.show_suggestions is False |