Files
prompts/test/update-policy.sh
T
sneak 9862d2b52a
check / check (push) Successful in 22s
update-policy: also fetch the style guides for the repo's languages (closes #182)
A go.mod adds the Go guide, plus the Go HTTP server conventions when a
tracked .go file imports net/http. A package.json adds the JS guide; a
pyproject.toml or setup.py adds the Python guide. Each goes to docs/.

Model: opus-5-5
2026-11-19 02:56:58 +00:00

136 lines
5.0 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. Like wget, it writes a
# new file without the execute bit, whatever the source's mode.
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
cat "$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 that is shorter than the new one. A run that rewrote the running
# script in place, instead of replacing it, would then read the new
# file's last lines as commands and fail.
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
grep -v '^# ' "$PROMPTS/script/update-policy" > script/update-policy
chmod +x script/update-policy
echo own > README.md
git add README.md script/update-policy
git commit -q -m initial
}
# committed_files_are: check that the last commit holds the listed files
# and the ones given, and no others.
committed_files_are() {
[ "$(git show --name-only --format= HEAD | sort)" = \
"$(echo "$LISTED $*" | tr ' ' '\n' | grep . | sort)" ] ||
fail "commit holds the wrong files"
}
new_repo "a first run, with no language files, commits exactly the listed files"
echo stray > stray.txt
echo staged >> README.md
git add README.md
script/update-policy > "$dir/output" 2>&1 || fail "exited non-zero"
[ "$(git rev-list --count HEAD)" = 2 ] || fail "made no single commit"
committed_files_are
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)" = "$(printf 'M README.md\n?? stray.txt')" ] ||
fail "changed files outside the list"
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 Go repo that uses net/http also gets the Go guide and HTTP conventions"
echo "module example.com/app" > go.mod
printf 'package main\n\nimport "net/http"\n' > main.go
git add go.mod main.go
git commit -q -m go
script/update-policy > "$dir/output" 2>&1 || fail "exited non-zero"
committed_files_are docs/CODE_STYLEGUIDE_GO.md docs/GO_HTTP_SERVER_CONVENTIONS.md
cmp -s docs/CODE_STYLEGUIDE_GO.md "$PROMPTS/prompts/CODE_STYLEGUIDE_GO.md" ||
fail "CODE_STYLEGUIDE_GO.md differs"
new_repo "a Go repo without net/http gets only the Go guide"
echo "module example.com/app" > go.mod
printf 'package main\n\nimport "fmt"\n' > main.go
git add go.mod main.go
git commit -q -m go
script/update-policy > "$dir/output" 2>&1 || fail "exited non-zero"
committed_files_are docs/CODE_STYLEGUIDE_GO.md
new_repo "a repo with package.json and pyproject.toml gets the JS and Python guides"
echo "{}" > package.json
echo "[project]" > pyproject.toml
git add package.json pyproject.toml
git commit -q -m js-and-python
script/update-policy > "$dir/output" 2>&1 || fail "exited non-zero"
committed_files_are docs/CODE_STYLEGUIDE_JS.md docs/CODE_STYLEGUIDE_PYTHON.md
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"