Files
cleanup-nuget/tests/entrypoint.sh
T

116 lines
2.8 KiB
Bash

#!/bin/bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT
assert_contains() {
local needle="$1"
local haystack="$2"
if ! grep -Fq "$needle" <<<"$haystack"; then
printf 'Expected output to contain: %s\n' "$needle" >&2
exit 1
fi
}
write_mock_curl() {
cat > "${TMP_DIR}/curl" <<'EOF'
#!/bin/sh
method=GET
out_file=
headers_file=
url=
while [ "$#" -gt 0 ]; do
case "$1" in
-X)
method=$2
shift 2
;;
-D)
headers_file=$2
shift 2
;;
-o)
out_file=$2
shift 2
;;
-w)
shift 2
;;
-H|-sS)
shift 1
;;
*)
url=$1
shift
;;
esac
done
status='HTTP/1.1 200 OK'
code='200'
body='[]'
case "$url" in
*'/api/v1/packages/test-owner?type=nuget&page=1&limit=100')
body='[{"name":"pkg-a","version":"1.0.0","created_at":"2023-01-01T00:00:00Z"},{"name":"pkg-a","version":"1.1.0","created_at":"2023-02-01T00:00:00Z"},{"name":"pkg-b","version":"0.1.0","created_at":"2023-03-01T00:00:00Z"}]'
;;
*'/api/v1/packages/test-owner/nuget/pkg-a/1.0.0')
if [ "$method" = "DELETE" ]; then
status='HTTP/1.1 204 No Content'
code='204'
body=''
fi
;;
esac
printf '%s' "$body" > "$out_file"
{
printf '%s\n' "$status"
printf 'X-Request-Id: req-123\n'
} > "$headers_file"
if [ "$method" = "DELETE" ] && [ -n "${MOCK_DELETE_FILE:-}" ]; then
printf '%s %s\n' "$method" "$url" >> "$MOCK_DELETE_FILE"
fi
printf '%s' "$code"
EOF
chmod +x "${TMP_DIR}/curl"
}
run_entrypoint() {
PATH="${TMP_DIR}:$PATH" \
GITEA_SERVER_URL="https://gitea.example.com" \
GITEA_REPOSITORY="test-owner/test-repo" \
env "$@" "${ROOT_DIR}/entrypoint.sh" 2>&1
}
write_mock_curl
output="$(run_entrypoint INPUT_RUNNER_TOKEN=abc INPUT_KEEP_VERSIONS=1)"
assert_contains "Using token from inputs.RUNNER_TOKEN" "$output"
assert_contains "keep_versions=1" "$output"
assert_contains "request_id=req-123" "$output"
assert_contains "Deleted package pkg-a version 1.0.0 -> 204 No Content" "$output"
output="$(run_entrypoint GITEA_TOKEN=secret INPUT_KEEP_VERSIONS=1)"
assert_contains "Using token from secrets.GITEA_TOKEN" "$output"
if run_entrypoint INPUT_KEEP_VERSIONS=-1 >/dev/null 2>&1; then
printf 'Expected invalid keep_versions to fail\n' >&2
exit 1
fi
if run_entrypoint >/dev/null 2>&1; then
printf 'Expected missing token to fail\n' >&2
exit 1
fi
delete_log="${TMP_DIR}/delete.log"
output="$(MOCK_DELETE_FILE="$delete_log" run_entrypoint INPUT_RUNNER_TOKEN=abc INPUT_KEEP_VERSIONS=1)"
assert_contains "Summary: packages=2 versions=3 kept=2 candidates=1 deleted=1 errors=0" "$output"
assert_contains "DELETE https://gitea.example.com/api/v1/packages/test-owner/nuget/pkg-a/1.0.0" "$(cat "$delete_log")"