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