Add release candidate checker
This commit is contained in:
@@ -101,6 +101,124 @@ func TestAssetBuilderBuildsNamedAssetsAndChecksEmbeddedVersion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
for _, note := range []string{
|
||||
"# Narratio v1.2.3\n\n## Summary\n\n## Compatibility\n\n## Upgrade\n",
|
||||
"# Narratio v1.2.4\n\n## Summary\n\n## Compatibility\n\n## Upgrade\n\n## Changes\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.25.0\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tracked workspace", want: "tracked workspace file is not allowed",
|
||||
prepare: func(t *testing.T, fixture *candidateFixture) {
|
||||
path := filepath.Join(fixture.root, "go.work")
|
||||
if err := os.WriteFile(path, []byte("go 1.25.0\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fixture.git("add", "go.work")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "vendor directory", want: "vendor directory is not allowed",
|
||||
prepare: func(t *testing.T, fixture *candidateFixture) {
|
||||
if err := os.Mkdir(filepath.Join(fixture.root, "vendor"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "replace directive", want: "replace directives are not allowed",
|
||||
prepare: func(t *testing.T, fixture *candidateFixture) {
|
||||
data, err := os.ReadFile(filepath.Join(fixture.root, "go.mod"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(fixture.root, "go.mod"), append(data, []byte("replace example.invalid/module => ./module\n")...), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
} {
|
||||
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 -race -count=1 ./...")
|
||||
if output, err := fixture.run("v1.2.3"); err == nil || !strings.Contains(output, "failed: go test -race -count=1 ./...") {
|
||||
t.Fatalf("validation failure = %v\n%s", err, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateCheckerUsesGoWorkOffForOwnedCommands(t *testing.T) {
|
||||
fixture := newCandidateFixture(t)
|
||||
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) < 9 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{
|
||||
"off mod tidy -diff",
|
||||
"off test -count=1 ./...",
|
||||
"off test -race -count=1 ./...",
|
||||
"off vet ./...",
|
||||
"off build ./...",
|
||||
"off test -count=1 ./internal/doccheck",
|
||||
"off test -count=1 ./internal/config -run ^TestExamplesLoadAndValidate$",
|
||||
"off gofmt -l",
|
||||
"off build-release-assets v1.2.3",
|
||||
} {
|
||||
if !containsLogLine(lines, want) {
|
||||
t.Fatalf("owned command log = %q, missing %q", data, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func releaseCheckRepoRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
@@ -165,3 +283,101 @@ func slicesEqual(left, right []string) bool {
|
||||
}
|
||||
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.25.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\n## Summary\n\n## Compatibility\n\n## Upgrade\n\n## Changes\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)
|
||||
}
|
||||
}
|
||||
|
||||
func containsLogLine(lines []string, want string) bool {
|
||||
for _, line := range lines {
|
||||
if strings.HasPrefix(line, want) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const fixtureGoShim = `#!/bin/sh
|
||||
set -eu
|
||||
printf '%s %s\n' "${GOWORK:-}" "$*" >> "$NARRATIO_RELEASE_CHECK_LOG"
|
||||
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"
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user