#!/usr/bin/env bash # Bash test harness for scripts/ci/sentry-release.sh. # # Approach: replace sentry-cli with a stub via SENTRY_CLI=... env and # assert the wrapper passes through the expected arguments to it. # Each test runs in its own temp dir. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" HELPER="$SCRIPT_DIR/sentry-release.sh" PASS=0 FAIL=0 ok() { echo " ok: $1" PASS=$((PASS + 1)) } fail() { echo " FAIL: $1" >&2 FAIL=$((FAIL + 1)) } run_test() { local name="$1" shift echo "test: $name" "$@" } make_stub() { local logfile="$1" local exitcode="${2:-0}" local stubdir stubdir="$(mktemp -d)" cat < "$stubdir/sentry-cli-stub" #!/usr/bin/env bash printf '%s\n' "\$*" >> "$logfile" exit $exitcode STUB chmod +x "$stubdir/sentry-cli-stub" echo "$stubdir/sentry-cli-stub" } assert_contains() { local logfile="$1" local needle="$2" if grep -Fq "$needle" "$logfile"; then ok "log contains: $needle" else fail "log missing: $needle (log: $(cat "$logfile" | tr '\n' '|'))" fi } assert_not_contains() { local logfile="$1" local needle="$2" if grep -Fq "$needle" "$logfile"; then fail "log unexpectedly contains: $needle" else ok "log absent: $needle" fi } # --------------------------------------------------------------- test_happy_path_with_sourcemaps() { local tmp tmp="$(mktemp -d)" local log="$tmp/calls.log" local stub stub="$(make_stub "$log")" local sourcemaps="$tmp/sm" mkdir -p "$sourcemaps" SENTRY_CLI="$stub" \ SENTRY_AUTH_TOKEN="t" SENTRY_ORG="sentry" SENTRY_URL="https://sentry.example" \ bash "$HELPER" \ --project boilerplate-web \ --environment production \ --release abc123 \ --sourcemaps "$sourcemaps" >/dev/null assert_contains "$log" "releases new abc123" assert_contains "$log" "sourcemaps upload --release abc123 $sourcemaps" assert_contains "$log" "releases set-commits abc123 --auto" assert_contains "$log" "releases finalize abc123" assert_contains "$log" "releases deploys abc123 new -e production" } # --------------------------------------------------------------- test_happy_path_without_sourcemaps() { local tmp tmp="$(mktemp -d)" local log="$tmp/calls.log" local stub stub="$(make_stub "$log")" SENTRY_CLI="$stub" \ SENTRY_AUTH_TOKEN="t" SENTRY_ORG="sentry" SENTRY_URL="https://sentry.example" \ bash "$HELPER" \ --project boilerplate-api \ --environment staging \ --release deadbeef \ --sourcemaps none >/dev/null assert_contains "$log" "releases new deadbeef" assert_not_contains "$log" "sourcemaps upload" assert_contains "$log" "releases set-commits deadbeef --auto" assert_contains "$log" "releases deploys deadbeef new -e staging" } # --------------------------------------------------------------- test_missing_env_var_fails() { local tmp tmp="$(mktemp -d)" local log="$tmp/calls.log" local stub stub="$(make_stub "$log")" set +e SENTRY_CLI="$stub" \ SENTRY_ORG="sentry" SENTRY_URL="https://sentry.example" \ bash "$HELPER" \ --project boilerplate-api \ --environment staging \ --release deadbeef \ --sourcemaps none >/dev/null 2>&1 local rc=$? set -e if [ "$rc" -ne 0 ]; then ok "exits non-zero when SENTRY_AUTH_TOKEN missing (rc=$rc)" else fail "expected non-zero exit when SENTRY_AUTH_TOKEN missing" fi } # --------------------------------------------------------------- test_missing_required_arg_fails() { local tmp tmp="$(mktemp -d)" local log="$tmp/calls.log" local stub stub="$(make_stub "$log")" set +e SENTRY_CLI="$stub" \ SENTRY_AUTH_TOKEN="t" SENTRY_ORG="sentry" SENTRY_URL="https://sentry.example" \ bash "$HELPER" \ --environment staging \ --release deadbeef \ --sourcemaps none >/dev/null 2>&1 local rc=$? set -e if [ "$rc" -ne 0 ]; then ok "exits non-zero when --project missing (rc=$rc)" else fail "expected non-zero exit when --project missing" fi } # --------------------------------------------------------------- test_missing_sourcemap_dir_fails() { local tmp tmp="$(mktemp -d)" local log="$tmp/calls.log" local stub stub="$(make_stub "$log")" set +e SENTRY_CLI="$stub" \ SENTRY_AUTH_TOKEN="t" SENTRY_ORG="sentry" SENTRY_URL="https://sentry.example" \ bash "$HELPER" \ --project boilerplate-web \ --environment production \ --release abc \ --sourcemaps "$tmp/does-not-exist" >/dev/null 2>&1 local rc=$? set -e if [ "$rc" -ne 0 ]; then ok "exits non-zero when sourcemaps dir missing (rc=$rc)" else fail "expected non-zero exit when sourcemaps dir missing" fi } run_test "happy-path with sourcemaps" test_happy_path_with_sourcemaps run_test "happy-path without sourcemaps" test_happy_path_without_sourcemaps run_test "missing env var fails" test_missing_env_var_fails run_test "missing required arg fails" test_missing_required_arg_fails run_test "missing sourcemap dir fails" test_missing_sourcemap_dir_fails echo echo "Summary: $PASS passed, $FAIL failed" if [ "$FAIL" -gt 0 ]; then exit 1 fi