Files
stack/packages/mosaic/framework/tools/git/test-pr-edit.sh
T
2026-08-12 14:41:29 -05:00

113 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regression harness for PR editing, including Gitea draft/ready and login binding.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="${MOSAIC_TEST_WORK_DIR:-$PWD/.mosaic-test-work/pr-edit}"
REPO_DIR="$WORK_DIR/repo"
BIN_DIR="$WORK_DIR/bin"
HOME_DIR="$WORK_DIR/home"
XDG_DIR="$WORK_DIR/xdg"
LOG_FILE="$WORK_DIR/calls.log"
rm -rf "$WORK_DIR"
mkdir -p "$REPO_DIR" "$BIN_DIR" "$HOME_DIR" "$XDG_DIR/tea"
git -C "$REPO_DIR" init -q
git -C "$REPO_DIR" remote add origin https://git.uscllc.com/USC/uconnect.git
git -C "$REPO_DIR" config mosaic.gitIdentity ""
cat > "$XDG_DIR/tea/config.yml" <<'YAML'
logins:
- name: usc
url: https://git.uscllc.com
token: fixture-usc-token
- name: mosaicstack
url: https://git.mosaicstack.dev
token: fixture-mosaic-token
YAML
cat > "$BIN_DIR/curl" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
printf 'curl' >> "$MOSAIC_TEST_LOG"
printf ' <%s>' "$@" >> "$MOSAIC_TEST_LOG"
printf '\n' >> "$MOSAIC_TEST_LOG"
printf '{"number":42,"draft":false}\n'
SH
cat > "$BIN_DIR/gh" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
printf 'gh' >> "$MOSAIC_TEST_LOG"
printf ' <%s>' "$@" >> "$MOSAIC_TEST_LOG"
printf '\n' >> "$MOSAIC_TEST_LOG"
SH
chmod +x "$BIN_DIR/curl" "$BIN_DIR/gh" "$SCRIPT_DIR/pr-edit.sh"
run_wrapper() {
(
cd "$REPO_DIR"
PATH="$BIN_DIR:$PATH" \
HOME="$HOME_DIR" \
XDG_CONFIG_HOME="$XDG_DIR" \
MOSAIC_TEST_LOG="$LOG_FILE" \
"$SCRIPT_DIR/pr-edit.sh" "$@"
)
}
: > "$LOG_FILE"
# shellcheck disable=SC2016 # literal backticks prove argument-array body safety.
run_wrapper -n 42 --login usc --title 'New title' --body 'Body with `literal` bytes' --base develop --draft >/dev/null
python3 - "$LOG_FILE" <<'PY'
import json
import pathlib
import sys
line = pathlib.Path(sys.argv[1]).read_text()
assert "-X> <PATCH>" in line, line
assert "Authorization: token fixture-usc-token" in line, line
assert "https://git.uscllc.com/api/v1/repos/USC/uconnect/pulls/42" in line, line
payload = line.split(" <-d> <", 1)[1].split("> <https://", 1)[0]
data = json.loads(payload)
assert data == {
"title": "New title",
"body": "Body with `literal` bytes",
"base": "develop",
"draft": True,
}, data
PY
: > "$LOG_FILE"
run_wrapper -n 42 --login usc --ready >/dev/null
grep -q '"draft": false' "$LOG_FILE"
: > "$LOG_FILE"
if run_wrapper -n 42 --login mosaicstack --draft >/dev/null 2>&1; then
echo "Expected a login bound to another host to fail closed" >&2
exit 1
fi
[[ ! -s "$LOG_FILE" ]] || { echo "Mismatched login unexpectedly reached curl" >&2; exit 1; }
if run_wrapper -n 42 --draft --ready >/dev/null 2>&1; then
echo "Expected --draft and --ready to be mutually exclusive" >&2
exit 1
fi
if run_wrapper -n 42 >/dev/null 2>&1; then
echo "Expected a no-op edit to fail" >&2
exit 1
fi
if ! run_wrapper --help 2>&1 | grep -q '^Usage:'; then
echo "Expected --help to exit zero and print usage" >&2
exit 1
fi
# GitHub uses gh's supported edit and ready/undo commands rather than raw defaults.
git -C "$REPO_DIR" remote set-url origin https://github.com/acme/widgets.git
: > "$LOG_FILE"
run_wrapper -n 7 --title 'GitHub title' --draft >/dev/null
grep -q 'gh <pr> <edit> <7> <--title> <GitHub title>' "$LOG_FILE"
grep -q 'gh <pr> <ready> <7> <--undo>' "$LOG_FILE"
echo "PR edit regression harness passed"