Skip to content

Commit 55dc524

Browse files
committed
tests: add coverage for --color option
Add tests for: - CLI override precedence in WestCommand.color_ui - behavior with and without configuration - parsing of the new --color argument in parse_early_args Signed-off-by: Tharaka Jayasena <9dmpires2k17.tuj@gmail.com>
1 parent 6824000 commit 55dc524

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

tests/test_commands.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def do_run(self):
2626

2727
cmd = WestCommandImpl(name="x", help="y", description="z")
2828

29+
30+
class DummyConfig:
31+
def __init__(self, color_ui_value):
32+
self.color_ui_value = color_ui_value
33+
34+
def getboolean(self, _option, default=True):
35+
return self.color_ui_value if self.color_ui_value is not None else default
36+
37+
2938
TEST_STR = "This is some test string"
3039
COL_RED = "\x1b[91m"
3140
COL_YELLOW = "\x1b[93m"
@@ -120,3 +129,28 @@ def test_die(capsys, test_case):
120129
stderr = captured.err
121130
assert stderr == exp_err
122131
assert stdout == exp_out
132+
133+
134+
def test_color_ui_uses_color():
135+
command = WestCommandImpl(name="x", help="y", description="z")
136+
command.color = 'always'
137+
assert command.color_ui is True
138+
139+
command.color = 'never'
140+
assert command.color_ui is False
141+
142+
143+
def test_color_ui_default_is_enabled_when_mode_is_auto():
144+
command = WestCommandImpl(name="x", help="y", description="z")
145+
command.color = 'auto'
146+
command.config = DummyConfig(False)
147+
148+
assert command.color_ui is True
149+
150+
151+
def test_color_ui_default_without_config():
152+
command = WestCommandImpl(name="x", help="y", description="z")
153+
command.color = None
154+
command.config = None
155+
156+
assert command.color_ui is True

tests/test_main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from conftest import cmd, cmd_subprocess
77

8+
import west.app.main as west_main
89
import west.version
910

1011

@@ -48,3 +49,18 @@ def test_module_run(tmp_path, monkeypatch):
4849
# check that that the sys.path was correctly inserted
4950
expected_path = Path(__file__).parents[1] / 'src'
5051
assert actual_path == [f'{expected_path}', 'initial-path']
52+
53+
54+
@pytest.mark.parametrize(
55+
"argv, expected_color, expected_command, expected_unexpected",
56+
[
57+
(['--color=always', 'help'], 'always', 'help', []),
58+
(['--color', 'never', 'status'], 'never', 'status', []),
59+
(['--color', 'invalid', 'status'], None, 'status', ['--color=invalid']),
60+
],
61+
)
62+
def test_parse_early_args_color(argv, expected_color, expected_command, expected_unexpected):
63+
ea = west_main.parse_early_args(argv)
64+
assert ea.color == expected_color
65+
assert ea.command_name == expected_command
66+
assert ea.unexpected_arguments == expected_unexpected

0 commit comments

Comments
 (0)