Skip to content

Commit cc2dca1

Browse files
committed
tests/shell_scripting: add test for running commands from file
1 parent 471e257 commit cc2dca1

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

tests/sys/shell_scripting/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include ../Makefile.sys_common
2+
3+
USEMODULE += shell
4+
USEMODULE += vfs_default
5+
USEMODULE += vfs_util
6+
USEMODULE += ztimer_msec
7+
8+
DISABLE_MODULE += test_utils_interactive_sync
9+
10+
# only boards with MTD are eligible
11+
TEST_ON_CI_WHITELIST += native native64
12+
13+
include $(RIOTBASE)/Makefile.include
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BOARD_INSUFFICIENT_MEMORY := \
2+
atmega8 \
3+
nucleo-l011k4 \
4+
#

tests/sys/shell_scripting/main.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (C) 2024 ML!PA Consulting GmbH
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup tests
11+
* @{
12+
*
13+
* @file
14+
* @brief Shell Scripting Test Application
15+
*
16+
* @author Benjamin Valentin <[email protected]>
17+
* @}
18+
*/
19+
20+
#include <stdlib.h>
21+
22+
#include "shell.h"
23+
#include "vfs_default.h"
24+
#include "vfs_util.h"
25+
#include "ztimer.h"
26+
27+
#ifndef SCRIPT_FILE
28+
#define SCRIPT_FILE VFS_DEFAULT_DATA "/script.sh"
29+
#endif
30+
31+
static int cmd_msleep(int argc, char **argv)
32+
{
33+
if (argc != 2) {
34+
return -1;
35+
}
36+
37+
ztimer_sleep(ZTIMER_MSEC, atoi(argv[1]));
38+
return 0;
39+
}
40+
41+
static int cmd_echo(int argc, char **argv)
42+
{
43+
for (int i = 1; i < argc; ++i) {
44+
printf("%s ", argv[i]);
45+
}
46+
puts("");
47+
48+
return 0;
49+
}
50+
51+
static int cmd_add(int argc, char **argv)
52+
{
53+
int sum = 0;
54+
55+
for (int i = 1; i < argc; ++i) {
56+
sum += atoi(argv[i]);
57+
}
58+
59+
printf("%d\n", sum);
60+
return 0;
61+
}
62+
63+
static const shell_command_t shell_commands[] = {
64+
{ "msleep", "sleep for a number of ms", cmd_msleep },
65+
{ "echo", "echo parameters to console", cmd_echo },
66+
{ "add", "add up a list of numbers", cmd_add },
67+
{ NULL, NULL, NULL }
68+
};
69+
70+
static int _create_script(void)
71+
{
72+
const char file[] = {
73+
"# this is a comment\n"
74+
"msleep 500\n"
75+
"echo Hello RIOT!\n"
76+
"\n"
77+
"add 10 23 9\n"
78+
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
79+
};
80+
81+
return vfs_file_from_buffer(SCRIPT_FILE, file, sizeof(file) - 1);
82+
}
83+
84+
int main(void)
85+
{
86+
_create_script();
87+
88+
unsigned line;
89+
int res = shell_parse_file(shell_commands, SCRIPT_FILE, &line);
90+
if (res) {
91+
fprintf(stderr, "%s:%u: error %d\n", SCRIPT_FILE, line, res);
92+
}
93+
return res;
94+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from testrunner import run
5+
6+
7+
def testfunc(child):
8+
child.expect_exact('Hello RIOT!')
9+
child.expect_exact('42')
10+
child.expect_exact('/nvm0/script.sh:6: error -7')
11+
12+
13+
if __name__ == "__main__":
14+
sys.exit(run(testfunc))

0 commit comments

Comments
 (0)