Skip to content

Commit 023248a

Browse files
asemjonovsnashif
authored andcommitted
shell: Add test for custom shell macros
Verify customized shell macro APIs Signed-off-by: Al Semjonovs <[email protected]>
1 parent 0bca046 commit 023248a

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(shell_custom_header)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
zephyr_include_directories(src)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CONFIG_SHELL=y
2+
CONFIG_SHELL_BACKEND_SERIAL=n
3+
CONFIG_SHELL_BACKEND_DUMMY=y
4+
CONFIG_SHELL_CMDS_SELECT=y
5+
CONFIG_SHELL_CMD_BUFF_SIZE=90
6+
CONFIG_SHELL_PRINTF_BUFF_SIZE=15
7+
CONFIG_SHELL_METAKEYS=n
8+
CONFIG_SHELL_CUSTOM_HEADER=y
9+
CONFIG_LOG=n
10+
CONFIG_ZTEST=y
11+
CONFIG_TEST_LOGGING_DEFAULTS=n
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2024 Google, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/** @file
8+
* @brief Custom header shell test suite
9+
*
10+
*/
11+
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/ztest.h>
14+
15+
#include <zephyr/shell/shell.h>
16+
#include <zephyr/shell/shell_dummy.h>
17+
18+
static void *shell_setup(void)
19+
{
20+
const struct shell *sh = shell_backend_dummy_get_ptr();
21+
22+
/* Wait for the initialization of the shell dummy backend. */
23+
WAIT_FOR(shell_ready(sh), 20000, k_msleep(1));
24+
zassert_true(shell_ready(sh), "timed out waiting for dummy shell backend");
25+
26+
return NULL;
27+
}
28+
29+
ZTEST_SUITE(sh, NULL, shell_setup, NULL, NULL, NULL);
30+
31+
ZTEST(sh, test_shell_fprintf)
32+
{
33+
static const char expect[] = "[CUSTOM_PREFIX]testing 1 2 3";
34+
const struct shell *sh;
35+
const char *buf;
36+
size_t size;
37+
38+
sh = shell_backend_dummy_get_ptr();
39+
zassert_not_null(sh, "Failed to get shell");
40+
41+
/* Clear the output buffer */
42+
shell_backend_dummy_clear_output(sh);
43+
44+
shell_fprintf(sh, SHELL_VT100_COLOR_DEFAULT, "testing %d %s %c",
45+
1, "2", '3');
46+
buf = shell_backend_dummy_get_output(sh, &size);
47+
zassert_true(size >= sizeof(expect), "Expected size > %u, got %d",
48+
sizeof(expect), size);
49+
50+
/*
51+
* There are prompts and various ANSI characters in the output, so just
52+
* check that the string is in there somewhere.
53+
*/
54+
zassert_true(strstr(buf, expect),
55+
"Expected string to contain '%s', got '%s'", expect, buf);
56+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2024 Google, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __ZEPHYR_CUSTOM_SHELL_H
8+
#define __ZEPHYR_CUSTOM_SHELL_H
9+
10+
#define CUSTOM_SHELL_PREFIX "[CUSTOM_PREFIX]"
11+
12+
#undef shell_fprintf
13+
14+
#define shell_fprintf(sh, color, fmt, ...) \
15+
shell_fprintf_impl(sh, color, CUSTOM_SHELL_PREFIX fmt, ##__VA_ARGS__)
16+
17+
#endif /* __ZEPHYR_CUSTOM_SHELL_H */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
common:
2+
integration_platforms:
3+
- native_sim
4+
5+
tests:
6+
shell.shell_custom_header:
7+
tags:
8+
- shell_custom_header
9+
- shell

0 commit comments

Comments
 (0)