test_echo.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include <common.h>
  8. #include <command.h>
  9. #include <asm/global_data.h>
  10. #include <display_options.h>
  11. #include <test/lib.h>
  12. #include <test/test.h>
  13. #include <test/ut.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. struct test_data {
  16. char *cmd;
  17. char *expected;
  18. };
  19. static struct test_data echo_data[] = {
  20. {"echo 1 2 3",
  21. "1 2 3"},
  22. /* Test new line handling */
  23. {"echo -n 1 2 3; echo a b c",
  24. "1 2 3a b c"},
  25. /*
  26. * Test handling of environment variables.
  27. *
  28. * j, q, x are among the least frequent letters in English.
  29. * Hence no collision for the variable name jQx is expected.
  30. */
  31. {"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv jQx",
  32. "a) X b) ${jQx} c) X"},
  33. /* Test shell variable assignments without substitutions */
  34. {"foo=bar echo baz", "baz"},
  35. /* Test handling of shell variables. */
  36. {"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
  37. "1, 2, 3, "},
  38. };
  39. static int lib_test_hush_echo(struct unit_test_state *uts)
  40. {
  41. int i;
  42. for (i = 0; i < ARRAY_SIZE(echo_data); ++i) {
  43. ut_silence_console(uts);
  44. console_record_reset_enable();
  45. ut_assertok(run_command(echo_data[i].cmd, 0));
  46. ut_unsilence_console(uts);
  47. console_record_readline(uts->actual_str,
  48. sizeof(uts->actual_str));
  49. ut_asserteq_str(echo_data[i].expected, uts->actual_str);
  50. ut_assertok(ut_check_console_end(uts));
  51. }
  52. return 0;
  53. }
  54. LIB_TEST(lib_test_hush_echo, 0);