blob: a9b6a8edf245564fde2191ec35c9329151c418f2 [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{
Francis Laniel432c9972023-12-22 22:02:26 +010017 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
Francis Laniel2223c492023-12-22 22:02:41 +010024 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!");
29 }
Francis Laniel432c9972023-12-22 22:02:26 +010030
31 return 0;
32}
Simon Glassd7751962024-08-22 07:58:00 -060033HUSH_TEST(hush_test_for, UTF_CONSOLE);
Francis Laniel432c9972023-12-22 22:02:26 +010034
35static int hush_test_while(struct unit_test_state *uts)
36{
Francis Laniel2223c492023-12-22 22:02:41 +010037 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 }
Francis Laniel432c9972023-12-22 22:02:26 +010052 ut_assert_nextline("bar");
53 ut_assert_console_end();
54
Francis Laniel2223c492023-12-22 22:02:41 +010055 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!");
60 }
Francis Laniel432c9972023-12-22 22:02:26 +010061
62 return 0;
63}
Simon Glassd7751962024-08-22 07:58:00 -060064HUSH_TEST(hush_test_while, UTF_CONSOLE);
Francis Laniel432c9972023-12-22 22:02:26 +010065
66static int hush_test_until(struct unit_test_state *uts)
67{
Francis Laniel432c9972023-12-22 22:02:26 +010068 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}
Simon Glassd7751962024-08-22 07:58:00 -060085HUSH_TEST(hush_test_until, UTF_CONSOLE);