From b60534796a281cf28efab67a26bdccde5049b44e Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 7 Nov 2024 17:37:56 +0100 Subject: [PATCH] tests/rust_libs: use shell_builtin_cmd_help_json 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. --- tests/rust_libs/Makefile | 1 + tests/rust_libs/tests/01-run.py | 53 +++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/tests/rust_libs/Makefile b/tests/rust_libs/Makefile index b2936e89683e..1610d96c5f11 100644 --- a/tests/rust_libs/Makefile +++ b/tests/rust_libs/Makefile @@ -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 diff --git a/tests/rust_libs/tests/01-run.py b/tests/rust_libs/tests/01-run.py index e4994164572b..55495d8e6d6b 100755 --- a/tests/rust_libs/tests/01-run.py +++ b/tests/rust_libs/tests/01-run.py @@ -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'} @@ -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__":