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