Francis Laniel | 939fb4f | 2023-12-22 22:02:26 +0100 | [diff] [blame] | 1 | // 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> |
| 12 | |
| 13 | static int hush_test_for(struct unit_test_state *uts) |
| 14 | { |
| 15 | console_record_reset_enable(); |
| 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 | puts("Beware: this test set local variable loop_i and it cannot be unset!"); |
| 25 | |
| 26 | return 0; |
| 27 | } |
| 28 | HUSH_TEST(hush_test_for, 0); |
| 29 | |
| 30 | static int hush_test_while(struct unit_test_state *uts) |
| 31 | { |
| 32 | console_record_reset_enable(); |
| 33 | |
| 34 | /* Exit status is that of test, so 1 since test is false to quit the loop. */ |
| 35 | ut_asserteq(1, run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0)); |
| 36 | ut_assert_nextline("bar"); |
| 37 | ut_assert_console_end(); |
| 38 | |
| 39 | puts("Beware: this test set local variable loop_foo and it cannot be unset!"); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | HUSH_TEST(hush_test_while, 0); |
| 44 | |
| 45 | static int hush_test_until(struct unit_test_state *uts) |
| 46 | { |
| 47 | console_record_reset_enable(); |
| 48 | env_set("loop_bar", "bar"); |
| 49 | |
| 50 | /* |
| 51 | * WARNING We have to use environment variable because it is not possible |
| 52 | * resetting local variable. |
| 53 | */ |
| 54 | ut_assertok(run_command("until test -z \"$loop_bar\"; do echo quux; setenv loop_bar; done", 0)); |
| 55 | ut_assert_nextline("quux"); |
| 56 | ut_assert_console_end(); |
| 57 | |
| 58 | /* |
| 59 | * Loop normally resets foo environment variable, but we reset it here in |
| 60 | * case the test failed. |
| 61 | */ |
| 62 | env_set("loop_bar", NULL); |
| 63 | return 0; |
| 64 | } |
| 65 | HUSH_TEST(hush_test_until, 0); |