Files
steipete--codexbar/Scripts/test_ci_path_gate.sh
T
2026-07-13 12:22:33 +08:00

120 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT
assert_gate() {
local expected="$1"
local name="$2"
local paths_file="${tmp_dir}/${name}.paths"
local output_file="${tmp_dir}/${name}.output"
shift 2
printf '%s\n' "$@" > "$paths_file"
GITHUB_OUTPUT="$output_file" "${ROOT_DIR}/Scripts/ci_macos_test_gate.sh" "$paths_file" >/dev/null
local actual
actual="$(sed -n 's/^macos-tests=//p' "$output_file")"
if [[ "$actual" != "$expected" ]]; then
printf '%s: expected macos-tests=%s, got %s\n' "$name" "$expected" "${actual:-<empty>}" >&2
exit 1
fi
local reason
reason="$(sed -n 's/^macos-tests-reason=//p' "$output_file")"
if [[ -z "$reason" ]]; then
printf '%s: expected macos-tests-reason output\n' "$name" >&2
exit 1
fi
local path_count
path_count="$(sed -n 's/^changed-path-count=//p' "$output_file")"
if ! [[ "$path_count" =~ ^[0-9]+$ ]]; then
printf '%s: expected numeric changed-path-count output, got %s\n' \
"$name" "${path_count:-<empty>}" >&2
exit 1
fi
if [[ "$expected" == false && "$reason" != "docs/site-only changes covered by portable checks" ]]; then
printf '%s: expected docs/site skip reason, got %s\n' "$name" "$reason" >&2
exit 1
fi
}
assert_gate false docs-only $'M\tdocs/providers.md' $'M\tREADME.md'
assert_gate true configuration-doc $'M\tdocs/configuration.md'
assert_gate true rename-to-configuration-doc $'R100\tdocs/old.md\tdocs/configuration.md'
assert_gate true rename-from-configuration-doc $'R100\tdocs/configuration.md\tdocs/new.md'
assert_gate true agents-contract $'M\tAGENTS.md'
assert_gate true rename-to-agents-contract $'R100\tdocs/old.md\tAGENTS.md'
assert_gate true rename-from-agents-contract $'R100\tAGENTS.md\tdocs/new.md'
assert_gate true source $'M\tSources/CodexBar/App.swift'
assert_gate false docs-site $'M\tdocs/index.html' $'M\tdocs/site.css' $'M\tdocs/site.js' \
$'M\tdocs/site-locales.mjs' $'M\tdocs/social.html' $'M\tdocs/social.png' \
$'M\tdocs/CNAME' $'M\tdocs/.nojekyll' $'M\tdocs/llms.txt'
assert_gate false docs-site-assets $'M\tdocs/icon.png' $'M\tdocs/logos/provider-logo.svg'
assert_gate true docs-unknown-code $'M\tdocs/custom-tool.js'
assert_gate true docs-site-with-config $'M\tdocs/site.css' $'M\tdocs/configuration.md'
assert_gate true empty
assert_gate true source-to-docs $'R100\tSources/CodexBar/App.swift\tdocs/App.md'
assert_gate true docs-to-source $'R100\tdocs/App.md\tSources/CodexBar/App.swift'
assert_gate false docs-to-site $'R100\tdocs/old.md\tdocs/site.css'
assert_gate_fails() {
local name="$1"
local paths_file="${tmp_dir}/${name}.paths"
local output_file="${tmp_dir}/${name}.output"
shift
printf '%s\n' "$@" > "$paths_file"
if GITHUB_OUTPUT="$output_file" "${ROOT_DIR}/Scripts/ci_macos_test_gate.sh" "$paths_file" >/dev/null 2>&1; then
printf '%s: malformed gate input unexpectedly succeeded\n' "$name" >&2
exit 1
fi
if [[ -s "$output_file" ]]; then
printf '%s: malformed gate input emitted an output\n' "$name" >&2
exit 1
fi
}
assert_gate_fails missing-rename-target $'R100\tREADME.md'
assert_gate_fails extra-modified-path $'M\tREADME.md\tdocs/configuration.md'
assert_gate_fails missing-rename-score $'R\tREADME.md\tdocs/README.md'
assert_gate_fails invalid-rename-score $'Rfoo\tREADME.md\tdocs/README.md'
assert_gate_fails out-of-range-rename-score $'R101\tREADME.md\tdocs/README.md'
unterminated_paths="${tmp_dir}/unterminated.paths"
unterminated_output="${tmp_dir}/unterminated.output"
printf '%s' $'M\tREADME.md\tdocs/configuration.md' > "$unterminated_paths"
if GITHUB_OUTPUT="$unterminated_output" \
"${ROOT_DIR}/Scripts/ci_macos_test_gate.sh" "$unterminated_paths" >/dev/null 2>&1
then
printf 'unterminated malformed gate input unexpectedly succeeded\n' >&2
exit 1
fi
if [[ -s "$unterminated_output" ]]; then
printf 'unterminated malformed gate input emitted an output\n' >&2
exit 1
fi
verify="${ROOT_DIR}/Scripts/ci_verify_test_jobs.sh"
"$verify" success success true success >/dev/null
"$verify" success success false skipped >/dev/null
assert_verify_fails() {
if "$verify" "$@" >/dev/null 2>&1; then
printf 'unexpected aggregate success: %s\n' "$*" >&2
exit 1
fi
}
assert_verify_fails success success true skipped
assert_verify_fails success success false success
assert_verify_fails success success "" skipped
assert_verify_fails failure success true success
assert_verify_fails success failure true success
printf 'CI macOS path gate tests passed.\n'