aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorFrancis Laniel2023-12-22 22:02:26 +0100
committerTom Rini2023-12-28 12:02:56 -0500
commit432c997eb3e686982263a671692d0aac7fa285d2 (patch)
tree8737597085c6d8ca8099697dbd1bcf93d4a715ae /test
parent2a70feaeeeae2530f597f69c1d79f26344ad1df8 (diff)
test: hush: Test hush loops
The added tests verifies correct behavior of for, while and until loops. Signed-off-by: Francis Laniel <francis.laniel@amarulasolutions.com> Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/hush/Makefile1
-rw-r--r--test/hush/loop.c65
2 files changed, 66 insertions, 0 deletions
diff --git a/test/hush/Makefile b/test/hush/Makefile
index ff4fe7700b6..a2d98815e50 100644
--- a/test/hush/Makefile
+++ b/test/hush/Makefile
@@ -7,3 +7,4 @@ obj-y += cmd_ut_hush.o
obj-y += if.o
obj-y += dollar.o
obj-y += list.o
+obj-y += loop.o
diff --git a/test/hush/loop.c b/test/hush/loop.c
new file mode 100644
index 00000000000..ca777e38fe6
--- /dev/null
+++ b/test/hush/loop.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * (C) Copyright 2021
+ * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
+ */
+
+#include <common.h>
+#include <command.h>
+#include <env_attr.h>
+#include <test/hush.h>
+#include <test/ut.h>
+
+static int hush_test_for(struct unit_test_state *uts)
+{
+ console_record_reset_enable();
+
+ ut_assertok(run_command("for loop_i in foo bar quux quux; do echo $loop_i; done", 0));
+ ut_assert_nextline("foo");
+ ut_assert_nextline("bar");
+ ut_assert_nextline("quux");
+ ut_assert_nextline("quux");
+ ut_assert_console_end();
+
+ puts("Beware: this test set local variable loop_i and it cannot be unset!");
+
+ return 0;
+}
+HUSH_TEST(hush_test_for, 0);
+
+static int hush_test_while(struct unit_test_state *uts)
+{
+ console_record_reset_enable();
+
+ /* Exit status is that of test, so 1 since test is false to quit the loop. */
+ ut_asserteq(1, run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
+ ut_assert_nextline("bar");
+ ut_assert_console_end();
+
+ puts("Beware: this test set local variable loop_foo and it cannot be unset!");
+
+ return 0;
+}
+HUSH_TEST(hush_test_while, 0);
+
+static int hush_test_until(struct unit_test_state *uts)
+{
+ console_record_reset_enable();
+ env_set("loop_bar", "bar");
+
+ /*
+ * WARNING We have to use environment variable because it is not possible
+ * resetting local variable.
+ */
+ ut_assertok(run_command("until test -z \"$loop_bar\"; do echo quux; setenv loop_bar; done", 0));
+ ut_assert_nextline("quux");
+ ut_assert_console_end();
+
+ /*
+ * Loop normally resets foo environment variable, but we reset it here in
+ * case the test failed.
+ */
+ env_set("loop_bar", NULL);
+ return 0;
+}
+HUSH_TEST(hush_test_until, 0);