diff --git a/scripts/check-phase35.sh b/scripts/check-phase35.sh new file mode 100755 index 00000000..53a1776f --- /dev/null +++ b/scripts/check-phase35.sh @@ -0,0 +1,787 @@ +#!/usr/bin/env bash +# check-phase35: fail-closed gate for Phase 35, mail templates and layouts +# editor: postcard overrides/partials/branding, cabana mail admin API, the +# admin SPA editor fields, and the sibling sm-user-plugin and +# sm-paymentgateway-plugin mail tests against this checkout. +# +# Stages: +# self-test the go test -json and vitest detectors on planted output, the +# marker rule on a planted repository, the additive comparison +# on planted packages, a missing consumer repository and the +# docker NOT RUN rule on a faked host. +# go go vet ./..., go test -short ./..., the Docker-free tests of +# the phase by required name (GO_TESTS) and the examples/hello +# workspace module. +# docker Docker host only: go test ./... in the framework and the +# non-short tests by required name (DOCKER_TESTS). +# consumers ../sm-user-plugin and ../sm-paymentgateway-plugin: the +# framework resolves to this checkout, go vet, and the named +# mail tests (without -short when Docker answers); read-only. +# admin npm run test (required vitest files ADMIN_FILES), +# npm run test:browser (required browser files), typecheck, +# scripts/check-admin-openapi.sh --check and +# scripts/check-admin-dist.sh. +# docs TestDocsTree, TestCommandsDocumented and summer docs:build +# --check. +# marker the lines this phase added since PHASE35_BASE_TAG to non-test +# Go files, READMEs and docs under modules/, cmd/, internal/ and +# docs/ (testdata excluded) name no consuming application. +# additive scripts/apisurface of every package under modules/ changed +# since PHASE35_BASE_TAG keeps every exported line of the tag +# and adds no method to an interface that exists at the tag +# (pact, postcard, cabana, lagoon/attach and surf only gain +# elements; this proves pact.HasMailTemplates, postcard.Mailer +# and Catalog.Register are unchanged). +# +# A Go stage exits non-zero on a failing command, or on a go test -json run +# that fails, skips a test (a subtest included), matches zero tests, prints +# "no tests to run" or misses a required name. A stage whose repository or +# directory does not exist fails with "missing ". +# +# Several stages may be given at once (--go --consumers ...). --all runs +# self-test, go, consumers, admin, docs, marker and additive, then docker +# when a Docker daemon answers. Without one docker is NOT RUN, which fails +# --all unless PHASE35_ALLOW_NO_DOCKER=1. +# +# Environment overrides: PHASE35_ROOT (this checkout), PHASE35_SIBLINGS (the +# directory holding the sibling repositories, default the parent of this +# checkout), PHASE35_BASE_TAG (default v0.6.3, the newest tag before the +# phase), PHASE35_ALLOW_NO_DOCKER. +set -euo pipefail + +unset FORCE_COLOR +export NO_COLOR=1 + +ROOT="${PHASE35_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" +SIBLINGS="${PHASE35_SIBLINGS:-$(dirname "$ROOT")}" +USER_PLUGIN="${USER_PLUGIN:-$SIBLINGS/sm-user-plugin}" +PAYMENTGATEWAY_PLUGIN="${PAYMENTGATEWAY_PLUGIN:-$SIBLINGS/sm-paymentgateway-plugin}" +PHASE35_BASE_TAG="${PHASE35_BASE_TAG:-v0.6.3}" + +# --------------------------------------------------------------------------- +# Required test names: :. + +# go stage: Docker-free framework tests (-short). +# Required-name list: only tests that never skip under -short. +# TestMailEditorSmoke / TestMailBrandSmoke still run via go_short ./...; +# their Postgres subtests skip there (go_short allows skips) and are +# required by name in DOCKER_TESTS. +GO_TESTS=( + ./modules/postcard:TestMailPartialsSmoke +) + +# docker stage: the non-short framework tests (Postgres). +DOCKER_TESTS=( + ./modules/postcard:TestMailOverrideSmoke + ./modules/postcard:TestMailEditorSmoke + ./modules/postcard:TestMailBrandSmoke + ./modules/cabana:TestMailScreensSmoke + ./modules/lagoon:TestMigrateStatusJSON +) + +# admin stage: vitest files that must pass. +ADMIN_FILES=(tests/form/mail-smoke.test.ts) + +# admin stage: browser vitest files that must pass. +ADMIN_BROWSER_FILES=(tests/browser/codeeditor-smoke.test.ts) + +# consumers: +# sm-user-plugin and sm-paymentgateway-plugin mail tests. +USER_MAIL_TESTS=(TestMail TestPolishMailSubjects) +PAYMENTGATEWAY_MAIL_TESTS=(TestMailCatalog TestMailTracer TestMailersMatrixSmoke) + +# marker stage: names of consuming applications (split so this file does +# not match itself). summercms.io, the framework's own site and default +# registry, is allowed as summercms""io pairing still matches "summercmsio". +APP_NAMES=("grzy""by" "fono""teka" "p[lł]yta""rium" "summercms""io" "sm-demo""-app" "sm-bm""-app") + +usage() { + cat >&2 <<'EOF' +usage: + check-phase35.sh --self-test + check-phase35.sh [--go] [--docker] [--consumers] [--admin] [--docs] [--marker] [--additive] + check-phase35.sh --all +EOF + exit 2 +} + +# detect_go reads go test -json. Exit 1 fail/build, 2 skip, 3 zero/no-tests, +# 4 non-JSON, 5 missing required name (REQUIRE_TESTS, space-separated). +detect_go() { + python3 - "$1" <<'PY' +import json, os, sys +path = sys.argv[1] +require = [n for n in os.environ.get("REQUIRE_TESTS", "").split() if n] +passed = set() +failed = [] +with open(path, encoding="utf-8", errors="replace") as fh: + for raw in fh: + line = raw.strip() + if not line.startswith("{"): + continue + try: + ev = json.loads(line) + except json.JSONDecodeError: + print("refuse: non-json test output", file=sys.stderr) + sys.exit(4) + action = ev.get("Action") + test = ev.get("Test") or "" + pkg = ev.get("Package") or ev.get("ImportPath") or "" + if action == "build-fail" or (action == "fail" and ev.get("FailedBuild")): + print(f"refuse: build failed {pkg}", file=sys.stderr) + sys.exit(1) + text = ev.get("Output") or "" + if action == "output" and "no tests to run" in text: + print(f"refuse: no tests to run in {pkg}", file=sys.stderr) + sys.exit(3) + if action == "skip" and test: + print(f"refuse: skipped {pkg} {test}", file=sys.stderr) + sys.exit(2) + if action == "fail": + failed.append(f"{pkg} {test}".strip()) + if action == "pass" and test: + passed.add(test) +if failed: + print("refuse: failed " + ", ".join(failed), file=sys.stderr) + sys.exit(1) +if not passed: + print("refuse: zero tests", file=sys.stderr) + sys.exit(3) +missing = [n for n in require if n not in passed] +if missing: + print("refuse: required tests did not pass: " + ", ".join(missing), file=sys.stderr) + sys.exit(5) +PY +} + +# detect_vitest reads a vitest run's output. Exit 1 failed, skipped or todo +# tests, 3 no all-passing summary, 5 a required file (REQUIRE_FILES) did +# not pass. +detect_vitest() { + python3 - "$1" <<'PY' +import os, re, sys +text = open(sys.argv[1], encoding="utf-8", errors="replace").read() +text = re.sub(r"\x1b\[[0-9;]*m", "", text) +need = os.environ.get("REQUIRE_FILES", "").split() +summary = re.findall(r"^\s*(Test Files|Tests)\s+(.*)$", text, re.M) +if any(re.search(r"\b(failed|skipped|todo)\b", rest) for _, rest in summary) or re.search(r"^\s*(FAIL|×)\s", text, re.M): + print("refuse: vitest reports failed, skipped or todo tests", file=sys.stderr) + sys.exit(1) +m = re.search(r"^\s*Tests\s+(\d+) passed \((\d+)\)\s*$", text, re.M) +if not m or m.group(1) != m.group(2) or m.group(1) == "0": + print("refuse: no all-passing vitest summary", file=sys.stderr) + sys.exit(3) +missing = [f for f in need if not re.search(r"^\s*✓ (?:\|\w+\| )?" + re.escape(f) + r" \(\d+ tests?\)", text, re.M)] +if missing: + print("refuse: did not pass: " + ", ".join(missing), file=sys.stderr) + sys.exit(5) +PY +} + +# detect_tap reads node --test TAP output. Exit 1 a "not ok" line or a +# non-zero fail, skipped or todo count, 3 no "# pass N" with N > 0. +detect_tap() { + python3 - "$1" <<'PY' +import re, sys +text = open(sys.argv[1], encoding="utf-8", errors="replace").read() +counts = dict((k, int(v)) for k, v in re.findall(r"^# (pass|fail|skipped|todo|cancelled) (\d+)\s*$", text, re.M)) +if re.search(r"^\s*not ok\b", text, re.M) or any(counts.get(k, 0) for k in ("fail", "skipped", "todo", "cancelled")): + print("refuse: TAP reports failed, skipped, todo or cancelled tests", file=sys.stderr) + sys.exit(1) +if counts.get("pass", 0) == 0: + print("refuse: TAP reports no passing test", file=sys.stderr) + sys.exit(3) +PY +} + +# detect_unittest reads python unittest output. Exit 1 a failure, error or +# skip, 3 no "Ran N tests" with N > 0 followed by OK. +detect_unittest() { + python3 - "$1" <<'PY' +import re, sys +text = open(sys.argv[1], encoding="utf-8", errors="replace").read() +if re.search(r"^FAILED\b", text, re.M) or re.search(r"skipped=\d+", text) or re.search(r"^(FAIL|ERROR):", text, re.M): + print("refuse: unittest reports failed, erroring or skipped tests", file=sys.stderr) + sys.exit(1) +m = re.search(r"^Ran (\d+) tests? in [0-9.]+s\s*\n\s*\nOK\s*$", text, re.M) +if not m or m.group(1) == "0": + print("refuse: no passing unittest run", file=sys.stderr) + sys.exit(3) +PY +} + +# go_json DIR ARGS... runs go test -json ARGS in DIR and checks the stream +# with detect_go. +go_json() { + local dir="$1" + shift + local log err rc=0 dc=0 + log="$(mktemp)" + err="$(mktemp)" + (cd "$dir" && go test -json "$@") >"$log" 2>"$err" || rc=$? + detect_go "$log" || dc=$? + if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then + cat "$err" >&2 || true + python3 -c 'import json,sys +for raw in open(sys.argv[1], errors="replace"): + try: ev = json.loads(raw) + except ValueError: continue + out = ev.get("Output", "") + if ev.get("Action") == "output" and ("FAIL" in out or "SKIP" in out or "_test.go:" in out): + sys.stderr.write(out)' "$log" 2>&1 | tail -n 40 >&2 || true + rm -f "$log" "$err" + echo "refuse: FAIL go test $* in $dir (test=$rc detect=$dc)" >&2 + return 1 + fi + echo "go: $(grep -c '"Action":"pass","Package":"[^"]*","Test":"[^"/]*","Elapsed"' "$log") top-level tests passed in $dir ($*)" + rm -f "$log" "$err" +} + +# go_named DIR PACKAGE FLAGS NAME... runs exactly the named top-level tests +# and requires every one to pass. FLAGS is word-split. +go_named() { + local dir="$1" pkg="$2" flags="$3" + shift 3 + if [[ "$#" -eq 0 ]]; then + echo "refuse: go_named $pkg in $dir without required names" >&2 + return 1 + fi + local names=("$@") regex + regex="^($(IFS='|'; echo "${names[*]}"))\$" + # shellcheck disable=SC2086 + REQUIRE_TESTS="${names[*]}" go_json "$dir" $flags -count=1 -run "$regex" "$pkg" +} + +# go_pkg_named DIR FLAGS ENTRY... groups : entries by +# package and runs go_named once per package. +go_pkg_named() { + local dir="$1" flags="$2" + shift 2 + local entry pkg name pkgs=() + declare -A by=() + for entry in "$@"; do + pkg="${entry%%:*}" + name="${entry#*:}" + if [[ -z "$pkg" || -z "$name" || "$pkg" == "$entry" ]]; then + echo "refuse: malformed required test entry $entry" >&2 + return 1 + fi + if [[ -z "${by[$pkg]+x}" ]]; then + pkgs+=("$pkg") + by[$pkg]="$name" + else + by[$pkg]="${by[$pkg]} $name" + fi + done + for pkg in "${pkgs[@]}"; do + # shellcheck disable=SC2086 + go_named "$dir" "$pkg" "$flags" ${by[$pkg]} + done +} + +# go_short DIR [SKIP] runs go test -short ./... in DIR, with go test -skip +# SKIP when given. +go_short() { + local dir="$1" skip="${2:-}" out args=(-count=1 -short) + if [[ -n "$skip" ]]; then + args+=(-skip "$skip") + fi + if ! out="$(cd "$dir" && go test "${args[@]}" ./... 2>&1)"; then + printf '%s\n' "$out" | grep -v '^ok ' | grep -E '^(FAIL|---|panic|\s+\S+_test\.go:)' | tail -n 60 >&2 || true + echo "refuse: FAIL go test -short ./... in $dir" >&2 + return 1 + fi + echo "short: $dir${skip:+ (skip $skip)}" +} + +go_vet() { + local dir="$1" + shift + (cd "$dir" && go vet "$@") + echo "vet: $dir ($*)" +} + +require_dir() { + [[ -d "$1" ]] || { + echo "refuse: missing $1 ($2)" >&2 + return 1 + } +} + +# workspace_check DIR refuses a repository that does not resolve the +# framework to this checkout (no trust in local go.work files). +workspace_check() { + local dir="$1" 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)" + rgot="$(realpath -e -- "$got" 2>/dev/null || true)" + rwant="$(realpath -e -- "$ROOT" 2>/dev/null || true)" + if [[ -z "$got" || -z "$rgot" || "$rgot" != "$rwant" ]]; then + echo "refuse: $dir resolves git.golem15.com/golem15/summercms to ${got:-}, not $ROOT" >&2 + return 1 + fi + echo "workspace: $dir resolves the framework to $rwant" +} + +# 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/, +# internal/ or docs/ (testdata excluded) that names a consuming application. +marker_hits() { + local dir="$1" base="$2" pattern + pattern="$(IFS='|'; echo "${APP_NAMES[*]}")" + git -C "$dir" diff -U0 "$base" HEAD -- \ + 'modules/*.go' 'modules/**/*.go' 'cmd/**/*.go' 'internal/**/*.go' \ + 'modules/**/README.md' 'cmd/**/README.md' 'internal/**/README.md' 'docs' \ + ':(exclude)*_test.go' ':(exclude)**/*_test.go' ':(exclude)**/testdata/**' | + PATTERN="$pattern" python3 -c ' +import os, re, sys +pat = re.compile(os.environ["PATTERN"], re.I) +path, line = "", 0 +for raw in sys.stdin: + if raw.startswith("+++ "): + path = raw[6:].rstrip("\n") if raw.startswith("+++ b/") else "" + continue + m = re.match(r"^@@ -\S+ \+(\d+)", raw) + if m: + line = int(m.group(1)) + continue + if raw.startswith("+") and not raw.startswith("+++"): + if path and pat.search(raw[1:]): + print(f"{path}:{line}: {raw[1:].strip()[:120]}") + line += 1 +' +} + +expect_detector() { + local detector="$1" name="$2" want="$3" payload="$4" log dc=0 + log="$(mktemp)" + printf '%s\n' "$payload" >"$log" + "$detector" "$log" >/dev/null 2>&1 || dc=$? + rm -f "$log" + if [[ "$dc" -ne "$want" ]]; then + echo "refuse: self-test $detector $name: exit $dc, want $want" >&2 + return 1 + fi + echo "PASS self-test $detector $name" +} + +# expect_mode NAME WANT_EXIT WANT_TEXT MODE ENV... runs this script in MODE +# with ENV and checks its exit status and output. +expect_mode() { + local name="$1" want="$2" text="$3" mode="$4" + shift 4 + local out rc=0 + out="$(env "$@" bash "${BASH_SOURCE[0]}" "$mode" 2>&1)" || rc=$? + if [[ "$rc" -ne "$want" ]] || ! grep -qF -- "$text" <<<"$out"; then + printf '%s\n' "$out" | tail -n 10 >&2 + echo "refuse: self-test $name: exit $rc (want $want), output must carry \"$text\"" >&2 + return 1 + fi + echo "PASS self-test $name" +} + + +run_self_test() { + bash -n "${BASH_SOURCE[0]}" + expect_detector detect_go pass 0 '{"Action":"pass","Package":"p","Test":"TestMailOverrideSmoke"}' + expect_detector detect_go fail 1 '{"Action":"pass","Package":"p","Test":"TestA"} +{"Action":"fail","Package":"p","Test":"TestMailOverrideSmoke"}' + expect_detector detect_go build 1 '{"Action":"build-fail","ImportPath":"p"}' + expect_detector detect_go skip 2 '{"Action":"pass","Package":"p","Test":"TestA"} +{"Action":"skip","Package":"p","Test":"TestMailOverrideSmoke"}' + expect_detector detect_go subtest-skip 2 '{"Action":"skip","Package":"p","Test":"TestMailOverrideSmoke/override"} +{"Action":"pass","Package":"p","Test":"TestMailOverrideSmoke"}' + expect_detector detect_go zero 3 '{"Action":"pass","Package":"p"}' + expect_detector detect_go no-tests 3 '{"Action":"output","Package":"p","Output":"testing: warning: no tests to run\n"} +{"Action":"pass","Package":"p"}' + expect_detector detect_go nonjson 4 '{"Action":"pass",' + REQUIRE_TESTS="TestMailPartialsSmoke TestMailEditorSmoke" expect_detector detect_go missing-named 5 '{"Action":"pass","Package":"p","Test":"TestMailPartialsSmoke"}' + REQUIRE_TESTS="TestMailEditorSmoke" expect_detector detect_go subtest-is-not-parent 5 '{"Action":"pass","Package":"p","Test":"TestOuter/TestMailEditorSmoke"} +{"Action":"pass","Package":"p","Test":"TestOther"}' + local ok=' ✓ tests/form/mail-smoke.test.ts (3 tests) 15ms + + Test Files 1 passed (1) + Tests 3 passed (3)' + REQUIRE_FILES="tests/form/mail-smoke.test.ts" expect_detector detect_vitest pass 0 "$ok" + REQUIRE_FILES="tests/form/Other.test.ts" expect_detector detect_vitest missing-file 5 "$ok" + expect_detector detect_vitest failed 1 ' Test Files 1 failed | 1 passed (2) + Tests 1 failed | 16 passed (17)' + expect_detector detect_vitest no-summary 3 ' ✓ tests/form/mail-smoke.test.ts (3 tests) 15ms' + + local tmp + tmp="$(mktemp -d)" + git -C "$tmp" init -q + mkdir -p "$tmp/modules/postcard/testdata" "$tmp/cmd/summer" "$tmp/docs" + printf 'package postcard\n\n// Old mentions fono''teka from before the phase.\nfunc Old() {}\n' >"$tmp/modules/postcard/old.go" + git -C "$tmp" add -A + git -C "$tmp" -c user.name=t -c user.email=t@example.test commit -q -m base + local base + base="$(git -C "$tmp" rev-parse HEAD)" + printf 'package postcard\n\n// Client asks the registry at summercms.io.\nfunc Client() {}\n' >"$tmp/modules/postcard/client.go" + printf 'package postcard\n\n// The grzy''by fixture.\nfunc helper() {}\n' >"$tmp/modules/postcard/client_test.go" + printf 'package fixture\n\n// sm-demo''-app fixture.\n' >"$tmp/modules/postcard/testdata/f.go" + git -C "$tmp" add -A + git -C "$tmp" -c user.name=t -c user.email=t@example.test commit -q -m clean + if [[ -n "$(marker_hits "$tmp" "$base")" ]]; then + rm -rf "$tmp" + echo "refuse: self-test marker_hits flagged a clean change, a test file or a fixture" >&2 + return 1 + fi + printf 'package postcard\n\n// Client asks the summercms''io app.\nfunc Client() {}\n' >"$tmp/modules/postcard/client.go" + printf 'package main\n\n// Used by p''łytarium.\nfunc x() {}\n' >"$tmp/cmd/summer/plugin.go" + printf '# Mail\n\nInstalled in sm-bm''-app.\n' >"$tmp/docs/mail.md" + git -C "$tmp" add -A + git -C "$tmp" -c user.name=t -c user.email=t@example.test commit -q -m dirty + local hits + hits="$(marker_hits "$tmp" "$base")" + if ! grep -q '^modules/postcard/client.go:3:' <<<"$hits" || ! grep -q '^cmd/summer/plugin.go:3:' <<<"$hits" || + ! grep -q '^docs/mail.md:3:' <<<"$hits"; then + rm -rf "$tmp" + echo "refuse: self-test marker_hits missed a planted name: $hits" >&2 + return 1 + fi + echo "PASS self-test marker_hits" + + # 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" + + # The docker NOT RUN rule: a docker that does not answer. + mkdir -p "$tmp/bin" + printf '#!/bin/sh\nexit 1\n' >"$tmp/bin/docker" + chmod +x "$tmp/bin/docker" + expect_mode docker-not-run 1 "NOT RUN docker" --docker-gate "PATH=$tmp/bin:$PATH" PHASE35_ALLOW_NO_DOCKER=0 + expect_mode docker-not-run-allowed 0 "NOT RUN docker" --docker-gate "PATH=$tmp/bin:$PATH" PHASE35_ALLOW_NO_DOCKER=1 + expect_mode docker-stage-needs-daemon 1 "needs a reachable Docker daemon" --docker "PATH=$tmp/bin:$PATH" + rm -rf "$tmp" + self_test_additive + echo "phase35 self-test passed" +} + +# 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 +# comparison must print ("removed or changed", "method added to existing +# interface" or "ok"). +additive_case() { + local tool="$1" name="$2" want="$3" dir out rc=0 ok=0 + dir="$(mktemp -d)" + mkdir -p "$dir/base" "$dir/head" + printf '%s\n' "$4" >"$dir/base/p.go" + printf '%s\n' "$5" >"$dir/head/p.go" + if api_surface "$tool" "$dir/base" "$dir/base.api" && api_surface "$tool" "$dir/head" "$dir/head.api"; then + out="$(additive_compare "selftest/$name" "$dir/base.api" "$dir/head.api" 2>&1)" || rc=$? + case "$want" in + ok) [[ "$rc" -eq 0 ]] && grep -q '^additive: ok selftest/' <<<"$out" && ok=1 ;; + *) [[ "$rc" -ne 0 ]] && grep -q "^additive: $want in selftest/$name: " <<<"$out" && ok=1 ;; + esac + fi + rm -rf -- "${dir:?}" + if [[ "$ok" -ne 1 ]]; then + printf '%s\n' "${out:-}" >&2 + echo "refuse: self-test additive $name (want $want, exit $rc)" + return 1 + fi + echo "PASS self-test additive $name" +} + +self_test_additive() { + local tmp tool failed=0 + tmp="$(mktemp -d)" + tool="$tmp/apisurface" + (cd "$ROOT" && go build -o "$tool" ./scripts/apisurface) + local p=$'package p\n\n' + additive_case "$tool" removed-func "removed or changed" "${p}func A() {}"$'\n\n'"func B() {}" "${p}func A() {}" || failed=1 + additive_case "$tool" changed-param "removed or changed" "${p}func F(n int) {}" "${p}func F(n int64) {}" || failed=1 + additive_case "$tool" removed-field "removed or changed" "${p}"$'type Info struct {\n\tID string\n\tVersion string\n}' "${p}"$'type Info struct {\n\tID string\n}' || failed=1 + additive_case "$tool" interface-method-added "method added to existing interface" \ + "${p}"$'type HasThing interface {\n\tThing() string\n}' \ + "${p}"$'type HasThing interface {\n\tThing() string\n\tOther() error\n}' || failed=1 + additive_case "$tool" func-added ok "${p}func A() {}" "${p}func A() {}"$'\n\n'"func B(s string) error { return nil }" || failed=1 + additive_case "$tool" struct-field-added ok \ + "${p}"$'type SettingsEntry struct {\n\tCode string `json:"code"`\n}' \ + "${p}"$'type SettingsEntry struct {\n\tCode string `json:"code"`\n\tPath string `json:"path,omitempty"`\n}' || failed=1 + rm -rf -- "${tmp:?}" + if [[ "$failed" -ne 0 ]]; then + return 1 + fi +} + +run_go() { + go_vet "$ROOT" ./... + go_short "$ROOT" + go_pkg_named "$ROOT" "-short" "${GO_TESTS[@]}" + require_dir "$ROOT/examples/hello" "examples/hello" + (cd "$ROOT/examples/hello" && go test -count=1 ./... >/dev/null) + echo "hello: examples/hello go test ./... ok" + echo "phase35 go passed" +} + +docker_up() { + command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1 +} + +run_docker() { + if ! docker_up; then + echo "refuse: the docker stage needs a reachable Docker daemon (testcontainers); run it on a Docker host (sudo -u golem when the socket is denied)" >&2 + return 1 + fi + local out + if ! out="$(cd "$ROOT" && go test -count=1 -timeout 30m ./... 2>&1)"; then + printf '%s\n' "$out" | grep -v '^ok ' | grep -E '^(FAIL|---|panic|\s+\S+_test\.go:)' | tail -n 60 >&2 || true + echo "refuse: FAIL go test ./... in $ROOT" >&2 + return 1 + fi + echo "full: $ROOT go test ./..." + go_pkg_named "$ROOT" "-timeout 20m" "${DOCKER_TESTS[@]}" + echo "phase35 docker passed" +} + +docker_gate() { + if docker_up; then + if bash "${BASH_SOURCE[0]}" --docker; then + echo "PASS docker" + return 0 + fi + echo "FAIL docker" + return 1 + fi + cat >&2 <<'EOF' +************************************************************************ +NOT RUN docker: no reachable Docker daemon on this host. +go test ./... of the framework and the Phase 35 Postgres suites did NOT +run. Run on a Docker host (through sudo -u golem when the socket is +denied): + bash scripts/check-phase35.sh --docker +************************************************************************ +EOF + echo "NOT RUN docker (Docker-host only; see the notice above)" + if [[ "${PHASE35_ALLOW_NO_DOCKER:-0}" != "1" ]]; then + echo "FAIL docker: no reachable Docker daemon; set PHASE35_ALLOW_NO_DOCKER=1 and run --docker on a Docker host" + return 1 + fi +} + +run_consumers() { + require_dir "$USER_PLUGIN" "sm-user-plugin" + require_dir "$PAYMENTGATEWAY_PLUGIN" "sm-paymentgateway-plugin" + local dir flags + for dir in "$USER_PLUGIN" "$PAYMENTGATEWAY_PLUGIN"; do + workspace_check "$dir" + go_vet "$dir" ./... + done + # Without -short when Docker answers so Mailpit/Postgres suites run. + if docker_up; then + flags="" + else + flags="-short" + echo "consumers: note Docker unavailable; running consumer mail tests with -short" + fi + # Root packages only — ./... hits packages with zero matching tests + # and detect_go refuses "no tests to run". + # shellcheck disable=SC2086 + go_named "$USER_PLUGIN" . "$flags" "${USER_MAIL_TESTS[@]}" + # shellcheck disable=SC2086 + go_named "$PAYMENTGATEWAY_PLUGIN" . "$flags" "${PAYMENTGATEWAY_MAIL_TESTS[@]}" + echo "phase35 consumers passed" +} + +run_admin() { + require_dir "$ROOT/admin" "admin" + local log rc=0 dc=0 + log="$(mktemp)" + npm --prefix "$ROOT/admin" run test >"$log" 2>&1 || rc=$? + REQUIRE_FILES="${ADMIN_FILES[*]}" detect_vitest "$log" || dc=$? + if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then + grep -E '(FAIL|×|Error|expected)' "$log" | head -n 40 >&2 || true + tail -n 20 "$log" >&2 + rm -f "$log" + echo "refuse: FAIL npm run test in admin (test=$rc detect=$dc)" >&2 + return 1 + fi + echo "vitest (admin): $(grep -E '^\s*Tests\s' "$log" | tr -s ' ')" + rm -f "$log" + log="$(mktemp)" + rc=0 + dc=0 + npm --prefix "$ROOT/admin" run test:browser >"$log" 2>&1 || rc=$? + REQUIRE_FILES="${ADMIN_BROWSER_FILES[*]}" detect_vitest "$log" || dc=$? + if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then + grep -E '(FAIL|×|Error|expected)' "$log" | head -n 40 >&2 || true + tail -n 20 "$log" >&2 + rm -f "$log" + echo "refuse: FAIL npm run test:browser in admin (test=$rc detect=$dc)" >&2 + return 1 + fi + echo "vitest browser (admin): $(grep -E '^\s*Tests\s' "$log" | tr -s ' ')" + rm -f "$log" + npm --prefix "$ROOT/admin" run typecheck >/dev/null + echo "typecheck: admin" + (cd "$ROOT" && bash scripts/check-admin-openapi.sh --check) + (cd "$ROOT" && bash scripts/check-admin-dist.sh) + echo "phase35 admin passed" +} + +run_docs() { + go_named "$ROOT" ./cmd/summer "" TestDocsTree TestCommandsDocumented + (cd "$ROOT" && go run ./cmd/summer docs:build --check) + echo "phase35 docs passed" +} + +run_marker() { + if ! git -C "$ROOT" rev-parse --verify -q "$PHASE35_BASE_TAG^{commit}" >/dev/null; then + echo "refuse: missing tag $PHASE35_BASE_TAG in $ROOT" >&2 + return 1 + fi + local hits + hits="$(marker_hits "$ROOT" "$PHASE35_BASE_TAG")" + if [[ -n "$hits" ]]; then + printf '%s\n' "$hits" | head -n 40 >&2 + echo "refuse: framework lines added since $PHASE35_BASE_TAG name a consuming application" >&2 + return 1 + fi + echo "marker: no line added since $PHASE35_BASE_TAG under modules/, cmd/, internal/ or docs/ names a consuming application" + echo "phase35 marker passed" +} + +# --------------------------------------------------------------------------- +# additive: the exported API of every package under modules/ changed since +# the base tag keeps every line of the tag. + +SCRATCH="" +CLEANUP_WORKTREES=() +scratch_cleanup() { + local entry + for entry in "${CLEANUP_WORKTREES[@]}"; do + git -c safe.directory='*' -C "${entry%%|*}" worktree remove --force "${entry#*|}" >/dev/null 2>&1 || true + git -c safe.directory='*' -C "${entry%%|*}" worktree prune >/dev/null 2>&1 || true + done + if [[ -n "$SCRATCH" && -d "$SCRATCH" ]]; then + chmod -R u+w -- "${SCRATCH:?}" 2>/dev/null || true + rm -rf -- "${SCRATCH:?}" + fi +} +scratch_init() { + SCRATCH="$(mktemp -d)" + trap scratch_cleanup EXIT +} + +add_worktree() { + git -c safe.directory='*' -C "$1" worktree add --detach -q "$2" "$3" + CLEANUP_WORKTREES+=("$1|$2") +} + +additive_compare() { + local pkg="$1" base="$2" head="$3" line name refused=0 added + while IFS= read -r line; do + [[ -n "$line" ]] || continue + echo "additive: removed or changed in $pkg: $line" >&2 + refused=1 + done < <(LC_ALL=C comm -23 "$base" "$head") + while IFS= read -r line; do + [[ "$line" =~ ^(imethod|iembed)\ ([A-Za-z0-9_]+)\. ]] || continue + name="${BASH_REMATCH[2]}" + if grep -qE "^type ${name}(\[.*\])? interface\$" "$base"; then + echo "additive: method added to existing interface in $pkg: $line" >&2 + refused=1 + fi + done < <(LC_ALL=C comm -13 "$base" "$head") + if [[ "$refused" -ne 0 ]]; then + return 1 + fi + added="$(LC_ALL=C comm -13 "$base" "$head" | grep -c . || true)" + echo "additive: ok $pkg (+$added lines)" +} + +api_surface() { + local tool="$1" dir="$2" out="$3" rc=0 + if [[ ! -d "$dir" ]]; then + : >"$out" + return 0 + fi + "$tool" "$dir" >"$out.raw" 2>"$out.err" || rc=$? + if [[ "$rc" -eq 2 ]]; then + : >"$out.raw" + elif [[ "$rc" -ne 0 ]]; then + cat "$out.err" >&2 + echo "refuse: apisurface $dir (exit $rc)" >&2 + return 1 + fi + LC_ALL=C sort "$out.raw" >"$out" +} + +run_additive() { + local dir name failed=0 pkgs=() base new=() + if ! git -C "$ROOT" rev-parse --verify -q "$PHASE35_BASE_TAG^{commit}" >/dev/null; then + echo "refuse: missing tag $PHASE35_BASE_TAG in $ROOT" >&2 + return 1 + fi + scratch_init + base="$SCRATCH/base" + add_worktree "$ROOT" "$base" "$PHASE35_BASE_TAG" + (cd "$ROOT" && go build -o "$SCRATCH/apisurface" ./scripts/apisurface) + mapfile -t pkgs < <(git -C "$ROOT" diff --name-only "$PHASE35_BASE_TAG" -- modules | + grep -E '\.go$' | grep -vE '_test\.go$' | grep -v '/testdata/' | xargs -r -n1 dirname | LC_ALL=C sort -u) + mkdir -p "$SCRATCH/api" + for dir in "${pkgs[@]}"; do + if [[ ! -d "$base/$dir" ]]; then + new+=("$dir") + continue + fi + name="${dir//\//_}" + api_surface "$SCRATCH/apisurface" "$base/$dir" "$SCRATCH/api/$name.base" + api_surface "$SCRATCH/apisurface" "$ROOT/$dir" "$SCRATCH/api/$name.head" + additive_compare "$dir" "$SCRATCH/api/$name.base" "$SCRATCH/api/$name.head" || failed=1 + done + if [[ "${#new[@]}" -gt 0 ]]; then + echo "additive: new since $PHASE35_BASE_TAG: ${new[*]}" + fi + if [[ "$failed" -ne 0 ]]; then + echo "refuse: additive: the framework API changed incompatibly since $PHASE35_BASE_TAG" >&2 + return 1 + fi + echo "phase35 additive passed ($((${#pkgs[@]} - ${#new[@]})) packages against $PHASE35_BASE_TAG)" +} + +run_all() { + local stage + for stage in self-test go consumers admin docs marker additive; do + if bash "${BASH_SOURCE[0]}" "--$stage"; then + echo "PASS $stage" + else + echo "FAIL $stage" + exit 1 + fi + done + docker_gate || exit 1 + echo "Phase 35 gate passed" +} + +run_stage() { + case "$1" in + --self-test | self-test) run_self_test ;; + --go | go) run_go ;; + --docker | docker) run_docker ;; + --docker-gate) docker_gate ;; + --consumers | consumers) run_consumers ;; + --admin | admin) run_admin ;; + --docs | docs) run_docs ;; + --marker | marker) run_marker ;; + --additive | additive) run_additive ;; + --all | all) run_all ;; + *) usage ;; + esac +} + +if [[ "$#" -eq 0 ]]; then + usage +fi +if [[ "$#" -eq 1 ]]; then + run_stage "$1" + exit 0 +fi +for arg in "$@"; do + if bash "${BASH_SOURCE[0]}" "$arg"; then + echo "PASS ${arg#--}" + else + echo "FAIL ${arg#--}" + exit 1 + fi +done