blob: 80a9071a800f3301b5d5f98d93f1be5edbcb34fa [file] [log] [blame]
Francis Laniel939fb4f2023-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
7#include <common.h>
8#include <command.h>
9#include <env_attr.h>
10#include <test/hush.h>
11#include <test/ut.h>
Francis Laniel66cf0ac2023-12-22 22:02:41 +010012#include <asm/global_data.h>
13
14DECLARE_GLOBAL_DATA_PTR;
Francis Laniel939fb4f2023-12-22 22:02:26 +010015
16static int hush_test_for(struct unit_test_state *uts)
17{
18 console_record_reset_enable();
19
20 ut_assertok(run_command("for loop_i in foo bar quux quux; do echo $loop_i; done", 0));
21 ut_assert_nextline("foo");
22 ut_assert_nextline("bar");
23 ut_assert_nextline("quux");
24 ut_assert_nextline("quux");
25 ut_assert_console_end();
26
Francis Laniel66cf0ac2023-12-22 22:02:41 +010027 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
28 /* Reset local variable. */
29 ut_assertok(run_command("loop_i=", 0));
30 } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
31 puts("Beware: this test set local variable loop_i and it cannot be unset!");
32 }
Francis Laniel939fb4f2023-12-22 22:02:26 +010033
34 return 0;
35}
36HUSH_TEST(hush_test_for, 0);
37
38static int hush_test_while(struct unit_test_state *uts)
39{
40 console_record_reset_enable();
41
Francis Laniel66cf0ac2023-12-22 22:02:41 +010042 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
43 /*
44 * Hush 2021 always returns 0 from while loop...
45 * You can see code snippet near this line to have a better
46 * understanding:
47 * debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n");
48 */
49 ut_assertok(run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
50 } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
51 /*
52 * Exit status is that of test, so 1 since test is false to quit
53 * the loop.
54 */
55 ut_asserteq(1, run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
56 }
Francis Laniel939fb4f2023-12-22 22:02:26 +010057 ut_assert_nextline("bar");
58 ut_assert_console_end();
59
Francis Laniel66cf0ac2023-12-22 22:02:41 +010060 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
61 /* Reset local variable. */
62 ut_assertok(run_command("loop_foo=", 0));
63 } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
64 puts("Beware: this test set local variable loop_foo and it cannot be unset!");
65 }
Francis Laniel939fb4f2023-12-22 22:02:26 +010066
67 return 0;
68}
69HUSH_TEST(hush_test_while, 0);
70
71static int hush_test_until(struct unit_test_state *uts)
72{
73 console_record_reset_enable();
74 env_set("loop_bar", "bar");
75
76 /*
77 * WARNING We have to use environment variable because it is not possible
78 * resetting local variable.
79 */
80 ut_assertok(run_command("until test -z \"$loop_bar\"; do echo quux; setenv loop_bar; done", 0));
81 ut_assert_nextline("quux");
82 ut_assert_console_end();
83
84 /*
85 * Loop normally resets foo environment variable, but we reset it here in
86 * case the test failed.
87 */
88 env_set("loop_bar", NULL);
89 return 0;
90}
91HUSH_TEST(hush_test_until, 0);