|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 5 | +tmp_dir="$(mktemp -d)" |
| 6 | +cleanup() { |
| 7 | + rm -rf "$tmp_dir" |
| 8 | +} |
| 9 | +trap cleanup EXIT |
| 10 | + |
| 11 | +mkdir -p "$tmp_dir/bin" |
| 12 | +counter_file="$tmp_dir/counter" |
| 13 | + |
| 14 | +cat > "$tmp_dir/bin/gh" <<'GH' |
| 15 | +#!/usr/bin/env bash |
| 16 | +set -euo pipefail |
| 17 | +
|
| 18 | +[[ "${1:-}" == "api" ]] || { echo "unexpected gh invocation: $*" >&2; exit 2; } |
| 19 | +count="$(cat "${TEST_GH_COUNTER:?}")" |
| 20 | +count=$((count + 1)) |
| 21 | +printf '%s' "$count" > "$TEST_GH_COUNTER" |
| 22 | +
|
| 23 | +case "${TEST_GH_MODE:?}" in |
| 24 | + recover) |
| 25 | + if ((count < 3)); then |
| 26 | + echo 'gh: HTTP 503' >&2 |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | + printf '{"tag_name":"codex-app-1.2.3"}\n' |
| 30 | + ;; |
| 31 | + persistent-503) |
| 32 | + echo 'gh: HTTP 503' >&2 |
| 33 | + exit 1 |
| 34 | + ;; |
| 35 | + not-found) |
| 36 | + echo 'gh: Not Found (HTTP 404)' >&2 |
| 37 | + exit 1 |
| 38 | + ;; |
| 39 | + release-assets) |
| 40 | + printf '{"assets":[{"name":"asset.bin","size":3,"digest":"sha256:abc"}]}\n' |
| 41 | + ;; |
| 42 | + *) |
| 43 | + echo "unexpected TEST_GH_MODE: $TEST_GH_MODE" >&2 |
| 44 | + exit 2 |
| 45 | + ;; |
| 46 | +esac |
| 47 | +GH |
| 48 | +chmod +x "$tmp_dir/bin/gh" |
| 49 | + |
| 50 | +source "$repo_root/scripts/github-api.sh" |
| 51 | + |
| 52 | +run_with_mode() { |
| 53 | + local mode="$1" |
| 54 | + shift |
| 55 | + |
| 56 | + printf '0' > "$counter_file" |
| 57 | + PATH="$tmp_dir/bin:$PATH" \ |
| 58 | + TEST_GH_MODE="$mode" \ |
| 59 | + TEST_GH_COUNTER="$counter_file" \ |
| 60 | + GITHUB_API_ATTEMPTS=3 \ |
| 61 | + GITHUB_API_RETRY_DELAY_SECONDS=0 \ |
| 62 | + "$@" |
| 63 | +} |
| 64 | + |
| 65 | +output="$(run_with_mode recover github_api_json_allow_404 'repos/{owner}/{repo}/releases/latest' 2>"$tmp_dir/recover.err")" |
| 66 | +[[ "$(jq -r .tag_name <<<"$output")" == "codex-app-1.2.3" ]] |
| 67 | +[[ "$(cat "$counter_file")" == "3" ]] |
| 68 | +[[ "$(grep -c 'retrying' "$tmp_dir/recover.err")" == "2" ]] |
| 69 | + |
| 70 | +set +e |
| 71 | +run_with_mode persistent-503 github_api_json_allow_404 'repos/{owner}/{repo}/releases/latest' >"$tmp_dir/503.out" 2>"$tmp_dir/503.err" |
| 72 | +status=$? |
| 73 | +set -e |
| 74 | +[[ "$status" -ne 0 && "$status" -ne "$GITHUB_API_NOT_FOUND_STATUS" ]] |
| 75 | +[[ "$(cat "$counter_file")" == "3" ]] |
| 76 | +[[ ! -s "$tmp_dir/503.out" ]] |
| 77 | +grep -Fq 'gh: HTTP 503' "$tmp_dir/503.err" |
| 78 | + |
| 79 | +set +e |
| 80 | +run_with_mode not-found github_api_json_allow_404 'repos/{owner}/{repo}/releases/latest' >"$tmp_dir/404.out" 2>"$tmp_dir/404.err" |
| 81 | +status=$? |
| 82 | +set -e |
| 83 | +[[ "$status" -eq "$GITHUB_API_NOT_FOUND_STATUS" ]] |
| 84 | +[[ "$(cat "$counter_file")" == "1" ]] |
| 85 | +[[ ! -s "$tmp_dir/404.out" ]] |
| 86 | + |
| 87 | +assets="$(run_with_mode release-assets github_release_assets_json_allow_404 codex-app-1.2.3)" |
| 88 | +[[ "$(jq -r '.[0].name' <<<"$assets")" == "asset.bin" ]] |
| 89 | + |
| 90 | +echo "GitHub API retry and 404 handling fixture PASS" |
0 commit comments