Remove superseded release scripts and tests
This commit is contained in:
@@ -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"
|
||||
`
|
||||
@@ -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
|
||||
`
|
||||
Reference in New Issue
Block a user