Skip to content

Commit

Permalink
tests/shell_scripting: add test for running commands from file
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 7, 2024
1 parent 28493e1 commit fc0b58a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/sys/shell_scripting/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DEVELHELP=0
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
4 changes: 4 additions & 0 deletions tests/sys/shell_scripting/Makefile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BOARD_INSUFFICIENT_MEMORY := \
atmega8 \
nucleo-l011k4 \
#
90 changes: 90 additions & 0 deletions tests/sys/shell_scripting/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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", 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[] = {
"msleep 500\n"
"echo Hello RIOT!\n"
"add 10 23 9\n"
};

if (vfs_file_exists(SCRIPT_FILE)) {
return 0;
}

return vfs_file_from_buffer(SCRIPT_FILE, file, sizeof(file) - 1);
}

int main(void)
{
_create_script();

return shell_parse_file(shell_commands, SCRIPT_FILE);
}
13 changes: 13 additions & 0 deletions tests/sys/shell_scripting/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

import sys
from testrunner import run


def testfunc(child):
child.expect_exact('Hello RIOT!')
child.expect_exact('42')


if __name__ == "__main__":
sys.exit(run(testfunc))

0 comments on commit fc0b58a

Please sign in to comment.