Coverage for tests/tests_cli/test_cli_utility.py: 92%
38 statements
« prev ^ index » next coverage.py v7.11.3, created at 2026-01-05 21:44 +0100
« prev ^ index » next coverage.py v7.11.3, created at 2026-01-05 21:44 +0100
1#!/usr/bin/env python3
2"""RAMSES RF - Test the CLI utility."""
4import io
5import sys
6from collections.abc import Collection
8import pytest
10sys.path.append("../tests_cli") # HACK: to access client.py
12try:
13 import colorama # noqa: F401
14except ModuleNotFoundError as err:
15 pytest.skip(f"{err}", allow_module_level=True) # No module named 'colorama'
18from ramses_cli import _DBG_FORCE_CLI_DEBUGGING # noqa: E402
19from ramses_cli.client import PARSE, cli # noqa: E402
21# TODO: add tests for:
22# client execute /dev/ttyACM0 -x "RQ 01:145038 1F09 00"
23# client execute /dev/ttyACM0 --get-schedule 01:145038 01
26if _DBG_FORCE_CLI_DEBUGGING:
27 pytest.skip(
28 f"_DBG_FORCE_CLI_DEBUGGING = {_DBG_FORCE_CLI_DEBUGGING}",
29 allow_module_level=True,
30 )
33STDIN = io.StringIO("053 I --- 01:123456 --:------ 01:123456 3150 002 FC00\r\n")
35CLI_CONFIG_BASE = {
36 "debug_mode": 0,
37 "restore_schema": None,
38 "restore_state": None,
39 "long_format": False,
40 "print_state": 0,
41 "show_schema": False,
42 "show_params": False,
43 "show_status": False,
44 "show_knowns": False,
45 "show_traits": False,
46 "show_crazys": False,
47}
49CLI_CONFIG_EXECUTE = CLI_CONFIG_BASE | {
50 "discover": None,
51 "exec_cmd": None,
52 "get_faults": None,
53 "get_schedule": (None, None),
54 "set_schedule": (None, None),
55}
56CLI_CONFIG_MONITOR = CLI_CONFIG_BASE | {
57 "exec_cmd": None,
58 "exec_scr": None,
59 "poll_devices": None,
60}
61CLI_CONFIG_LISTEN_ = CLI_CONFIG_BASE
62CLI_CONFIG_PARSE__ = CLI_CONFIG_BASE
64LIB_CONFIG_BASE = {
65 "config": {"reduce_processing": 0, "evofw_flag": None, "disable_discovery": False},
66 "serial_port": None,
67 "packet_log": None,
68}
70LIB_CONFIG_EXECUTE = {
71 "config": {
72 "reduce_processing": 0,
73 "evofw_flag": None,
74 "disable_discovery": True,
75 "disable_qos": False, # the client enforces this
76 },
77 "serial_port": "/dev/ttyUSB0",
78 "packet_log": None,
79}
80LIB_CONFIG_MONITOR = {
81 "config": {"reduce_processing": 0, "evofw_flag": None, "disable_discovery": False},
82 "serial_port": "/dev/ttyUSB0",
83 "packet_log": None,
84}
85LIB_CONFIG_LISTEN_ = {
86 "config": {"reduce_processing": 0, "evofw_flag": None, "disable_sending": True},
87 "serial_port": "/dev/ttyUSB0",
88 "packet_log": None,
89}
90LIB_CONFIG_PARSE__ = {
91 "config": {"reduce_processing": 0},
92 "input_file": "-", # will be replaced by sys.stdin by fileinput
93}
95BASIC_TESTS = ( # can't use "-z"
96 (["client.py", "execute", "/dev/ttyUSB0"], CLI_CONFIG_EXECUTE, LIB_CONFIG_EXECUTE),
97 (["client.py", "monitor", "/dev/ttyUSB0"], CLI_CONFIG_MONITOR, LIB_CONFIG_MONITOR),
98 (["client.py", "listen", "/dev/ttyUSB0"], CLI_CONFIG_LISTEN_, LIB_CONFIG_LISTEN_),
99 (
100 ["client.py", "parse", "filename-stand_in"],
101 CLI_CONFIG_PARSE__,
102 LIB_CONFIG_PARSE__,
103 ),
104)
107def id_fnc(param: int) -> str:
108 return f"{BASIC_TESTS[param][0][1]:7}"
111@pytest.mark.parametrize("index", range(len(BASIC_TESTS)), ids=id_fnc)
112def test_client_basic(
113 monkeypatch: pytest.MonkeyPatch,
114 index: int,
115 tests: tuple[
116 tuple[
117 list[str],
118 dict[str, int | tuple[None, None] | None],
119 dict[str, str | dict[str, int | None] | None],
120 ],
121 tuple[
122 list[str],
123 dict[str, int | None],
124 dict[str, str | dict[str, int | None] | None],
125 ],
126 tuple[
127 list[str],
128 dict[str, int | None],
129 dict[str, str | dict[str, int | None] | None],
130 ],
131 tuple[list[str], dict[str, int | None], dict[str, Collection[str]]],
132 ] = BASIC_TESTS,
133) -> None:
134 monkeypatch.setattr("sys.argv", tests[index][0])
135 if tests[index][0][1] == PARSE:
136 monkeypatch.setattr("sys.stdin", STDIN)
138 cmd_string, lib_config, cli_config = cli(standalone_mode=False)
140 if lib_config.get("input_file"):
141 lib_config["input_file"] = LIB_CONFIG_PARSE__["input_file"]
143 assert cmd_string == tests[index][0][1]
144 assert cli_config == tests[index][1]
145 assert lib_config == tests[index][2]