Skip to content

Commit c2293b7

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 86c5fc7 commit c2293b7

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

tests/test_commands.py

Lines changed: 39 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,33 @@ 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_cli_override_precedence():
135+
command = WestCommandImpl(name="x", help="y", description="z")
136+
command.config = DummyConfig(False)
137+
138+
command._color_override = True
139+
assert command.color_ui is True
140+
141+
command._color_override = False
142+
assert command.color_ui is False
143+
144+
145+
def test_color_ui_uses_config_without_override():
146+
command = WestCommandImpl(name="x", help="y", description="z")
147+
command._color_override = None
148+
149+
command.config = DummyConfig(True)
150+
assert command.color_ui is True
151+
152+
command.config = DummyConfig(False)
153+
assert command.color_ui is False
154+
155+
156+
def test_color_ui_default_without_config():
157+
command = WestCommandImpl(name="x", help="y", description="z")
158+
command._color_override = None
159+
command.config = None
160+
161+
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)