-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/shell_scripting: add test for running commands from file
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
BOARD_INSUFFICIENT_MEMORY := \ | ||
atmega8 \ | ||
nucleo-l011k4 \ | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |