Coverage for tests/tests_cli/test_cli_utility.py: 92%

1#!/usr/bin/env python3

2"""RAMSES RF - Test the CLI utility."""

3

4 import io

5 import sys

6 from collections.abc import Collection

7

8 import pytest

9

10 sys.path.append("../tests_cli") # HACK: to access client.py

11

12 try:

13 import colorama # noqa: F401

14 except ModuleNotFoundError as err:

15 pytest.skip(f"{err}", allow_module_level=True) # No module named 'colorama'

16

17

18 from ramses_cli import _DBG_FORCE_CLI_DEBUGGING # noqa: E402

19 from ramses_cli.client import PARSE, cli # noqa: E402

20

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

24

25

26 if _DBG_FORCE_CLI_DEBUGGING:

27 pytest.skip(

28 f"_DBG_FORCE_CLI_DEBUGGING = {_DBG_FORCE_CLI_DEBUGGING}",

29 allow_module_level=True,

30 )

31

32

33 STDIN = io.StringIO("053 I --- 01:123456 --:------ 01:123456 3150 002 FC00\r\n")

34

35 CLI_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}

48

49 CLI_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}

56 CLI_CONFIG_MONITOR = CLI_CONFIG_BASE | {

57 "exec_cmd": None,

58 "exec_scr": None,

59 "poll_devices": None,

60}

61 CLI_CONFIG_LISTEN_ = CLI_CONFIG_BASE

62 CLI_CONFIG_PARSE__ = CLI_CONFIG_BASE

63

64 LIB_CONFIG_BASE = {

65 "config": {"reduce_processing": 0, "evofw_flag": None, "disable_discovery": False},

66 "serial_port": None,

67 "packet_log": None,

68}

69

70 LIB_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}

80 LIB_CONFIG_MONITOR = {

81 "config": {"reduce_processing": 0, "evofw_flag": None, "disable_discovery": False},

82 "serial_port": "/dev/ttyUSB0",

83 "packet_log": None,

84}

85 LIB_CONFIG_LISTEN_ = {

86 "config": {"reduce_processing": 0, "evofw_flag": None, "disable_sending": True},

87 "serial_port": "/dev/ttyUSB0",

88 "packet_log": None,

89}

90 LIB_CONFIG_PARSE__ = {

91 "config": {"reduce_processing": 0},

92 "input_file": "-", # will be replaced by sys.stdin by fileinput

93}

94

95 BASIC_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)

105

106

107 def id_fnc(param: int) -> str:

108 return f"{BASIC_TESTS[param][0][1]:7}"

109

110

111@pytest.mark.parametrize("index", range(len(BASIC_TESTS)), ids=id_fnc)

112 def 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)

137

138 cmd_string, lib_config, cli_config = cli(standalone_mode=False)

139

140 if lib_config.get("input_file"):

141 lib_config["input_file"] = LIB_CONFIG_PARSE__["input_file"]

142

143 assert cmd_string == tests[index][0][1]

144 assert cli_config == tests[index][1]

145 assert lib_config == tests[index][2]