blob: cde74ebeb616228ce96a652da575019506fe0653 [file] [log] [blame]
Heinrich Schuchardt7a9814c2021-01-21 18:41:28 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for echo command
4 *
5 * Copyright 2020, Heinrich Schuchadt <xypron.glpk@gmx.de>
6 */
7
Heinrich Schuchardt7a9814c2021-01-21 18:41:28 +01008#include <command.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Heinrich Schuchardt7a9814c2021-01-21 18:41:28 +010010#include <display_options.h>
11#include <test/lib.h>
12#include <test/test.h>
13#include <test/ut.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17struct test_data {
18 char *cmd;
19 char *expected;
20};
21
22static struct test_data echo_data[] = {
23 {"echo 1 2 3",
24 "1 2 3"},
25 /* Test new line handling */
26 {"echo -n 1 2 3; echo a b c",
27 "1 2 3a b c"},
28 /*
29 * Test handling of environment variables.
30 *
31 * j, q, x are among the least frequent letters in English.
32 * Hence no collision for the variable name jQx is expected.
33 */
34 {"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv jQx",
35 "a) X b) ${jQx} c) X"},
Sean Anderson69d61a32021-02-28 16:29:51 -050036 /* Test shell variable assignments without substitutions */
37 {"foo=bar echo baz", "baz"},
Heinrich Schuchardt7a9814c2021-01-21 18:41:28 +010038 /* Test handling of shell variables. */
39 {"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
40 "1, 2, 3, "},
41};
42
43static int lib_test_hush_echo(struct unit_test_state *uts)
44{
45 int i;
46
47 for (i = 0; i < ARRAY_SIZE(echo_data); ++i) {
Simon Glass4d294a72021-03-15 18:11:13 +130048 ut_silence_console(uts);
Heinrich Schuchardt7a9814c2021-01-21 18:41:28 +010049 console_record_reset_enable();
50 ut_assertok(run_command(echo_data[i].cmd, 0));
Simon Glass4d294a72021-03-15 18:11:13 +130051 ut_unsilence_console(uts);
Heinrich Schuchardt7a9814c2021-01-21 18:41:28 +010052 console_record_readline(uts->actual_str,
53 sizeof(uts->actual_str));
54 ut_asserteq_str(echo_data[i].expected, uts->actual_str);
55 ut_assertok(ut_check_console_end(uts));
56 }
57 return 0;
58}
59
60LIB_TEST(lib_test_hush_echo, 0);