Files
gotea/cgi/tests/run.sh
T
lotherk 17156e1d97 fix(pages): remove got worktree refs after publishing
Every got checkout creates refs/got/worktree/base-<uuid> in the bare repo. Delete it in the publisher trap so repeated pushes do not accumulate refs and block object cleanup, and assert the ref count stays stable in the hook test.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash
2026-11-22 12:32:06 +00:00

228 lines
7.7 KiB
Bash
Executable File

#!/bin/sh
# Tests for gotea-cgi. git is used only to create fixtures.
set -eu
here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cgi=$(dirname "$here")
bin="$cgi/gotea-cgi"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
fail() {
echo "FAIL: $*" >&2
exit 1
}
contains() {
printf '%s' "$1" | grep -Fq -- "$2"
}
expect_contains() {
contains "$1" "$2" || fail "$3 (missing: $2)"
}
expect_no() {
if contains "$1" "$2"; then
fail "$3 (found: $2)"
fi
}
run() {
env -i PATH=/usr/bin:/bin HOME="$tmp" \
GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \
SCRIPT_NAME=/cgi-bin/gotea REQUEST_METHOD="${2:-GET}" \
PATH_INFO="$1" "$bin"
}
run_palette() {
env -i PATH=/usr/bin:/bin HOME="$tmp" \
GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \
GOTEA_PALETTE="$2" SCRIPT_NAME=/cgi-bin/gotea \
REQUEST_METHOD=GET PATH_INFO="$1" "$bin"
}
make -C "$cgi" >/dev/null
mkdir -p "$tmp/repos/owner"
git init -q -b main "$tmp/src"
cd "$tmp/src"
printf 'hello gotea\n' > README.md
mkdir src
printf 'int main(void){return 0;}\n' > src/main.c
printf '\000\001\002' > src/logo.bin
git add .
GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \
GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \
git commit -qm 'initial import'
cd "$cgi"
hash=$(git -C "$tmp/src" rev-parse HEAD)
git clone -q --bare "$tmp/src" "$tmp/repos/owner/demo.git"
git clone -q --bare "$tmp/src" "$tmp/repos/flat.git"
git init -q --bare "$tmp/repos/owner/empty.git"
home=$(run /)
expect_contains "$home" 'DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' "home: HTML 4.01 doctype"
expect_contains "$home" 'GOTEA' "home: brand"
expect_contains "$home" 'owner/demo' "home: nested repository"
expect_contains "$home" 'flat' "home: flat repository"
expect_contains "$home" 'empty repository' "home: empty repository"
expect_no "$home" '<script' "home: no script tag"
expect_no "$home" 'javascript:' "home: no javascript URL"
echo "PASS home"
tree=$(run /repo/owner/demo/tree)
expect_contains "$tree" 'README.md' "tree: README"
expect_contains "$tree" '[src]' "tree: directory link"
expect_contains "$tree" '/repo/owner/demo/blob/README.md' "tree: blob link"
expect_contains "$tree" '[history]' "tree: history link"
echo "PASS tree"
sub=$(run /repo/owner/demo/tree/src)
expect_contains "$sub" 'main.c' "subtree: file"
expect_contains "$sub" 'logo.bin' "subtree: binary file"
echo "PASS subtree"
blob=$(run /repo/owner/demo/blob/README.md)
expect_contains "$blob" '<pre>hello gotea' "blob: content"
expect_contains "$blob" '/raw/README.md' "blob: raw link"
expect_contains "$blob" '[history]' "blob: history link"
echo "PASS blob"
raw=$(run /repo/owner/demo/raw/README.md)
expect_contains "$raw" 'Content-Type: text/plain; charset=UTF-8' "raw: content type"
expect_contains "$raw" 'hello gotea' "raw: content"
echo "PASS raw"
binblob=$(run /repo/owner/demo/blob/src/logo.bin)
expect_contains "$binblob" 'Binary file' "binary: notice"
echo "PASS binary"
log=$(run /repo/owner/demo/log)
expect_contains "$log" 'commit ' "log: commit line"
expect_contains "$log" 'initial import' "log: message"
expect_contains "$log" "$hash" "log: full hash"
echo "PASS log"
commit=$(run "/repo/owner/demo/commit/$hash")
expect_contains "$commit" 'initial import' "commit: message"
expect_contains "$commit" 'diff ' "commit: patch"
echo "PASS commit"
notfound=$(run /repo/nope/tree)
expect_contains "$notfound" 'Status: 404' "missing repo: 404"
echo "PASS 404"
missingfile=$(run /repo/owner/demo/blob/nope.txt)
expect_contains "$missingfile" 'Status: 404' "missing file: 404"
echo "PASS missing file"
badpath=$(run /repo/../tree)
expect_contains "$badpath" 'Status: 400' "traversal: 400"
echo "PASS traversal"
head=$(run /repo/owner/demo/tree HEAD)
expect_contains "$head" 'Content-Type: text/html; charset=UTF-8' "HEAD: headers"
expect_no "$head" '<!DOCTYPE' "HEAD: no body"
echo "PASS HEAD"
printf 'base = #123456\n' > "$tmp/pal.palette"
pal=$(run_palette / "$tmp/pal.palette")
expect_contains "$pal" '#123456' "palette: custom base colour"
expect_contains "$pal" 'bgcolor="#123456"' "palette: body fallback"
echo "PASS palette"
flat=$(run /repo/flat/tree)
expect_contains "$flat" 'README.md' "flat: tree"
echo "PASS flat"
bb="$cgi/../third_party/bb/bb"
"$bb" init "$tmp/bb-src" >/dev/null
cd "$tmp/bb-src"
rm -rf dist build
git init -q -b main
git add .
GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \
GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \
git commit -qm 'pages site'
git branch pages
git clone -q --bare . "$tmp/repos/owner/site.git"
bb_rev=$(git rev-parse pages)
mkdir -p "$tmp/pages"
printf 'owner/site\n' > "$tmp/allow"
refs_before=$(got ref -l -r "$tmp/repos/owner/site.git" |
grep -c 'refs/got/worktree' || true)
GOTEA_PAGES_ROOT="$tmp/pages" GOTEA_BB="$bb" \
"$cgi/pages/publish.sh" "$tmp/repos/owner/site.git" owner/site \
"$bb_rev" >/dev/null
[ -f "$tmp/pages/owner/site/index.html" ] ||
fail "publish: no index.html"
expect_contains "$(cat "$tmp/pages/owner/site/index.html")" \
'<!DOCTYPE HTML PUBLIC' "publish: bb output"
refs_after=$(got ref -l -r "$tmp/repos/owner/site.git" |
grep -c 'refs/got/worktree' || true)
[ "$refs_before" = "$refs_after" ] ||
fail "publish: worktree ref leaked ($refs_before -> $refs_after)"
echo "PASS publish"
pages_run() {
env -i PATH=/usr/bin:/bin HOME="$tmp" \
GOTEA_PAGES_HOST=pages.test HTTP_HOST="${2:-pages.test}" \
GOTEA_PAGES_ROOT="$tmp/pages" GOTEA_PAGES_ALLOW="$tmp/allow" \
SCRIPT_NAME=/cgi-bin/gotea REQUEST_METHOD=GET PATH_INFO="$1" "$bin"
}
pages_home=$(pages_run /)
expect_contains "$pages_home" 'GOTEA PAGES' "pages: index banner"
expect_contains "$pages_home" 'owner/site' "pages: allowlisted site"
echo "PASS pages index"
pages_site=$(pages_run /owner/site/)
expect_contains "$pages_site" 'Content-Type: text/html; charset=UTF-8' \
"pages: html content type"
expect_contains "$pages_site" '<!DOCTYPE HTML PUBLIC' "pages: site html"
echo "PASS pages site"
pages_css=$(pages_run /owner/site/css/style.css)
expect_contains "$pages_css" 'Content-Type: text/css; charset=UTF-8' \
"pages: css content type"
echo "PASS pages css"
pages_redirect=$(pages_run /owner/site)
expect_contains "$pages_redirect" 'Status: 301 Moved Permanently' \
"pages: trailing slash redirect"
expect_contains "$pages_redirect" 'Location: /cgi-bin/gotea/owner/site/' \
"pages: redirect target"
echo "PASS pages redirect"
pages_unknown=$(pages_run /owner/nope/)
expect_contains "$pages_unknown" 'Status: 404' "pages: unknown site"
echo "PASS pages 404"
pages_host=$(pages_run / wrong.test)
expect_contains "$pages_host" 'Status: 404' "pages: host isolation"
echo "PASS pages host"
pages_rootmount=$(env -i PATH=/usr/bin:/bin HOME="$tmp" \
GOTEA_PAGES_HOST=pages.test HTTP_HOST=pages.test \
GOTEA_PAGES_ROOT="$tmp/pages" GOTEA_PAGES_ALLOW="$tmp/allow" \
SCRIPT_NAME=/ REQUEST_METHOD=GET PATH_INFO=/ "$bin")
expect_contains "$pages_rootmount" 'href="/owner/site/"' \
"pages: root mount links"
expect_no "$pages_rootmount" 'href="//' "pages: no scheme-relative links"
echo "PASS pages root mount"
"$cgi/pages/install-hook.sh" "$tmp/repos/owner/site.git" "$tmp/pages" \
"$tmp/allow" "$bb" pages >/dev/null
hookout=$(printf '%040d %s refs/heads/pages\n' 0 "$bb_rev" |
env -i PATH=/usr/bin:/bin GIT_DIR="$tmp/repos/owner/site.git" \
"$tmp/repos/owner/site.git/hooks/post-receive.d/gotea-pages" 2>&1)
expect_contains "$hookout" "published owner/site" "hook: publish output"
refs_hook=$(got ref -l -r "$tmp/repos/owner/site.git" |
grep -c 'refs/got/worktree' || true)
[ "$refs_before" = "$refs_hook" ] ||
fail "hook: worktree ref leaked ($refs_before -> $refs_hook)"
echo "PASS post-receive hook"
echo "all gotea-cgi tests passed"