|
| 1 | +#! /usr/bin/env bats |
| 2 | + |
| 3 | +load ../environment |
| 4 | + |
| 5 | +setup() { |
| 6 | + test_filter |
| 7 | + @go.create_test_go_script '. "$_GO_USE_MODULES" "prompt"' \ |
| 8 | + 'declare prompt="$1"' \ |
| 9 | + 'declare default="$2"' \ |
| 10 | + 'declare fail_msg="$3"' \ |
| 11 | + 'declare response="initial value"' \ |
| 12 | + 'declare result' \ |
| 13 | + '@go.prompt_for_safe_input 'response' "$prompt" "$default" "$fail_msg"' \ |
| 14 | + 'result="$?"' \ |
| 15 | + 'if [[ "$result" -eq "0" ]]; then' \ |
| 16 | + ' printf -- "%s\n" "$response"' \ |
| 17 | + 'fi' \ |
| 18 | + 'exit "$result"' |
| 19 | +} |
| 20 | + |
| 21 | +teardown() { |
| 22 | + @go.remove_test_go_rootdir |
| 23 | +} |
| 24 | + |
| 25 | +@test "$SUITE: error if variable not a valid identifier" { |
| 26 | + @go.create_test_go_script '. "$_GO_USE_MODULES" "prompt"' \ |
| 27 | + '@go.prompt_for_safe_input "invalid;"' |
| 28 | + |
| 29 | + run "$TEST_GO_SCRIPT" |
| 30 | + assert_failure |
| 31 | + |
| 32 | + local err_msg='Input prompt response variable name "invalid;" for ' |
| 33 | + err_msg+='@go.prompt_for_safe_input ' |
| 34 | + err_msg+='contains invalid identifier characters at:' |
| 35 | + |
| 36 | + assert_lines_match "^${err_msg}\$" \ |
| 37 | + "^ $TEST_GO_SCRIPT:[0-9] main$" |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +@test "$SUITE: reads value with no metacharacters or control operators" { |
| 42 | + run "$TEST_GO_SCRIPT" $'What is your quest?\n' <<<'To seek the grail!' |
| 43 | + assert_success 'What is your quest?' \ |
| 44 | + 'To seek the grail!' |
| 45 | +} |
| 46 | + |
| 47 | +@test "$SUITE: reads a value with an escaped metacharacter" { |
| 48 | + run "$TEST_GO_SCRIPT" $'What is your quest?\n' <<<"To seek the grail\;" |
| 49 | + assert_success 'What is your quest?' \ |
| 50 | + "To seek the grail\;" |
| 51 | +} |
| 52 | + |
| 53 | +@test "$SUITE: fails when reading a value with an unescaped metacharacter" { |
| 54 | + run "$TEST_GO_SCRIPT" $'What is your quest?\n' <<<'To seek the grail;' |
| 55 | + |
| 56 | + local err_msg='"To seek the grail;" is an invalid response, as it contains ' |
| 57 | + err_msg+='unescaped shell metacharacters or control operators.' |
| 58 | + |
| 59 | + assert_failure 'What is your quest?' \ |
| 60 | + "$err_msg" |
| 61 | +} |
| 62 | + |
| 63 | +@test "$SUITE: fails when the default value has an unescaped metacharacter" { |
| 64 | + run "$TEST_GO_SCRIPT" $'What is your quest?\n' 'To seek the grail;' <<<'' |
| 65 | + |
| 66 | + local err_msg='"To seek the grail;" is an invalid response, as it contains ' |
| 67 | + err_msg+='unescaped shell metacharacters or control operators.' |
| 68 | + |
| 69 | + assert_failure 'What is your quest? [default: To seek the grail;]' \ |
| 70 | + "$err_msg" |
| 71 | +} |
| 72 | + |
| 73 | +@test "$SUITE: fails when no default or input provided" { |
| 74 | + run "$TEST_GO_SCRIPT" $'What is your quest?\n' '' 'Auuuuuuuugh!' <<<'' |
| 75 | + assert_failure 'What is your quest?' \ |
| 76 | + 'Auuuuuuuugh!' |
| 77 | +} |
| 78 | + |
| 79 | +@test "$SUITE: --or-die exits when the value has an unescaped metacharacter" { |
| 80 | + @go.create_test_go_script '. "$_GO_USE_MODULES" "prompt"' \ |
| 81 | + 'declare response="initial value"' \ |
| 82 | + "@go.prompt_for_safe_input --or-die 'Quest response' 'response' \\" \ |
| 83 | + $' "What is your quest?\n"' |
| 84 | + |
| 85 | + run "$TEST_GO_SCRIPT" <<<'To seek the grail;' |
| 86 | + assert_failure |
| 87 | + |
| 88 | + local err_msg='^Quest response "To seek the grail;" for ' |
| 89 | + err_msg+='@go.prompt_for_safe_input contains ' |
| 90 | + err_msg+='unescaped shell metacharacters or control operators at:$' |
| 91 | + |
| 92 | + assert_lines_match 'What is your quest?' \ |
| 93 | + "$err_msg" \ |
| 94 | + " $TEST_GO_SCRIPT:[0-9] main" |
| 95 | +} |
0 commit comments