|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Checks that request-decision/test reports broken or missing cases as failures instead of passing or stopping early. |
| 3 | +# |
| 4 | +# Each check copies the test script into a temporary directory beside a stand-in decision that echoes its input, sets |
| 5 | +# up the cases for that check, runs the test script, and compares its exit status and output with what is expected. |
| 6 | +# The stand-in keeps these checks about the test script alone, whatever the real decision does. |
| 7 | +# |
| 8 | +# Exits non-zero if any check fails. Needs only bash and jq. |
| 9 | +set -euo pipefail |
| 10 | +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | +work="$(mktemp -d)" |
| 12 | +trap 'rm -rf -- "$work"' EXIT |
| 13 | + |
| 14 | +failed=0 |
| 15 | + |
| 16 | +# Makes a fresh copy of the test script, with the stand-in decision and an empty cases directory, and prints its path. |
| 17 | +new_copy() { |
| 18 | + local dir |
| 19 | + dir="$(mktemp -d "$work/copy.XXXXXX")" |
| 20 | + cp "$here/test" "$dir/" |
| 21 | + printf '#!/usr/bin/env bash\nexec jq --compact-output .\n' >"$dir/decide" |
| 22 | + chmod +x "$dir/decide" |
| 23 | + mkdir "$dir/cases" |
| 24 | + echo "$dir" |
| 25 | +} |
| 26 | + |
| 27 | +# Adds a case that passes to a copy. |
| 28 | +add_good_case() { |
| 29 | + mkdir "$1/cases/good" |
| 30 | + echo '{"event_name": "push", "event": {}}' >"$1/cases/good/input.json" |
| 31 | + "$1/decide" <"$1/cases/good/input.json" >"$1/cases/good/expected.json" |
| 32 | +} |
| 33 | + |
| 34 | +# check NAME COPY WANT_STATUS WANT_TEXT: runs the copy's test script and checks it exits with WANT_STATUS |
| 35 | +# ("zero" or "non-zero") and prints a line containing WANT_TEXT. |
| 36 | +check() { |
| 37 | + local name="$1" copy="$2" want_status="$3" want_text="$4" output status=0 |
| 38 | + output="$("$copy/test" 2>&1)" || status=$? |
| 39 | + local got_status=zero |
| 40 | + [[ "$status" -eq 0 ]] || got_status=non-zero |
| 41 | + if [[ "$got_status" == "$want_status" ]] && grep --quiet --fixed-strings -- "$want_text" <<<"$output"; then |
| 42 | + echo "ok $name" |
| 43 | + else |
| 44 | + echo "FAIL $name: wanted a $want_status exit and the text \"$want_text\", got a $got_status exit and:" |
| 45 | + sed 's/^/ /' <<<"$output" |
| 46 | + failed=$((failed + 1)) |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +copy="$(new_copy)" |
| 51 | +add_good_case "$copy" |
| 52 | +check "a passing case passes" "$copy" zero "1 cases, 0 failed" |
| 53 | + |
| 54 | +copy="$(new_copy)" |
| 55 | +check "no cases fails" "$copy" non-zero "no cases found" |
| 56 | + |
| 57 | +copy="$(new_copy)" |
| 58 | +add_good_case "$copy" |
| 59 | +mkdir "$copy/cases/missing-expected" |
| 60 | +cp "$copy/cases/good/input.json" "$copy/cases/missing-expected/" |
| 61 | +check "a missing expected.json is a failed case" "$copy" non-zero "2 cases, 1 failed" |
| 62 | + |
| 63 | +copy="$(new_copy)" |
| 64 | +add_good_case "$copy" |
| 65 | +mkdir "$copy/cases/invalid-expected" |
| 66 | +cp "$copy/cases/good/input.json" "$copy/cases/invalid-expected/" |
| 67 | +echo '{not json' >"$copy/cases/invalid-expected/expected.json" |
| 68 | +check "an invalid expected.json is a failed case" "$copy" non-zero "2 cases, 1 failed" |
| 69 | + |
| 70 | +copy="$(new_copy)" |
| 71 | +add_good_case "$copy" |
| 72 | +mkdir "$copy/cases/missing-input" |
| 73 | +cp "$copy/cases/good/expected.json" "$copy/cases/missing-input/" |
| 74 | +check "a missing input.json is a failed case" "$copy" non-zero "2 cases, 1 failed" |
| 75 | + |
| 76 | +[[ "$failed" -eq 0 ]] |
0 commit comments