Skip to content

Commit

Permalink
tests/rust_libs: use shell_builtin_cmd_help_json
Browse files Browse the repository at this point in the history
This increases the robustness of the test by not relying on the
order shell commands are printed in. At least for XFA based shell
commands, there is no guarantee in which order they will be shown in
the help.
  • Loading branch information
maribu committed Nov 11, 2024
1 parent f0a88dc commit b605347
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions tests/rust_libs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include ../Makefile.tests_common

USEMODULE += shell
USEMODULE += shell_democommands
USEMODULE += shell_builtin_cmd_help_json # for automated testing
USEMODULE += ztimer_msec

FEATURES_REQUIRED += rust_target
Expand Down
53 changes: 30 additions & 23 deletions tests/rust_libs/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,25 @@
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

import json
import sys
from testrunner import run


EXPECTED_HELP = (
'Command Description',
'---------------------------------------',
'bufsize Get the shell\'s buffer size',
'start_test starts a test',
'end_test ends a test',
'echo prints the input command',
'empty print nothing on command',
'hello_world Print a greeting',
'xfa_test1 xfa test command 1',
'xfa_test2 xfa test command 2',
)
EXPECTED_CMDS = {
'bufsize': 'Get the shell\'s buffer size',
'start_test': 'starts a test',
'end_test': 'ends a test',
'echo': 'prints the input command',
'empty': 'print nothing on command',
'periodic': 'periodically print command',
'hello_world': 'Print a greeting',
'xfa_test1': 'xfa test command 1',
'xfa_test2': 'xfa test command 2',
}

PROMPT = '> '

CMDS = (
('start_test', '[TEST_START]'),

# test default commands
('help', EXPECTED_HELP),

('end_test', '[TEST_END]'),
)

CMDS_REGEX = {'ps.rs'}


Expand All @@ -49,10 +40,26 @@ def check_cmd(child, cmd, expected):
child.expect_exact(line)


def check_cmd_list(child):
child.expect(PROMPT)
child.sendline('help_json')
child.expect(r"(\{[^\n\r]*\})\r\n")
cmdlist = json.loads(child.match.group(1))["cmds"]
cmds = set(EXPECTED_CMDS)
for item in cmdlist:
assert item['cmd'] in EXPECTED_CMDS, f"command {item['cmd']} not expected"
assert item['cmd'] in cmds, f"command {item['cmd']} listed twice"
assert item['desc'] == EXPECTED_CMDS[item['cmd']], f"description of {item['cmd']} not expected"
cmds.remove(item['cmd'])

assert len(cmds) == 0, f"commands {cmds} missing"


def testfunc(child):
# loop other defined commands and expected output
for cmd, expected in CMDS:
check_cmd(child, cmd, expected)
check_cmd(child, 'start_test', '[TEST_START]')
check_cmd_list(child)
check_cmd(child, 'end_test', '[TEST_END]')


if __name__ == "__main__":
Expand Down

0 comments on commit b605347

Please sign in to comment.