]> Git Repo - J-u-boot.git/blob - test/hush/loop.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-u-boot.git] / test / hush / loop.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2021
4  * Francis Laniel, Amarula Solutions, [email protected]
5  */
6
7 #include <command.h>
8 #include <env_attr.h>
9 #include <test/hush.h>
10 #include <test/ut.h>
11 #include <asm/global_data.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 static int hush_test_for(struct unit_test_state *uts)
16 {
17         ut_assertok(run_command("for loop_i in foo bar quux quux; do echo $loop_i; done", 0));
18         ut_assert_nextline("foo");
19         ut_assert_nextline("bar");
20         ut_assert_nextline("quux");
21         ut_assert_nextline("quux");
22         ut_assert_console_end();
23
24         if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
25                 /* Reset local variable. */
26                 ut_assertok(run_command("loop_i=", 0));
27         } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
28                 puts("Beware: this test set local variable loop_i and it cannot be unset!\n");
29         }
30
31         return 0;
32 }
33 HUSH_TEST(hush_test_for, UTF_CONSOLE);
34
35 static int hush_test_while(struct unit_test_state *uts)
36 {
37         if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
38                 /*
39                  * Hush 2021 always returns 0 from while loop...
40                  * You can see code snippet near this line to have a better
41                  * understanding:
42                  * debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n");
43                  */
44                 ut_assertok(run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
45         } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
46                 /*
47                  * Exit status is that of test, so 1 since test is false to quit
48                  * the loop.
49                  */
50                 ut_asserteq(1, run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
51         }
52         ut_assert_nextline("bar");
53         ut_assert_console_end();
54
55         if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
56                 /* Reset local variable. */
57                 ut_assertok(run_command("loop_foo=", 0));
58         } else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
59                 puts("Beware: this test set local variable loop_foo and it cannot be unset!\n");
60         }
61
62         return 0;
63 }
64 HUSH_TEST(hush_test_while, UTF_CONSOLE);
65
66 static int hush_test_until(struct unit_test_state *uts)
67 {
68         env_set("loop_bar", "bar");
69
70         /*
71          * WARNING We have to use environment variable because it is not possible
72          * resetting local variable.
73          */
74         ut_assertok(run_command("until test -z \"$loop_bar\"; do echo quux; setenv loop_bar; done", 0));
75         ut_assert_nextline("quux");
76         ut_assert_console_end();
77
78         /*
79          * Loop normally resets foo environment variable, but we reset it here in
80          * case the test failed.
81          */
82         env_set("loop_bar", NULL);
83         return 0;
84 }
85 HUSH_TEST(hush_test_until, UTF_CONSOLE);
This page took 0.031454 seconds and 4 git commands to generate.