93 lines
3.7 KiB
Bash
Executable File
93 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Unit tests for required-labels/check.sh. Runs on GitHub and Gitea.
|
|
#
|
|
set -euo pipefail
|
|
|
|
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
check="${here}/../required-labels/check.sh"
|
|
events="${here}/events"
|
|
|
|
failures=0
|
|
|
|
# run_case <name> <event-file> <expected-exit> <KEY=value...>
|
|
run_case() {
|
|
local name="$1" event="$2" expected="$3"
|
|
shift 3
|
|
local status=0
|
|
env GITHUB_EVENT_PATH="${events}/${event}" GITHUB_OUTPUT=/dev/null "$@" \
|
|
bash "$check" >/dev/null 2>&1 || status=$?
|
|
if [ "$status" -eq "$expected" ]; then
|
|
echo "ok - ${name}"
|
|
else
|
|
echo "FAIL - ${name} (expected exit ${expected}, got ${status})"
|
|
failures=$((failures + 1))
|
|
fi
|
|
}
|
|
|
|
# kredits regex, at-least-one
|
|
run_case "regex/any passes when a kredits label is present" \
|
|
with-kredits.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true RL_MODE=any
|
|
run_case "regex/any fails when no kredits label is present" \
|
|
without.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true RL_MODE=any
|
|
|
|
# opt-out
|
|
run_case "empty labels pass" without.json 0 RL_LABELS=''
|
|
run_case "disabled sentinel passes" without.json 0 RL_LABELS='disabled'
|
|
|
|
# all
|
|
run_case "all passes when every required label is present" \
|
|
with-kredits.json 0 RL_LABELS='bug,kredits-2' RL_MODE=all
|
|
run_case "all fails when a required label is missing" \
|
|
without.json 1 RL_LABELS='bug,kredits-2' RL_MODE=all
|
|
|
|
# exactly
|
|
run_case "exactly passes on the expected count" \
|
|
with-kredits.json 0 RL_LABELS='kredits-2,bug' RL_MODE=exactly RL_COUNT=2
|
|
run_case "exactly fails on a different count" \
|
|
with-kredits.json 1 RL_LABELS='kredits-2,bug' RL_MODE=exactly RL_COUNT=1
|
|
|
|
# exact (non-regex) matching, and multi-line input
|
|
run_case "exact match is case-insensitive" \
|
|
with-kredits.json 0 RL_LABELS='BUG' RL_MODE=all
|
|
run_case "multi-line labels are parsed" \
|
|
with-kredits.json 0 RL_LABELS=$'bug\nkredits-2' RL_MODE=all
|
|
|
|
# no labels on the PR
|
|
run_case "regex/any fails when the PR has no labels" \
|
|
no-labels.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
|
|
|
|
# Draft ("WIP") pull requests pass by default.
|
|
run_case "draft passes by default even without a required label" \
|
|
draft-without.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
|
|
run_case "draft with the label passes too" \
|
|
draft-with-kredits.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true
|
|
run_case "draft fails when draft-passes is false" \
|
|
draft-without.json 1 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true RL_DRAFT_PASSES=false
|
|
|
|
# API path: a file:// "API" returns the pull request. The event payload has no
|
|
# labels, so passing proves the API result was used.
|
|
api_dir="$(mktemp -d)"
|
|
mkdir -p "${api_dir}/repos/o/r/pulls" "${api_dir}/draft/repos/o/r/pulls"
|
|
printf '{"draft":false,"labels":[{"name":"kredits-3"},{"name":"bug"}]}' > "${api_dir}/repos/o/r/pulls/1"
|
|
printf '{"draft":true,"labels":[]}' > "${api_dir}/draft/repos/o/r/pulls/1"
|
|
run_case "api labels are used when available (payload has none)" \
|
|
no-labels.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true \
|
|
RL_API_URL="file://${api_dir}" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
|
|
run_case "api labels can also fail the check" \
|
|
no-labels.json 1 RL_LABELS='^feature-[0-9]$' RL_USE_REGEX=true \
|
|
RL_API_URL="file://${api_dir}" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
|
|
run_case "api draft state is honoured" \
|
|
no-labels.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true \
|
|
RL_API_URL="file://${api_dir}/draft" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
|
|
run_case "falls back to the payload when the API call fails" \
|
|
with-kredits.json 0 RL_LABELS='^kredits-[0-9]$' RL_USE_REGEX=true \
|
|
RL_API_URL="file://${api_dir}/does-not-exist" RL_REPOSITORY='o/r' RL_PR_NUMBER='1'
|
|
rm -rf "$api_dir"
|
|
|
|
if [ "$failures" -ne 0 ]; then
|
|
echo "${failures} test(s) failed"
|
|
exit 1
|
|
fi
|
|
echo "all tests passed"
|