test(35): read-only consumers with a scratch modfile in check-phase35.sh

- consumers stage runs each core plugin with GOWORK=off, GOPROXY=off and
  -mod=mod -modfile=<scratch>/go.mod (its go.sum merged with the framework's),
  so the go-premailer requirement resolves without writing the sibling tree
- workspace_check takes the modfile flags; a sibling whose git status
  --porcelain prints anything before or after the runs is refused
- self-test: planted dirty repository and a modfile resolving the framework
  elsewhere
This commit is contained in:
Jakub Zych
2026-11-13 21:23:00 +01:00
parent 0e97113a4f
commit ae13e4dd41

View File

@@ -7,16 +7,26 @@
# Stages: # Stages:
# self-test the go test -json and vitest detectors on planted output, the # self-test the go test -json and vitest detectors on planted output, the
# marker rule on a planted repository, the additive comparison # marker rule on a planted repository, the additive comparison
# on planted packages, a missing consumer repository and the # on planted packages, a missing consumer repository, the
# docker NOT RUN rule on a faked host. # dirty-sibling refusal on a planted repository, workspace_check
# under a scratch modfile on a planted module that resolves the
# framework elsewhere, and the docker NOT RUN rule on a faked
# host.
# go go vet ./..., go test -short ./..., the Docker-free tests of # go go vet ./..., go test -short ./..., the Docker-free tests of
# the phase by required name (GO_TESTS) and the examples/hello # the phase by required name (GO_TESTS) and the examples/hello
# workspace module. # workspace module.
# docker Docker host only: go test ./... in the framework and the # docker Docker host only: go test ./... in the framework and the
# non-short tests by required name (DOCKER_TESTS). # non-short tests by required name (DOCKER_TESTS).
# consumers ../sm-user-plugin and ../sm-paymentgateway-plugin: the # consumers ../sm-user-plugin and ../sm-paymentgateway-plugin, read-only:
# framework resolves to this checkout, go vet, and the named # each runs with GOWORK=off and -mod=mod -modfile=<scratch>/go.mod,
# mail tests (without -short when Docker answers); read-only. # a scratch copy of its go.mod and of its go.sum merged with the
# framework's go.sum (so a requirement the framework added, such
# as go-premailer, resolves without writing the sibling) and
# GOPROXY=off (module cache only, no network). The framework
# must resolve to this checkout under that modfile; then go vet
# and the named mail tests (without -short when Docker answers).
# A sibling whose git status --porcelain prints anything, before
# or after the runs, is refused.
# admin npm run test (required vitest files ADMIN_FILES), # admin npm run test (required vitest files ADMIN_FILES),
# npm run test:browser (required browser files), typecheck, # npm run test:browser (required browser files), typecheck,
# scripts/check-admin-openapi.sh --check and # scripts/check-admin-openapi.sh --check and
@@ -343,11 +353,14 @@ require_dir() {
} }
} }
# workspace_check DIR refuses a repository that does not resolve the # workspace_check DIR [FLAGS] refuses a repository that does not resolve the
# framework to this checkout (no trust in local go.work files). # framework to this checkout (no trust in local go.work files). FLAGS (word
# split) carries the consumers stage's -mod=mod -modfile=<scratch>/go.mod, so
# the check reads the same module graph the vet and test runs use.
workspace_check() { workspace_check() {
local dir="$1" got rgot rwant local dir="$1" flags="${2:-}" got rgot rwant
got="$( (cd "$dir" && go list -m -f '{{if .Replace}}{{.Replace.Dir}}{{else}}{{.Dir}}{{end}}' git.golem15.com/golem15/summercms 2>/dev/null) || true)" # shellcheck disable=SC2086
got="$( (cd "$dir" && go list $flags -m -f '{{if .Replace}}{{.Replace.Dir}}{{else}}{{.Dir}}{{end}}' git.golem15.com/golem15/summercms 2>/dev/null) || true)"
rgot="$(realpath -e -- "$got" 2>/dev/null || true)" rgot="$(realpath -e -- "$got" 2>/dev/null || true)"
rwant="$(realpath -e -- "$ROOT" 2>/dev/null || true)" rwant="$(realpath -e -- "$ROOT" 2>/dev/null || true)"
if [[ -z "$got" || -z "$rgot" || "$rgot" != "$rwant" ]]; then if [[ -z "$got" || -z "$rgot" || "$rgot" != "$rwant" ]]; then
@@ -357,6 +370,43 @@ workspace_check() {
echo "workspace: $dir resolves the framework to $rwant" echo "workspace: $dir resolves the framework to $rwant"
} }
# consumer_modfile DIR NAME copies DIR's go.mod into $SCRATCH/modfile/NAME and
# writes next to it a go.sum holding DIR's go.sum plus this checkout's go.sum
# (the framework's requirements were verified when they were added here).
# Prints the scratch go.mod path. Nothing is written under DIR.
consumer_modfile() {
local dir="$1" name="$2" out
out="$SCRATCH/modfile/$name"
mkdir -p "$out"
cp -- "$dir/go.mod" "$out/go.mod"
{
if [[ -f "$dir/go.sum" ]]; then
cat -- "$dir/go.sum"
fi
if [[ -f "$ROOT/go.sum" ]]; then
cat -- "$ROOT/go.sum"
fi
} | LC_ALL=C sort -u >"$out/go.sum"
printf '%s\n' "$out/go.mod"
}
# sibling_clean DIR WHEN refuses a repository whose git status --porcelain
# prints anything (the consumers stage must leave sibling trees untouched).
sibling_clean() {
local dir="$1" when="$2" status
if ! status="$(git -c safe.directory='*' -C "$dir" status --porcelain 2>&1)"; then
printf '%s\n' "$status" >&2
echo "refuse: git status failed in $dir ($when)" >&2
return 1
fi
if [[ -n "$status" ]]; then
printf '%s\n' "$status" | head -n 20 >&2
echo "refuse: $dir is not clean $when the consumers stage (git status --porcelain)" >&2
return 1
fi
echo "clean: $dir ($when)"
}
# marker_hits DIR BASE prints file:line for every line added since BASE to # marker_hits DIR BASE prints file:line for every line added since BASE to
# a non-test Go file, a README or a docs page under modules/, cmd/, # a non-test Go file, a README or a docs page under modules/, cmd/,
# internal/ or docs/ (testdata excluded) that names a consuming application. # internal/ or docs/ (testdata excluded) that names a consuming application.
@@ -479,6 +529,11 @@ run_self_test() {
# A missing consumer repository fails the consumers stage. # A missing consumer repository fails the consumers stage.
expect_mode missing-consumers 1 "missing $tmp/none/sm-user-plugin" --consumers "PHASE35_SIBLINGS=$tmp/none" "USER_PLUGIN=$tmp/none/sm-user-plugin" "PAYMENTGATEWAY_PLUGIN=$tmp/none/sm-paymentgateway-plugin" expect_mode missing-consumers 1 "missing $tmp/none/sm-user-plugin" --consumers "PHASE35_SIBLINGS=$tmp/none" "USER_PLUGIN=$tmp/none/sm-user-plugin" "PAYMENTGATEWAY_PLUGIN=$tmp/none/sm-paymentgateway-plugin"
self_test_consumers "$tmp" || {
rm -rf "$tmp"
return 1
}
# The docker NOT RUN rule: a docker that does not answer. # The docker NOT RUN rule: a docker that does not answer.
mkdir -p "$tmp/bin" mkdir -p "$tmp/bin"
printf '#!/bin/sh\nexit 1\n' >"$tmp/bin/docker" printf '#!/bin/sh\nexit 1\n' >"$tmp/bin/docker"
@@ -491,6 +546,60 @@ run_self_test() {
echo "phase35 self-test passed" echo "phase35 self-test passed"
} }
# self_test_consumers TMP plants the consumers stage's read-only rules: a
# repository with an untracked file is refused by sibling_clean, and
# workspace_check reads the scratch modfile it is given (a planted consumer
# whose own go.mod resolves the framework to the expected checkout, and whose
# scratch modfile resolves it elsewhere, is refused only under the modfile).
self_test_consumers() {
local tmp="$1" repo out rc
repo="$tmp/sibling"
mkdir -p "$repo"
git -C "$repo" init -q
printf 'module example.test/sibling\n' >"$repo/go.mod"
git -C "$repo" add go.mod
git -C "$repo" -c user.name=t -c user.email=t@example.test commit -q -m base
if ! out="$(sibling_clean "$repo" after 2>&1)"; then
printf '%s\n' "$out" >&2
echo "refuse: self-test sibling_clean refused a clean repository" >&2
return 1
fi
printf 'scratch\n' >"$repo/untracked.txt"
rc=0
out="$(sibling_clean "$repo" after 2>&1)" || rc=$?
if [[ "$rc" -eq 0 ]] || ! grep -q "^refuse: $repo is not clean after" <<<"$out"; then
printf '%s\n' "$out" >&2
echo "refuse: self-test sibling_clean missed an untracked file (exit $rc)" >&2
return 1
fi
echo "PASS self-test dirty-sibling"
local fw="$tmp/framework" other="$tmp/elsewhere" consumer="$tmp/consumer" mod="$tmp/modfile/consumer"
mkdir -p "$fw" "$other" "$consumer" "$mod"
printf 'module git.golem15.com/golem15/summercms\n\ngo 1.21\n' >"$fw/go.mod"
printf 'module git.golem15.com/golem15/summercms\n\ngo 1.21\n' >"$other/go.mod"
printf 'module example.test/consumer\n\ngo 1.21\n\nrequire git.golem15.com/golem15/summercms v0.0.0\n\nreplace git.golem15.com/golem15/summercms => ../framework\n' >"$consumer/go.mod"
printf 'module example.test/consumer\n\ngo 1.21\n\nrequire git.golem15.com/golem15/summercms v0.0.0\n\nreplace git.golem15.com/golem15/summercms => ../elsewhere\n' >"$mod/go.mod"
: >"$mod/go.sum"
if ! out="$(ROOT="$fw" GOWORK=off GOPROXY=off GOFLAGS= GOTOOLCHAIN=local workspace_check "$consumer" "" 2>&1)"; then
printf '%s\n' "$out" >&2
echo "refuse: self-test workspace_check refused a consumer that resolves the framework to the expected checkout" >&2
return 1
fi
rc=0
out="$(ROOT="$fw" GOWORK=off GOPROXY=off GOFLAGS= GOTOOLCHAIN=local workspace_check "$consumer" "-mod=mod -modfile=$mod/go.mod" 2>&1)" || rc=$?
if [[ "$rc" -eq 0 ]] || ! grep -q "^refuse: $consumer resolves git.golem15.com/golem15/summercms to .*elsewhere" <<<"$out"; then
printf '%s\n' "$out" >&2
echo "refuse: self-test workspace_check under a scratch modfile accepted a framework that resolves elsewhere (exit $rc)" >&2
return 1
fi
if [[ -n "$(git -c safe.directory='*' -C "$repo" status --porcelain -- go.mod)" ]] || ! grep -q '=> ../framework' "$consumer/go.mod"; then
echo "refuse: self-test the modfile run wrote the planted consumer's go.mod" >&2
return 1
fi
echo "PASS self-test workspace-modfile"
}
# additive_case TOOL NAME WANT BASE_SRC HEAD_SRC runs a planted package pair # additive_case TOOL NAME WANT BASE_SRC HEAD_SRC runs a planted package pair
# through scripts/apisurface and additive_compare; WANT is the line the # through scripts/apisurface and additive_compare; WANT is the line the
# comparison must print ("removed or changed", "method added to existing # comparison must print ("removed or changed", "method added to existing
@@ -597,10 +706,24 @@ EOF
run_consumers() { run_consumers() {
require_dir "$USER_PLUGIN" "sm-user-plugin" require_dir "$USER_PLUGIN" "sm-user-plugin"
require_dir "$PAYMENTGATEWAY_PLUGIN" "sm-paymentgateway-plugin" require_dir "$PAYMENTGATEWAY_PLUGIN" "sm-paymentgateway-plugin"
local dir flags scratch_init
# Read-only: no workspace file, module cache only, scratch modfiles.
export GOWORK=off GOPROXY=off
local dir flags user_mod pg_mod mflags
for dir in "$USER_PLUGIN" "$PAYMENTGATEWAY_PLUGIN"; do for dir in "$USER_PLUGIN" "$PAYMENTGATEWAY_PLUGIN"; do
workspace_check "$dir" sibling_clean "$dir" before
go_vet "$dir" ./... done
user_mod="$(consumer_modfile "$USER_PLUGIN" sm-user-plugin)"
pg_mod="$(consumer_modfile "$PAYMENTGATEWAY_PLUGIN" sm-paymentgateway-plugin)"
for dir in "$USER_PLUGIN" "$PAYMENTGATEWAY_PLUGIN"; do
if [[ "$dir" == "$USER_PLUGIN" ]]; then
mflags="-mod=mod -modfile=$user_mod"
else
mflags="-mod=mod -modfile=$pg_mod"
fi
workspace_check "$dir" "$mflags"
# shellcheck disable=SC2086
go_vet "$dir" $mflags ./...
done done
# Without -short when Docker answers so Mailpit/Postgres suites run. # Without -short when Docker answers so Mailpit/Postgres suites run.
if docker_up; then if docker_up; then
@@ -611,10 +734,11 @@ run_consumers() {
fi fi
# Root packages only — ./... hits packages with zero matching tests # Root packages only — ./... hits packages with zero matching tests
# and detect_go refuses "no tests to run". # and detect_go refuses "no tests to run".
# shellcheck disable=SC2086 go_named "$USER_PLUGIN" . "$flags -mod=mod -modfile=$user_mod" "${USER_MAIL_TESTS[@]}"
go_named "$USER_PLUGIN" . "$flags" "${USER_MAIL_TESTS[@]}" go_named "$PAYMENTGATEWAY_PLUGIN" . "$flags -mod=mod -modfile=$pg_mod" "${PAYMENTGATEWAY_MAIL_TESTS[@]}"
# shellcheck disable=SC2086 for dir in "$USER_PLUGIN" "$PAYMENTGATEWAY_PLUGIN"; do
go_named "$PAYMENTGATEWAY_PLUGIN" . "$flags" "${PAYMENTGATEWAY_MAIL_TESTS[@]}" sibling_clean "$dir" after
done
echo "phase35 consumers passed" echo "phase35 consumers passed"
} }