-
Notifications
You must be signed in to change notification settings - Fork 75
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 confg values for the new CLI #3294
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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
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
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 |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the user sets this to
NO_COLOR=0
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From: https://no-color.org/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw the same website says that
NO_COLOR
should be the last checked thing: that if configuration or CLI says show color then you show color regardless.our CLI will have a no-color boolean option instead of a color=? option that you can set to things like 'yes, enabled, disabled, auto, blablabla' so we good CLI wise
but the configuration is not being respected:
NO_COLOR
env means no color, I'm ANDing the cases.If you think this is a problem, we can always rename the config defaults from
cli_color = True
todisable_color = False
and we would get a reasonable behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(by reasonable I mean that any of the triggers would disable the colors, regardless of order, and none would need precedence)