blob: d734abf136d51db04ceb00c1b4b2d4b239684db7 [file] [log] [blame]
Francis Laniel432c9972023-12-22 22:02:26 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2021
4 * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
5 */
6
Francis Laniel432c9972023-12-22 22:02:26 +01007#include <command.h>
8#include <env_attr.h>
9#include <test/hush.h>
10#include <test/ut.h>
Francis Laniel2223c492023-12-22 22:02:41 +010011#include <asm/global_data.h>
12
13DECLARE_GLOBAL_DATA_PTR;
Francis Laniel432c9972023-12-22 22:02:26 +010014
15static int hush_test_for(struct unit_test_state *uts)
16{
17 console_record_reset_enable();
18
19 ut_assertok(run_command("for loop_i in foo bar quux quux; do echo $loop_i; done", 0));
20 ut_assert_nextline("foo");
21 ut_assert_nextline("bar");
22 ut_assert_nextline("quux");
23 ut_assert_nextline("quux");
24 ut_assert_console_end();
25
Francis Laniel2223c492023-12-22 22:02:41 +010026 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
27 /* Reset local variable. */
28 ut_assertok(run_command("loop_i=", 0));
29 } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
30 puts("Beware: this test set local variable loop_i and it cannot be unset!");
31 }
Francis Laniel432c9972023-12-22 22:02:26 +010032
33 return 0;
34}
35HUSH_TEST(hush_test_for, 0);
36
37static int hush_test_while(struct unit_test_state *uts)
38{
39 console_record_reset_enable();
40
Francis Laniel2223c492023-12-22 22:02:41 +010041 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
42 /*
43 * Hush 2021 always returns 0 from while loop...
44 * You can see code snippet near this line to have a better
45 * understanding:
46 * debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n");
47 */
48 ut_assertok(run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
49 } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
50 /*
51 * Exit status is that of test, so 1 since test is false to quit
52 * the loop.
53 */
54 ut_asserteq(1, run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
55 }
Francis Laniel432c9972023-12-22 22:02:26 +010056 ut_assert_nextline("bar");
57 ut_assert_console_end();
58
Francis Laniel2223c492023-12-22 22:02:41 +010059 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
60 /* Reset local variable. */
61 ut_assertok(run_command("loop_foo=", 0));
62 } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
63 puts("Beware: this test set local variable loop_foo and it cannot be unset!");
64 }
Francis Laniel432c9972023-12-22 22:02:26 +010065
66 return 0;
67}
68HUSH_TEST(hush_test_while, 0);
69
70static int hush_test_until(struct unit_test_state *uts)
71{
72 console_record_reset_enable();
73 env_set("loop_bar", "bar");
74
75 /*
76 * WARNING We have to use environment variable because it is not possible
77 * resetting local variable.
78 */
79 ut_assertok(run_command("until test -z \"$loop_bar\"; do echo quux; setenv loop_bar; done", 0));
80 ut_assert_nextline("quux");
81 ut_assert_console_end();
82
83 /*
84 * Loop normally resets foo environment variable, but we reset it here in
85 * case the test failed.
86 */
87 env_set("loop_bar", NULL);
88 return 0;
89}
90HUSH_TEST(hush_test_until, 0);