Remove superseded release scripts and tests

This commit is contained in:
2026-11-23 18:15:10 +00:00
parent ef4335cda3
commit b031d35af7
7 changed files with 4 additions and 1046 deletions

View File

@@ -234,7 +234,10 @@ integration surface without recreating ReleaseKit's suite.
## Stage 5 — Remove the superseded release implementation
Status: pending.
Status: completed. Removed the four release scripts and their dedicated test
package; no active code or workflow callers remain. Verified focused app and
doccheck tests and `go build ./...` with Go 1.27.4. Current maintainer prose is
updated in the next stage.
Read all four scripts and `internal/releasecheck/` to identify references and
fixtures exclusively owned by that implementation. Search code through the

View File

@@ -1,474 +0,0 @@
package releasecheck
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
)
func TestReleaseVersionValidationIsExact(t *testing.T) {
repoRoot := releaseCheckRepoRoot(t)
library := filepath.Join(repoRoot, "scripts", "release-lib.sh")
workingDirectory := t.TempDir()
if err := os.WriteFile(filepath.Join(workingDirectory, "2"), nil, 0o644); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
version string
valid bool
}{
{version: "v0.0.0", valid: true},
{version: "v1.2.3", valid: true},
{version: "v10.200.3000", valid: true},
{version: "1.2.3"},
{version: "v01.2.3"},
{version: "v1.02.3"},
{version: "v1.2.03"},
{version: "v1.2.3."},
{version: "v1.2.3-rc.1"},
{version: "v1.2.3+build"},
{version: "v1.*.3"},
{version: "v1.2.3\nv4.5.6"},
} {
t.Run(test.version, func(t *testing.T) {
command := exec.Command("sh", "-c", `. "$1"; narratio_release_validate_version "$2"`, "release-version-test", library, test.version)
command.Dir = workingDirectory
err := command.Run()
if test.valid && err != nil {
t.Fatalf("valid version %q rejected: %v", test.version, err)
}
if !test.valid && err == nil {
t.Fatalf("invalid version %q accepted", test.version)
}
})
}
}
func TestAssetBuilderRejectsUnsafeDestinations(t *testing.T) {
repoRoot := releaseCheckRepoRoot(t)
builder := filepath.Join(repoRoot, "scripts", "build-release-assets.sh")
nonempty := t.TempDir()
if err := os.WriteFile(filepath.Join(nonempty, "existing"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
notDirectory := filepath.Join(t.TempDir(), "not-a-directory")
if err := os.WriteFile(notDirectory, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
linkTarget := t.TempDir()
symlink := filepath.Join(t.TempDir(), "linked-output")
if err := os.Symlink(linkTarget, symlink); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{
nil,
{"v1.2", t.TempDir()},
{"1.2.3", t.TempDir()},
{"v01.2.3", t.TempDir()},
{"v1.02.3", t.TempDir()},
{"v1.2.03", t.TempDir()},
{"v1.2.3-rc.1", t.TempDir()},
{"v1.2.3+build", t.TempDir()},
{"v1.2.3", "relative-output"},
{"v1.2.3", "/"},
{"v1.2.3", repoRoot},
{"v1.2.3", nonempty},
{"v1.2.3", notDirectory},
{"v1.2.3", symlink},
} {
command := exec.Command("sh", append([]string{builder}, args...)...)
command.Dir = t.TempDir()
output, err := command.CombinedOutput()
if err == nil {
t.Fatalf("asset builder %q succeeded", args)
}
if !strings.Contains(string(output), "build-release-assets:") {
t.Fatalf("asset builder %q output = %q", args, output)
}
}
}
func TestAssetBuilderBuildsNamedAssetsAndChecksEmbeddedVersion(t *testing.T) {
repoRoot := releaseCheckRepoRoot(t)
builder := filepath.Join(repoRoot, "scripts", "build-release-assets.sh")
binDir := t.TempDir()
writeFakeGo(t, filepath.Join(binDir, "go"))
outputDir := filepath.Join(t.TempDir(), "assets")
goLog := filepath.Join(t.TempDir(), "go.log")
command := exec.Command("sh", builder, "v0.0.0-test", outputDir)
command.Dir = t.TempDir()
command.Env = append(os.Environ(),
"PATH="+binDir+string(os.PathListSeparator)+os.Getenv("PATH"),
"GOWORK=/caller/controlled/go.work",
"GOFLAGS=-tags=releasecheck",
"NARRATIO_RELEASE_EXPECT_GOFLAGS=-tags=releasecheck",
"NARRATIO_RELEASE_ASSET_GO_LOG="+goLog,
)
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("asset builder error = %v\n%s", err, output)
}
entries, err := os.ReadDir(outputDir)
if err != nil {
t.Fatal(err)
}
got := make([]string, 0, len(entries))
for _, entry := range entries {
got = append(got, entry.Name())
}
sort.Strings(got)
want := []string{
"narratio-v0.0.0-test-darwin-arm64",
"narratio-v0.0.0-test-linux-amd64",
"narratio-v0.0.0-test-linux-arm64",
}
if !slicesEqual(got, want) {
t.Fatalf("asset names = %#v, want %#v", got, want)
}
version := exec.Command(filepath.Join(outputDir, "narratio-v0.0.0-test-linux-amd64"), "version", "--format", "text")
var versionOutput bytes.Buffer
version.Stdout = &versionOutput
if err := version.Run(); err != nil {
t.Fatal(err)
}
if got := versionOutput.String(); got != "narratio v0.0.0-test\n" {
t.Fatalf("embedded version output = %q", got)
}
goCommands, err := os.ReadFile(goLog)
if err != nil {
t.Fatal(err)
}
builds := 0
for _, line := range strings.Split(strings.TrimSpace(string(goCommands)), "\n") {
if !strings.HasPrefix(line, "off ") {
t.Fatalf("asset builder Go command did not receive GOWORK=off: %q", line)
}
if strings.Contains(line, " build ") {
builds++
if !strings.Contains(line, " build -mod=readonly ") {
t.Fatalf("asset builder did not select readonly modules: %q", line)
}
}
}
if builds != 3 {
t.Fatalf("asset builder ran %d builds, want three: %q", builds, goCommands)
}
}
func TestCandidateCheckerRejectsMalformedVersionAndReleaseNotes(t *testing.T) {
fixture := newCandidateFixture(t)
if output, err := fixture.run("v1.2"); err == nil || !strings.Contains(output, "invalid stable version") {
t.Fatalf("malformed version error = %v\n%s", err, output)
}
if err := os.Remove(fixture.notePath); err != nil {
t.Fatal(err)
}
if output, err := fixture.run("v1.2.3"); err == nil || !strings.Contains(output, "release note must be nonempty") {
t.Fatalf("missing note error = %v\n%s", err, output)
}
for _, note := range []string{
"",
"# Narratio v1.2.4\n\nA release.\n",
} {
fixture := newCandidateFixture(t)
if err := os.WriteFile(fixture.notePath, []byte(note), 0o644); err != nil {
t.Fatal(err)
}
if output, err := fixture.run("v1.2.3"); err == nil || !strings.Contains(output, "release note") {
t.Fatalf("invalid note error = %v\n%s", err, output)
}
}
}
func TestCandidateCheckerRejectsRepositoryHygieneViolations(t *testing.T) {
for _, test := range []struct {
name string
prepare func(*testing.T, *candidateFixture)
want string
}{
{
name: "wrong module", want: "go.mod must declare module",
prepare: func(t *testing.T, fixture *candidateFixture) {
if err := os.WriteFile(filepath.Join(fixture.root, "go.mod"), []byte("module example.invalid/release\n\ngo 1.26.0\n"), 0o644); err != nil {
t.Fatal(err)
}
},
},
{
name: "filesystem replacement", want: "filesystem module replacements are not allowed",
prepare: func(t *testing.T, fixture *candidateFixture) {
appendFixtureModuleReplacement(t, fixture, "replace example.invalid/module => ./module\n")
fixture.extraEnv = append(fixture.extraEnv, "NARRATIO_RELEASE_LOCAL_REPLACEMENT=example.invalid/module")
},
},
} {
t.Run(test.name, func(t *testing.T) {
fixture := newCandidateFixture(t)
test.prepare(t, fixture)
if output, err := fixture.run("v1.2.3"); err == nil || !strings.Contains(output, test.want) {
t.Fatalf("hygiene error = %v\n%s", err, output)
}
})
}
}
func TestCandidateCheckerPropagatesOwnedValidationFailure(t *testing.T) {
fixture := newCandidateFixture(t)
fixture.extraEnv = append(fixture.extraEnv, "NARRATIO_RELEASE_FAIL_GO=test -mod=readonly -race -count=1 ./...")
if output, err := fixture.run("v1.2.3"); err == nil || !strings.Contains(output, "failed: go test -mod=readonly -race -count=1 ./...") {
t.Fatalf("validation failure = %v\n%s", err, output)
}
}
func TestCandidateCheckerSelectsOwnedValidationOnce(t *testing.T) {
fixture := newCandidateFixture(t)
fixture.extraEnv = append(fixture.extraEnv, "GOFLAGS=-tags=releasecheck", "NARRATIO_RELEASE_EXPECT_GOFLAGS=-tags=releasecheck")
if output, err := fixture.run("v1.2.3"); err != nil {
t.Fatalf("candidate checker error = %v\n%s", err, output)
}
data, err := os.ReadFile(fixture.logPath)
if err != nil {
t.Fatal(err)
}
lines := strings.FieldsFunc(strings.TrimSpace(string(data)), func(r rune) bool { return r == '\n' })
if len(lines) != 6 {
t.Fatalf("owned command log = %q", data)
}
for _, line := range lines {
if !strings.HasPrefix(line, "off ") {
t.Fatalf("owned command did not receive GOWORK=off: %q", line)
}
}
want := []string{
"off list -m -mod=readonly -f {{if .Replace}}{{if not .Replace.Version}}{{.Path}}{{end}}{{end}} all",
"off mod tidy -diff",
"off gofmt -l",
"off test -mod=readonly -race -count=1 ./...",
"off vet -mod=readonly ./...",
"off build-release-assets v1.2.3",
}
for _, prefix := range want {
count := 0
for _, line := range lines {
if strings.HasPrefix(line, prefix) {
count++
}
}
if count != 1 {
t.Fatalf("owned command %q ran %d times, want once: %q", prefix, count, lines)
}
}
}
func TestCandidateCheckerAllowsInertModuleInputs(t *testing.T) {
fixture := newCandidateFixture(t)
if err := os.Mkdir(filepath.Join(fixture.root, "vendor"), 0o755); err != nil {
t.Fatal(err)
}
writeFixtureFile(t, filepath.Join(fixture.root, "go.work"), "go 1.26.0\n", 0o644)
writeFixtureFile(t, filepath.Join(fixture.root, "go.work.sum"), "checksum\n", 0o644)
writeFixtureFile(t, filepath.Join(fixture.root, "vendor", "modules.txt"), "# module metadata\n", 0o644)
fixture.git("add", ".")
if output, err := fixture.run("v1.2.3"); err != nil {
t.Fatalf("candidate checker rejected inert workspace/vendor files: %v\n%s", err, output)
}
}
func TestCandidateCheckerAllowsVersionedReplacement(t *testing.T) {
fixture := newCandidateFixture(t)
appendFixtureModuleReplacement(t, fixture, "replace example.invalid/module v1.0.0 => example.invalid/fork v1.0.1\n")
if output, err := fixture.run("v1.2.3"); err != nil {
t.Fatalf("candidate checker rejected versioned replacement metadata: %v\n%s", err, output)
}
}
func appendFixtureModuleReplacement(t *testing.T, fixture *candidateFixture, replacement string) {
t.Helper()
path := filepath.Join(fixture.root, "go.mod")
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, append(data, []byte(replacement)...), 0o644); err != nil {
t.Fatal(err)
}
}
func releaseCheckRepoRoot(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller() failed")
}
return filepath.Clean(filepath.Join(filepath.Dir(file), "..", ".."))
}
func writeFakeGo(t *testing.T, path string) {
t.Helper()
const fake = `#!/bin/sh
set -eu
if [ -n "${NARRATIO_RELEASE_ASSET_GO_LOG:-}" ]; then
printf '%s %s\n' "${GOWORK:-}" "$*" >> "$NARRATIO_RELEASE_ASSET_GO_LOG"
fi
if [ -n "${NARRATIO_RELEASE_EXPECT_GOFLAGS:-}" ] && [ "${GOFLAGS:-}" != "$NARRATIO_RELEASE_EXPECT_GOFLAGS" ]; then
exit 1
fi
if [ "$1" = env ]; then
case $2 in
GOOS) printf '%s\n' linux ;;
GOARCH) printf '%s\n' amd64 ;;
*) exit 1 ;;
esac
exit 0
fi
output=
version=
while [ "$#" -gt 0 ]; do
case $1 in
-o)
shift
output=$1
;;
-ldflags)
shift
case $1 in
*gitea.maximumdirect.net/eric/narratio/internal/buildinfo.Version=*)
version=${1##*=}
;;
esac
;;
esac
shift
done
[ -n "$output" ]
[ -n "$version" ]
printf '#!/bin/sh\nprintf "narratio %%s\\n" "%s"\n' "$version" > "$output"
chmod +x "$output"
`
if err := os.WriteFile(path, []byte(fake), 0o755); err != nil {
t.Fatal(err)
}
}
func slicesEqual(left, right []string) bool {
if len(left) != len(right) {
return false
}
for index := range left {
if left[index] != right[index] {
return false
}
}
return true
}
type candidateFixture struct {
root string
notePath string
binDir string
logPath string
extraEnv []string
}
func newCandidateFixture(t *testing.T) *candidateFixture {
t.Helper()
root := t.TempDir()
for _, dir := range []string{"scripts", "docs/releases"} {
if err := os.MkdirAll(filepath.Join(root, dir), 0o755); err != nil {
t.Fatal(err)
}
}
repoRoot := releaseCheckRepoRoot(t)
copyFixtureFile(t, filepath.Join(repoRoot, "scripts", "release-lib.sh"), filepath.Join(root, "scripts", "release-lib.sh"), 0o755)
copyFixtureFile(t, filepath.Join(repoRoot, "scripts", "check-release-candidate.sh"), filepath.Join(root, "scripts", "check-release-candidate.sh"), 0o755)
writeFixtureFile(t, filepath.Join(root, "scripts", "build-release-assets.sh"), `#!/bin/sh
set -eu
printf '%s build-release-assets %s\n' "${GOWORK:-}" "$1" >> "$NARRATIO_RELEASE_CHECK_LOG"
`, 0o755)
writeFixtureFile(t, filepath.Join(root, "go.mod"), "module gitea.maximumdirect.net/eric/narratio\n\ngo 1.26.0\n", 0o644)
writeFixtureFile(t, filepath.Join(root, "main.go"), "package narratio\n", 0o644)
notePath := filepath.Join(root, "docs", "releases", "v1.2.3.md")
writeFixtureFile(t, notePath, "# Narratio v1.2.3\n\nA concise release note.\n", 0o644)
fixture := &candidateFixture{root: root, notePath: notePath, binDir: t.TempDir(), logPath: filepath.Join(t.TempDir(), "commands.log")}
writeFixtureFile(t, filepath.Join(fixture.binDir, "go"), fixtureGoShim, 0o755)
writeFixtureFile(t, filepath.Join(fixture.binDir, "gofmt"), fixtureGofmtShim, 0o755)
fixture.git("init", "-q")
fixture.git("config", "user.email", "releasecheck@example.test")
fixture.git("config", "user.name", "Release Check")
fixture.git("add", ".")
fixture.git("commit", "-qm", "fixture")
return fixture
}
func (fixture *candidateFixture) run(version string) (string, error) {
command := exec.Command("sh", filepath.Join(fixture.root, "scripts", "check-release-candidate.sh"), version)
command.Dir = fixture.binDir
command.Env = append(os.Environ(),
"PATH="+fixture.binDir+string(os.PathListSeparator)+os.Getenv("PATH"),
"NARRATIO_RELEASE_CHECK_LOG="+fixture.logPath,
)
command.Env = append(command.Env, fixture.extraEnv...)
output, err := command.CombinedOutput()
return string(output), err
}
func (fixture *candidateFixture) git(args ...string) {
command := exec.Command("git", args...)
command.Dir = fixture.root
output, err := command.CombinedOutput()
if err != nil {
panic("fixture git command failed: " + string(output))
}
}
func copyFixtureFile(t *testing.T, source, destination string, mode os.FileMode) {
t.Helper()
data, err := os.ReadFile(source)
if err != nil {
t.Fatal(err)
}
writeFixtureFile(t, destination, string(data), mode)
}
func writeFixtureFile(t *testing.T, path, contents string, mode os.FileMode) {
t.Helper()
if err := os.WriteFile(path, []byte(contents), mode); err != nil {
t.Fatal(err)
}
}
const fixtureGoShim = `#!/bin/sh
set -eu
printf '%s %s\n' "${GOWORK:-}" "$*" >> "$NARRATIO_RELEASE_CHECK_LOG"
if [ -n "${NARRATIO_RELEASE_EXPECT_GOFLAGS:-}" ] && [ "${GOFLAGS:-}" != "$NARRATIO_RELEASE_EXPECT_GOFLAGS" ]; then
exit 1
fi
if [ "$1" = list ]; then
if [ -n "${NARRATIO_RELEASE_LOCAL_REPLACEMENT:-}" ]; then
printf '%s\n' "$NARRATIO_RELEASE_LOCAL_REPLACEMENT"
fi
exit 0
fi
if [ "${NARRATIO_RELEASE_FAIL_GO:-}" = "$*" ]; then
exit 1
fi
`
const fixtureGofmtShim = `#!/bin/sh
set -eu
printf '%s gofmt %s\n' "${GOWORK:-}" "$*" >> "$NARRATIO_RELEASE_CHECK_LOG"
`

View File

@@ -1,267 +0,0 @@
package releasecheck
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestReleaseCommandRejectsWrongBranchAndDirtyCheckout(t *testing.T) {
t.Run("wrong branch", func(t *testing.T) {
fixture := newReleaseFixture(t)
fixture.git("checkout", "-qb", "feature")
fixture.mustFail(t, "release checkout must be on main")
fixture.requireNoTag(t)
})
t.Run("dirty checkout", func(t *testing.T) {
fixture := newReleaseFixture(t)
writeReleaseFixtureFile(t, filepath.Join(fixture.worktree, "dirty.txt"), "dirty\n", 0o644)
fixture.mustFail(t, "release checkout must be clean")
fixture.requireNoTag(t)
})
}
func TestReleaseCommandRejectsInvalidInvocation(t *testing.T) {
fixture := newReleaseFixture(t)
if output, err := fixture.runWith(); err == nil || !strings.Contains(output, "usage: scripts/release.sh VERSION") {
t.Fatalf("missing argument error = %v\n%s", err, output)
}
if output, err := fixture.runWith("v1.2"); err == nil || !strings.Contains(output, "invalid stable version") {
t.Fatalf("invalid version error = %v\n%s", err, output)
}
fixture.requireNoTag(t)
}
func TestReleaseCommandRejectsUnpublishedOrExistingCandidates(t *testing.T) {
t.Run("head differs from origin main", func(t *testing.T) {
fixture := newReleaseFixture(t)
writeReleaseFixtureFile(t, filepath.Join(fixture.worktree, "local.txt"), "local\n", 0o644)
fixture.git("add", "local.txt")
fixture.git("commit", "-qm", "local only")
fixture.mustFail(t, "HEAD must equal origin/main")
fixture.requireNoTag(t)
})
t.Run("candidate checker fails", func(t *testing.T) {
fixture := newReleaseFixture(t)
fixture.extraEnv = append(fixture.extraEnv, "NARRATIO_RELEASE_CHECKER_MODE=fail")
fixture.mustFail(t, "release candidate validation failed")
fixture.requireNoTag(t)
assertCheckerCalls(t, fixture, 1)
})
t.Run("local tag exists", func(t *testing.T) {
fixture := newReleaseFixture(t)
fixture.git("tag", "v1.2.3")
fixture.mustFail(t, "local tag already exists")
})
t.Run("upstream tag exists", func(t *testing.T) {
fixture := newReleaseFixture(t)
fixture.git("tag", "v1.2.3")
fixture.git("push", "origin", "refs/tags/v1.2.3:refs/tags/v1.2.3")
fixture.git("tag", "-d", "v1.2.3")
fixture.mustFail(t, "local tag already exists")
})
}
func TestReleaseCommandRejectsChangedUpstreamDuringValidation(t *testing.T) {
fixture := newReleaseFixture(t)
advance := filepath.Join(t.TempDir(), "advance-origin.sh")
writeReleaseFixtureFile(t, advance, `#!/bin/sh
set -eu
clone_dir=$(mktemp -d)
trap 'rm -rf "$clone_dir"' 0 HUP INT TERM
git clone -q --branch main "$NARRATIO_RELEASE_TEST_ORIGIN" "$clone_dir"
cd "$clone_dir"
git config user.email releasecheck@example.test
git config user.name 'Release Check'
printf 'advance\n' > upstream.txt
git add upstream.txt
git commit -qm advance
git push -q origin main
`, 0o755)
fixture.extraEnv = append(fixture.extraEnv,
"NARRATIO_RELEASE_CHECKER_MODE=advance-origin",
"NARRATIO_RELEASE_ADVANCE_ORIGIN="+advance,
"NARRATIO_RELEASE_TEST_ORIGIN="+fixture.origin,
)
fixture.mustFail(t, "candidate or origin/main changed during release validation")
fixture.requireNoTag(t)
}
func TestReleaseCommandPushesOnlyTheRecordedLightweightTag(t *testing.T) {
fixture := newReleaseFixture(t)
writeReleaseFixtureFile(t, filepath.Join(fixture.worktree, ".git", "info", "exclude"), "ignored.tmp\n", 0o644)
writeReleaseFixtureFile(t, filepath.Join(fixture.worktree, "ignored.tmp"), "local-only\n", 0o644)
candidate := fixture.gitOutput("rev-parse", "HEAD^{commit}")
output, err := fixture.run()
if err != nil {
t.Fatalf("release command error = %v\n%s", err, output)
}
if !strings.Contains(output, "release: published v1.2.3 at "+candidate) {
t.Fatalf("release output = %q", output)
}
if got := fixture.gitOutput("cat-file", "-t", "refs/tags/v1.2.3"); got != "commit" {
t.Fatalf("local tag type = %q, want commit", got)
}
if got := fixture.gitOutput("rev-parse", "refs/tags/v1.2.3^{commit}"); got != candidate {
t.Fatalf("local tag commit = %q, want %q", got, candidate)
}
if got := fixture.gitOutput("--git-dir", fixture.origin, "show-ref", "--verify", "--hash", "refs/tags/v1.2.3"); got != candidate {
t.Fatalf("remote tag commit = %q, want %q", got, candidate)
}
if got := fixture.gitOutput("--git-dir", fixture.origin, "show-ref", "--verify", "--hash", "refs/heads/main"); got != candidate {
t.Fatalf("remote main = %q, want %q", got, candidate)
}
if got := strings.Fields(fixture.gitOutput("--git-dir", fixture.origin, "for-each-ref", "--format=%(refname)", "refs/heads", "refs/tags")); !slicesEqual(got, []string{"refs/heads/main", "refs/tags/v1.2.3"}) {
t.Fatalf("remote refs = %#v", got)
}
assertCheckerCalls(t, fixture, 1)
}
func TestReleaseCommandRetainsLocalTagAfterUnconfirmedPush(t *testing.T) {
fixture := newReleaseFixture(t)
writeReleaseFixtureFile(t, filepath.Join(fixture.origin, "hooks", "pre-receive"), "#!/bin/sh\nexit 1\n", 0o755)
output, err := fixture.run()
if err == nil || !strings.Contains(output, "tag push for v1.2.3 failed or is unconfirmed") || strings.Contains(output, "release: published") || strings.Contains(output, "not published") {
t.Fatalf("push failure = %v\n%s", err, output)
}
if got := fixture.gitOutput("cat-file", "-t", "refs/tags/v1.2.3"); got != "commit" {
t.Fatalf("local tag type after failed push = %q", got)
}
if output, err := runReleaseGit(fixture.worktree, "--git-dir", fixture.origin, "show-ref", "--verify", "refs/tags/v1.2.3"); err == nil {
t.Fatalf("remote tag exists after failed push: %s", output)
}
assertCheckerCalls(t, fixture, 1)
}
func assertCheckerCalls(t *testing.T, fixture *releaseFixture, want int) {
t.Helper()
data, err := os.ReadFile(fixture.checkerLog)
if err != nil {
t.Fatal(err)
}
if got := len(strings.Fields(string(data))); got != want {
t.Fatalf("candidate checker called %d times, want %d: %q", got, want, data)
}
}
type releaseFixture struct {
worktree string
origin string
checkerLog string
extraEnv []string
}
func newReleaseFixture(t *testing.T) *releaseFixture {
t.Helper()
root := t.TempDir()
worktree := filepath.Join(root, "worktree")
origin := filepath.Join(root, "origin.git")
if err := os.MkdirAll(filepath.Join(worktree, "scripts"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(worktree, "docs", "releases"), 0o755); err != nil {
t.Fatal(err)
}
repoRoot := releaseCheckRepoRoot(t)
copyReleaseFixtureFile(t, filepath.Join(repoRoot, "scripts", "release-lib.sh"), filepath.Join(worktree, "scripts", "release-lib.sh"), 0o755)
copyReleaseFixtureFile(t, filepath.Join(repoRoot, "scripts", "release.sh"), filepath.Join(worktree, "scripts", "release.sh"), 0o755)
writeReleaseFixtureFile(t, filepath.Join(worktree, "scripts", "check-release-candidate.sh"), releaseCandidateStub, 0o755)
writeReleaseFixtureFile(t, filepath.Join(worktree, "docs", "releases", "v1.2.3.md"), "# Narratio v1.2.3\n", 0o644)
writeReleaseFixtureFile(t, filepath.Join(worktree, "README.md"), "fixture\n", 0o644)
fixture := &releaseFixture{worktree: worktree, origin: origin, checkerLog: filepath.Join(root, "checker.log")}
fixture.git("init", "-qb", "main")
fixture.git("config", "user.email", "releasecheck@example.test")
fixture.git("config", "user.name", "Release Check")
fixture.git("add", ".")
fixture.git("commit", "-qm", "fixture")
if output, err := runReleaseGit(worktree, "init", "--bare", "-q", origin); err != nil {
t.Fatalf("init bare origin: %v\n%s", err, output)
}
fixture.git("remote", "add", "origin", origin)
fixture.git("push", "-qu", "origin", "main")
return fixture
}
func (fixture *releaseFixture) run() (string, error) {
return fixture.runWith("v1.2.3")
}
func (fixture *releaseFixture) runWith(args ...string) (string, error) {
commandArgs := append([]string{filepath.Join(fixture.worktree, "scripts", "release.sh")}, args...)
command := exec.Command("sh", commandArgs...)
command.Dir = fixture.worktree
command.Env = append(os.Environ(), "NARRATIO_RELEASE_CHECKER_LOG="+fixture.checkerLog)
command.Env = append(command.Env, fixture.extraEnv...)
output, err := command.CombinedOutput()
return string(output), err
}
func (fixture *releaseFixture) mustFail(t *testing.T, want string) {
t.Helper()
output, err := fixture.run()
if err == nil || !strings.Contains(output, want) {
t.Fatalf("release failure = %v\n%s", err, output)
}
}
func (fixture *releaseFixture) requireNoTag(t *testing.T) {
t.Helper()
if fixture.gitOutput("show-ref", "--verify", "refs/tags/v1.2.3") != "" {
t.Fatal("unexpected local tag")
}
if output, err := runReleaseGit(fixture.worktree, "--git-dir", fixture.origin, "show-ref", "--verify", "refs/tags/v1.2.3"); err == nil {
t.Fatalf("unexpected remote tag: %s", output)
}
}
func (fixture *releaseFixture) git(args ...string) {
if output, err := runReleaseGit(fixture.worktree, args...); err != nil {
panic("fixture git command failed: " + output)
}
}
func (fixture *releaseFixture) gitOutput(args ...string) string {
output, err := runReleaseGit(fixture.worktree, args...)
if err != nil {
return ""
}
return strings.TrimSpace(output)
}
func runReleaseGit(dir string, args ...string) (string, error) {
command := exec.Command("git", args...)
command.Dir = dir
output, err := command.CombinedOutput()
return string(output), err
}
func copyReleaseFixtureFile(t *testing.T, source, destination string, mode os.FileMode) {
t.Helper()
data, err := os.ReadFile(source)
if err != nil {
t.Fatal(err)
}
writeReleaseFixtureFile(t, destination, string(data), mode)
}
func writeReleaseFixtureFile(t *testing.T, path, contents string, mode os.FileMode) {
t.Helper()
if err := os.WriteFile(path, []byte(contents), mode); err != nil {
t.Fatal(err)
}
}
const releaseCandidateStub = `#!/bin/sh
set -eu
printf 'called\n' >> "$NARRATIO_RELEASE_CHECKER_LOG"
case "${NARRATIO_RELEASE_CHECKER_MODE:-success}" in
success) ;;
fail) exit 1 ;;
advance-origin) "$NARRATIO_RELEASE_ADVANCE_ORIGIN" ;;
*) exit 1 ;;
esac
`

View File

@@ -1,94 +0,0 @@
#!/bin/sh
set -eu
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd -P)
# shellcheck source=release-lib.sh
. "$script_dir/release-lib.sh"
tool_name=build-release-assets
# Release builds always use the tagged module in this repository. Own this
# setting here so direct callers and CI cannot supply an ambient Go workspace.
GOWORK=off
export GOWORK
if [ "$#" -ne 2 ]; then
narratio_release_fail "$tool_name" 'usage: scripts/build-release-assets.sh VERSION OUTPUT_DIR'
fi
release_version=$1
output_dir=$2
if ! narratio_release_validate_version "$release_version" && [ "$release_version" != "v0.0.0-test" ]; then
narratio_release_fail "$tool_name" "invalid stable version: $release_version"
fi
case $output_dir in
/*) ;;
*) narratio_release_fail "$tool_name" "output directory must be absolute: $output_dir" ;;
esac
repo_root=$(narratio_release_repo_root "$0") || narratio_release_fail "$tool_name" 'cannot locate repository root'
if ! cd "$repo_root"; then
narratio_release_fail "$tool_name" "cannot enter repository root: $repo_root"
fi
if [ "$output_dir" = / ]; then
narratio_release_fail "$tool_name" 'output directory must not be /'
fi
if [ -L "$output_dir" ]; then
narratio_release_fail "$tool_name" "output directory must not be a symlink: $output_dir"
fi
if [ -e "$output_dir" ]; then
if [ ! -d "$output_dir" ]; then
narratio_release_fail "$tool_name" "output path must be a directory: $output_dir"
fi
resolved_output_dir=$(CDPATH= cd "$output_dir" && pwd -P) || narratio_release_fail "$tool_name" "cannot access output directory: $output_dir"
if [ "$resolved_output_dir" = "$repo_root" ]; then
narratio_release_fail "$tool_name" 'output directory must not be the repository root'
fi
for existing_entry in "$output_dir"/* "$output_dir"/.[!.]* "$output_dir"/..?*; do
if [ -e "$existing_entry" ] || [ -L "$existing_entry" ]; then
narratio_release_fail "$tool_name" "output directory must be empty: $output_dir"
fi
done
else
if ! mkdir "$output_dir"; then
narratio_release_fail "$tool_name" "cannot create output directory: $output_dir"
fi
fi
temporary_smoke_binary=
cleanup_temporary_smoke_binary() {
if [ -n "$temporary_smoke_binary" ]; then
rm -f "$temporary_smoke_binary"
fi
}
trap cleanup_temporary_smoke_binary 0 HUP INT TERM
build_flags="-s -w -X gitea.maximumdirect.net/eric/narratio/internal/buildinfo.Version=$release_version"
host_os=$(go env GOOS)
host_arch=$(go env GOARCH)
host_asset=
for target in linux/amd64 linux/arm64 darwin/arm64; do
target_os=${target%%/*}
target_arch=${target#*/}
asset_path="$output_dir/narratio-$release_version-$target_os-$target_arch"
CGO_ENABLED=0 GOOS="$target_os" GOARCH="$target_arch" go build -mod=readonly -trimpath -ldflags "$build_flags" -o "$asset_path" ./cmd/narratio
if [ "$target_os" = "$host_os" ] && [ "$target_arch" = "$host_arch" ]; then
host_asset=$asset_path
fi
done
if [ -z "$host_asset" ]; then
temporary_smoke_binary="$output_dir/.narratio-release-version-smoke"
CGO_ENABLED=0 go build -mod=readonly -trimpath -ldflags "$build_flags" -o "$temporary_smoke_binary" ./cmd/narratio
host_asset=$temporary_smoke_binary
fi
reported_version=$("$host_asset" version --format text)
if [ "$reported_version" != "narratio $release_version" ]; then
narratio_release_fail "$tool_name" "release binary reported unexpected version: $reported_version"
fi
cleanup_temporary_smoke_binary
temporary_smoke_binary=

View File

@@ -1,80 +0,0 @@
#!/bin/sh
set -eu
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd -P)
# shellcheck source=release-lib.sh
. "$script_dir/release-lib.sh"
tool_name=check-release-candidate
if [ "$#" -ne 1 ]; then
narratio_release_fail "$tool_name" 'usage: scripts/check-release-candidate.sh VERSION'
fi
release_version=$1
if ! narratio_release_validate_version "$release_version"; then
narratio_release_fail "$tool_name" "invalid stable version: $release_version"
fi
repo_root=$(narratio_release_repo_root "$0") || narratio_release_fail "$tool_name" 'cannot locate repository root'
if ! cd "$repo_root"; then
narratio_release_fail "$tool_name" "cannot enter repository root: $repo_root"
fi
release_note="docs/releases/$release_version.md"
if [ ! -s "$release_note" ]; then
narratio_release_fail "$tool_name" "release note must be nonempty: $release_note"
fi
note_heading=$(sed -n '1p' "$release_note")
if [ "$note_heading" != "# Narratio $release_version" ]; then
narratio_release_fail "$tool_name" "release note must begin with # Narratio $release_version"
fi
if ! grep -Eq '^module[[:space:]]+gitea\.maximumdirect\.net/eric/narratio[[:space:]]*$' go.mod; then
narratio_release_fail "$tool_name" 'go.mod must declare module gitea.maximumdirect.net/eric/narratio'
fi
if ! local_replacements=$(GOWORK=off go list -m -mod=readonly -f '{{if .Replace}}{{if not .Replace.Version}}{{.Path}}{{end}}{{end}}' all); then
narratio_release_fail "$tool_name" 'failed: go list -m replacement metadata'
fi
if [ -n "$local_replacements" ]; then
narratio_release_fail "$tool_name" "filesystem module replacements are not allowed: $local_replacements"
fi
narratio_release_go() {
if ! GOWORK=off go "$@"; then
narratio_release_fail "$tool_name" "failed: go $*"
fi
}
narratio_release_go mod tidy -diff
unformatted_files=$(git ls-files -z -- '*.go' | xargs -0 env GOWORK=off gofmt -l) || narratio_release_fail "$tool_name" 'failed: gofmt tracked Go files'
if [ -n "$unformatted_files" ]; then
narratio_release_fail "$tool_name" "tracked Go files are not gofmt clean: $unformatted_files"
fi
if ! git diff --check; then
narratio_release_fail "$tool_name" 'working-tree whitespace check failed'
fi
if ! git diff --cached --check; then
narratio_release_fail "$tool_name" 'cached whitespace check failed'
fi
narratio_release_go test -mod=readonly -race -count=1 ./...
narratio_release_go vet -mod=readonly ./...
temporary_assets_dir=$(mktemp -d "${TMPDIR:-/tmp}/narratio-release-candidate.XXXXXX") || narratio_release_fail "$tool_name" 'cannot create temporary asset directory'
case $temporary_assets_dir in
/*) ;;
*) narratio_release_fail "$tool_name" 'temporary asset directory must be absolute' ;;
esac
if [ ! -d "$temporary_assets_dir" ] || [ -L "$temporary_assets_dir" ]; then
narratio_release_fail "$tool_name" "temporary asset directory is unsafe: $temporary_assets_dir"
fi
cleanup_temporary_assets() {
if [ -n "${temporary_assets_dir:-}" ] && [ -d "$temporary_assets_dir" ] && [ ! -L "$temporary_assets_dir" ]; then
rm -rf "$temporary_assets_dir"
fi
}
trap cleanup_temporary_assets 0 HUP INT TERM
if ! GOWORK=off "$script_dir/build-release-assets.sh" "$release_version" "$temporary_assets_dir"; then
narratio_release_fail "$tool_name" 'failed: build-release-assets.sh'
fi

View File

@@ -1,26 +0,0 @@
#!/bin/sh
# Shared helpers for Narratio's checked-in release scripts. This file is
# intentionally source-only and has no effect when loaded.
narratio_release_fail() {
tool_name=$1
shift
printf '%s: %s\n' "$tool_name" "$*" >&2
exit 1
}
narratio_release_validate_version() {
case $1 in
*'
'*) return 1 ;;
esac
printf '%s\n' "$1" |
grep -Eq '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$'
}
narratio_release_repo_root() (
script_path=$1
script_dir=$(CDPATH= cd "$(dirname "$script_path")" && pwd -P) || return 1
CDPATH= cd "$script_dir/.." && pwd -P
)

View File

@@ -1,104 +0,0 @@
#!/bin/sh
set -eu
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd -P)
# shellcheck source=release-lib.sh
. "$script_dir/release-lib.sh"
tool_name=release
if [ "$#" -ne 1 ]; then
narratio_release_fail "$tool_name" 'usage: scripts/release.sh VERSION'
fi
release_version=$1
if ! narratio_release_validate_version "$release_version"; then
narratio_release_fail "$tool_name" "invalid stable version: $release_version"
fi
repo_root=$(narratio_release_repo_root "$0") || narratio_release_fail "$tool_name" 'cannot locate repository root'
if ! cd "$repo_root"; then
narratio_release_fail "$tool_name" "cannot enter repository root: $repo_root"
fi
narratio_release_require_main_branch() {
current_branch=$(git branch --show-current) || narratio_release_fail "$tool_name" 'cannot determine current branch'
if [ "$current_branch" != main ]; then
narratio_release_fail "$tool_name" 'release checkout must be on main'
fi
}
narratio_release_require_clean_checkout() {
if [ -n "$(git status --porcelain)" ]; then
narratio_release_fail "$tool_name" 'release checkout must be clean'
fi
}
narratio_release_fetch_origin() {
if ! git fetch origin main --tags; then
narratio_release_fail "$tool_name" 'failed to fetch origin main and tags'
fi
}
narratio_release_require_no_local_tag() {
if git show-ref --verify --quiet "refs/tags/$release_version"; then
narratio_release_fail "$tool_name" "local tag already exists: $release_version"
fi
}
narratio_release_require_no_remote_tag() {
if ! remote_tag=$(git ls-remote --tags origin "refs/tags/$release_version"); then
narratio_release_fail "$tool_name" "cannot inspect tag at origin: $release_version"
fi
if [ -n "$remote_tag" ]; then
narratio_release_fail "$tool_name" "upstream tag already exists: $release_version"
fi
}
narratio_release_require_main_branch
narratio_release_require_clean_checkout
narratio_release_fetch_origin
candidate_commit=$(git rev-parse --verify 'HEAD^{commit}') || narratio_release_fail "$tool_name" 'cannot resolve HEAD commit'
upstream_commit=$(git rev-parse --verify 'origin/main^{commit}') || narratio_release_fail "$tool_name" 'cannot resolve origin/main commit'
if [ "$candidate_commit" != "$upstream_commit" ]; then
narratio_release_fail "$tool_name" 'HEAD must equal origin/main before release validation'
fi
release_note="docs/releases/$release_version.md"
if ! git cat-file -e "$candidate_commit:$release_note"; then
narratio_release_fail "$tool_name" "candidate commit is missing release note: $release_note"
fi
narratio_release_require_no_local_tag
narratio_release_require_no_remote_tag
if ! "$script_dir/check-release-candidate.sh" "$release_version"; then
narratio_release_fail "$tool_name" 'release candidate validation failed'
fi
narratio_release_fetch_origin
narratio_release_require_main_branch
narratio_release_require_clean_checkout
current_commit=$(git rev-parse --verify 'HEAD^{commit}') || narratio_release_fail "$tool_name" 'cannot resolve HEAD commit after validation'
current_upstream_commit=$(git rev-parse --verify 'origin/main^{commit}') || narratio_release_fail "$tool_name" 'cannot resolve origin/main commit after validation'
if [ "$current_commit" != "$candidate_commit" ] || [ "$current_upstream_commit" != "$candidate_commit" ]; then
narratio_release_fail "$tool_name" 'candidate or origin/main changed during release validation'
fi
narratio_release_require_no_local_tag
narratio_release_require_no_remote_tag
if ! git -c tag.gpgSign=false tag "$release_version" "$candidate_commit"; then
narratio_release_fail "$tool_name" "cannot create local tag: $release_version"
fi
if [ "$(git cat-file -t "refs/tags/$release_version")" != commit ]; then
narratio_release_fail "$tool_name" "local tag is not lightweight: $release_version"
fi
if [ "$(git rev-parse --verify "refs/tags/$release_version^{commit}")" != "$candidate_commit" ]; then
narratio_release_fail "$tool_name" "local tag does not resolve to candidate commit: $release_version"
fi
if ! git push origin "refs/tags/$release_version:refs/tags/$release_version"; then
printf 'release: tag push for %s failed or is unconfirmed; local tag retained; inspect origin before retrying\n' "$release_version" >&2
exit 1
fi
printf 'release: published %s at %s\n' "$release_version" "$candidate_commit"
exit 0