Files
prompts/test/update-policy.sh
T
sneak 47b35661a2
check / check (push) Successful in 13s
Replace script/update-policy with a short wget-and-commit script (closes #165)
The script fetches a fixed list of three files from the main branch of
prompts with wget: docs/REPO_POLICIES.md, docs/CODE_STYLEGUIDE.md and
itself. It moves them into place and commits them. It does not push. It
stops before anything is replaced when a fetch fails, when a listed file
has uncommitted changes, and when REPO_POLICIES.md is still at the repo
root. It runs under busybox.

The line-keeping rules, the hash argument and the push are gone. The
policies and checklists say so, and name docs/REPO_POLICIES.md as the
file's place.

Model: opus-5-5
2026-11-18 20:56:57 +00:00

97 lines
3.3 KiB
Bash
Executable File

#!/bin/sh
# test/update-policy.sh: test script/update-policy on throwaway repos. A
# stub wget serves the files from this checkout, or fails for the path
# in WGET_FAIL, so nothing touches the network.
set -eu
PROMPTS="$(cd "$(dirname "$0")/.." && pwd -P)"
LISTED="docs/REPO_POLICIES.md docs/CODE_STYLEGUIDE.md script/update-policy"
dir="$(mktemp -d)"
trap 'rm -rf "$dir"' EXIT
fail() {
echo "FAIL: $scenario: $*" >&2
cat "$dir/output" >&2
exit 1
}
mkdir "$dir/bin"
cat > "$dir/bin/wget" << EOF
#!/bin/sh
while [ "\$#" -gt 1 ]; do
[ "\$1" = "-O" ] && output="\$2"
shift
done
path="\${1#https://git.eeqj.de/sneak/prompts/raw/branch/main/}"
[ "\$path" != "\${WGET_FAIL:-}" ] || exit 1
cp "$PROMPTS/\$path" "\$output"
EOF
chmod +x "$dir/bin/wget"
PATH="$dir/bin:$PATH"
export PATH
# new_repo: a repo with one commit, holding only an older copy of the
# script, so that a run must replace the script that is running.
new_repo() {
scenario="$1"
rm -rf "$dir/repo"
git init -q "$dir/repo"
cd "$dir/repo"
git config user.name test
git config user.email test@example.com
mkdir script
cp "$PROMPTS/script/update-policy" script/
echo "# older copy" >> script/update-policy
echo own > README.md
git add README.md script/update-policy
git commit -q -m initial
}
new_repo "a first run writes and commits exactly the listed files"
echo stray > stray.txt
script/update-policy > "$dir/output" 2>&1 || fail "exited non-zero"
[ "$(git rev-list --count HEAD)" = 2 ] || fail "made no single commit"
[ "$(git show --name-only --format= HEAD | sort)" = \
"$(echo "$LISTED" | tr ' ' '\n' | sort)" ] || fail "commit holds other files"
cmp -s docs/REPO_POLICIES.md "$PROMPTS/prompts/REPO_POLICIES.md" ||
fail "REPO_POLICIES.md differs"
[ -x script/update-policy ] || fail "script not executable"
[ "$(git status --porcelain)" = "?? stray.txt" ] || fail "tree not clean"
scenario="a second run commits nothing"
script/update-policy > "$dir/output" 2>&1 || fail "exited non-zero"
[ "$(git rev-list --count HEAD)" = 2 ] || fail "made a commit"
grep -q "nothing to commit" "$dir/output" || fail "did not say so"
new_repo "a failed fetch writes nothing"
WGET_FAIL=prompts/CODE_STYLEGUIDE.md script/update-policy > "$dir/output" 2>&1 &&
fail "exited zero"
grep -q "cannot fetch" "$dir/output" || fail "did not say cannot fetch"
[ ! -e docs ] || fail "wrote docs/"
[ -z "$(git status --porcelain)" ] || fail "tree changed"
new_repo "an uncommitted change to a listed file stops the script"
mkdir docs
echo mine > docs/REPO_POLICIES.md
script/update-policy > "$dir/output" 2>&1 && fail "exited zero"
[ "$(cat docs/REPO_POLICIES.md)" = mine ] || fail "overwrote the change"
new_repo "a REPO_POLICIES.md at the root stops the script"
echo old > REPO_POLICIES.md
git add REPO_POLICIES.md
git commit -q -m "root policy file"
script/update-policy > "$dir/output" 2>&1 && fail "exited zero"
grep -q "git mv" "$dir/output" || fail "did not say to move it"
[ ! -e docs ] || fail "wrote docs/"
[ -z "$(git status --porcelain)" ] || fail "tree changed"
[ "$(git rev-list --count HEAD)" = 2 ] || fail "made a commit"
new_repo "the prompts repo itself is refused"
mkdir prompts
echo source > prompts/REPO_POLICIES.md
script/update-policy > "$dir/output" 2>&1 && fail "exited zero"
[ ! -e docs ] || fail "wrote docs/"
echo "update-policy tests passed"