Coverage for tests/tests/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

7 import pytest

8

9 sys.path.append(".") # HACK: to access client.py

10

11 try:

12 import colorama # noqa: F401

13 except ModuleNotFoundError as err:

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

15

16

17 from ramses_cli import _DBG_FORCE_CLI_DEBUGGING # noqa: E402

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

19

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

23

24

25 if _DBG_FORCE_CLI_DEBUGGING:

26 pytest.skip(

27 f"_DBG_FORCE_CLI_DEBUGGING = {_DBG_FORCE_CLI_DEBUGGING}",

28 allow_module_level=True,

29 )

30

31

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

33

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

47

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

55 CLI_CONFIG_MONITOR = CLI_CONFIG_BASE | {

56 "exec_cmd": None,

57 "exec_scr": None,

58 "poll_devices": None,

59}

60 CLI_CONFIG_LISTEN_ = CLI_CONFIG_BASE

61 CLI_CONFIG_PARSE__ = CLI_CONFIG_BASE

62

63 LIB_CONFIG_BASE = {

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

65 "serial_port": None,

66 "packet_log": None,

67}

68

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

79 LIB_CONFIG_MONITOR = {

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

81 "serial_port": "/dev/ttyUSB0",

82 "packet_log": None,

83}

84 LIB_CONFIG_LISTEN_ = {

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

86 "serial_port": "/dev/ttyUSB0",

87 "packet_log": None,

88}

89 LIB_CONFIG_PARSE__ = {

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

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

92}

93

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

104

105

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

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

108

109

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

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

117

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

119

120 if lib_config.get("input_file"):

121 lib_config["input_file"] = LIB_CONFIG_PARSE__["input_file"]

122

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

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

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