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