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