1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

tests/shell_scripting: add test for running commands from file

This commit is contained in:
Benjamin Valentin 2024-05-06 19:34:41 +02:00
parent 471e257c7c
commit cc2dca1064
4 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,13 @@
include ../Makefile.sys_common
USEMODULE += shell
USEMODULE += vfs_default
USEMODULE += vfs_util
USEMODULE += ztimer_msec
DISABLE_MODULE += test_utils_interactive_sync
# only boards with MTD are eligible
TEST_ON_CI_WHITELIST += native native64
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,4 @@
BOARD_INSUFFICIENT_MEMORY := \
atmega8 \
nucleo-l011k4 \
#

View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2024 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Shell Scripting Test Application
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/
#include <stdlib.h>
#include "shell.h"
#include "vfs_default.h"
#include "vfs_util.h"
#include "ztimer.h"
#ifndef SCRIPT_FILE
#define SCRIPT_FILE VFS_DEFAULT_DATA "/script.sh"
#endif
static int cmd_msleep(int argc, char **argv)
{
if (argc != 2) {
return -1;
}
ztimer_sleep(ZTIMER_MSEC, atoi(argv[1]));
return 0;
}
static int cmd_echo(int argc, char **argv)
{
for (int i = 1; i < argc; ++i) {
printf("%s ", argv[i]);
}
puts("");
return 0;
}
static int cmd_add(int argc, char **argv)
{
int sum = 0;
for (int i = 1; i < argc; ++i) {
sum += atoi(argv[i]);
}
printf("%d\n", sum);
return 0;
}
static const shell_command_t shell_commands[] = {
{ "msleep", "sleep for a number of ms", cmd_msleep },
{ "echo", "echo parameters to console", cmd_echo },
{ "add", "add up a list of numbers", cmd_add },
{ NULL, NULL, NULL }
};
static int _create_script(void)
{
const char file[] = {
"# this is a comment\n"
"msleep 500\n"
"echo Hello RIOT!\n"
"\n"
"add 10 23 9\n"
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
};
return vfs_file_from_buffer(SCRIPT_FILE, file, sizeof(file) - 1);
}
int main(void)
{
_create_script();
unsigned line;
int res = shell_parse_file(shell_commands, SCRIPT_FILE, &line);
if (res) {
fprintf(stderr, "%s:%u: error %d\n", SCRIPT_FILE, line, res);
}
return res;
}

View File

@ -0,0 +1,14 @@
#!/usr/bin/env python3
import sys
from testrunner import run
def testfunc(child):
child.expect_exact('Hello RIOT!')
child.expect_exact('42')
child.expect_exact('/nvm0/script.sh:6: error -7')
if __name__ == "__main__":
sys.exit(run(testfunc))