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 <command.h> 8#include <env_attr.h> 9#include <test/hush.h> 10#include <test/ut.h> 11#include <asm/global_data.h> 12 13DECLARE_GLOBAL_DATA_PTR; 14 15static 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 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 } 32 33 return 0; 34} 35HUSH_TEST(hush_test_for, 0); 36 37static int hush_test_while(struct unit_test_state *uts) 38{ 39 console_record_reset_enable(); 40 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 } 56 ut_assert_nextline("bar"); 57 ut_assert_console_end(); 58 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 } 65 66 return 0; 67} 68HUSH_TEST(hush_test_while, 0); 69 70static 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} 90HUSH_TEST(hush_test_until, 0); 91