beads/scripts/check-versions/main_test.go
Julian Knutsen 555b01d789
fix(release): name the version-drift remedies that work (#7083)
Post-merge review follow-up to #5614.

#5614 replaced the shell checker's three-way drift remedy with a bare
"Run: scripts/update-versions.sh <canonical>" in both the Go checker
(status 1) and bd preflight's Version sync output, and the release-tag
pre-push hook kept answering status 1 with "Fix with:
scripts/update-versions.sh <version>". update-versions.sh derives the old
version from cmd/bd/version.go and anchors its substitutions on it, so
with version.go already at the canonical version that re-run rewrites
only the .githooks markers and uv.lock and leaves any other drifted file
as it was.

Restore the remedy as versioncheck.Report.MismatchRemedy, shared by the
checker and preflight:
- one file drifted: edit it to the expected value
- several drifted: set version.go to the previous version, then replay
  scripts/update-versions.sh <canonical>
- version.go is wrong: scripts/update-versions.sh <intended-version>

The hook's status-1 text now points at the checker output instead of
prescribing update-versions.sh; status 3 is unchanged. The text is sized
so one drifted file shows in full within preflight's 500-byte human
output, with the several-files case ahead of the version.go one.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 11:34:06 -07:00

270 lines
8.3 KiB
Go

package main
import (
"bytes"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/versioncheck"
)
func TestRunExpectedVersionContract(t *testing.T) {
root := writeReleaseFixture(t, "1.1.0")
tests := []struct {
name string
args []string
wantCode int
wantStdout string
wantStderr string
}{
{
name: "aligned version",
args: []string{"--expect", "1.1.0"},
wantCode: 0,
wantStdout: "✓ Version files in sync at: 1.1.0",
},
{
name: "mismatched version",
args: []string{"--expect", "9.9.9"},
wantCode: 3,
wantStdout: "Canonical version (from version.go): 1.1.0",
wantStderr: "Canonical release version 1.1.0 does not match required version 9.9.9",
},
{
name: "empty version",
args: []string{"--expect="},
wantCode: 2,
wantStderr: "--expect requires a non-empty VERSION",
},
{
name: "duplicate version",
args: []string{"--expect", "1.1.0", "--expect", "1.1.0"},
wantCode: 2,
wantStderr: "--expect may be specified only once",
},
{
name: "unknown argument",
args: []string{"--unknown"},
wantCode: 2,
wantStderr: "flag provided but not defined: -unknown",
},
{
name: "positional argument",
args: []string{"1.1.0"},
wantCode: 2,
wantStderr: "usage: scripts/check-versions.sh [--expect VERSION]",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := run(
test.args,
root,
&stdout,
&stderr,
func(string) (bool, error) { return false, nil },
)
if code != test.wantCode {
t.Fatalf(
"exit code = %d, want %d\nstdout:\n%s\nstderr:\n%s",
code,
test.wantCode,
stdout.String(),
stderr.String(),
)
}
if test.wantStdout != "" && !strings.Contains(stdout.String(), test.wantStdout) {
t.Fatalf("stdout = %q, want substring %q", stdout.String(), test.wantStdout)
}
if test.wantStdout == "" && stdout.Len() != 0 {
t.Fatalf("stdout = %q, want empty", stdout.String())
}
if !strings.Contains(stderr.String(), test.wantStderr) {
t.Fatalf("stderr = %q, want substring %q", stderr.String(), test.wantStderr)
}
if test.wantStderr == "" && stderr.Len() != 0 {
t.Fatalf("stderr = %q, want empty", stderr.String())
}
})
}
}
func TestRunPreservesOptionalUVLockFreshnessCheck(t *testing.T) {
root := writeReleaseFixture(t, "1.1.0")
for _, test := range []struct {
name string
check versioncheck.UVLockChecker
wantCode int
wantStdout string
wantStderr string
}{
{
name: "fresh",
check: func(string) (bool, error) {
return true, nil
},
wantCode: 0,
wantStdout: "✓ MCP uv.lock: fresh (uv lock --check)",
},
{
name: "stale",
check: func(string) (bool, error) {
return true, errors.New("stale")
},
wantCode: 1,
wantStderr: "❌ MCP uv.lock: stale",
},
} {
t.Run(test.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := run(nil, root, &stdout, &stderr, test.check)
if code != test.wantCode {
t.Fatalf("exit code = %d, want %d", code, test.wantCode)
}
if !strings.Contains(stdout.String(), test.wantStdout) {
t.Fatalf("stdout = %q, want substring %q", stdout.String(), test.wantStdout)
}
if !strings.Contains(stderr.String(), test.wantStderr) {
t.Fatalf("stderr = %q, want substring %q", stderr.String(), test.wantStderr)
}
if test.wantCode != 0 && strings.Contains(
stdout.String(),
"Version files in sync at:",
) {
t.Fatal("stale uv.lock emitted a success result")
}
})
}
}
func TestRunRejectsTrackedHookMarkerMismatch(t *testing.T) {
root := writeReleaseFixture(t, "1.1.0")
if err := os.WriteFile(
filepath.Join(root, ".githooks", "pre-push"),
[]byte(
"# --- BEGIN BEADS INTEGRATION v1.1.0 ---\n"+
"body\n# --- END BEADS INTEGRATION v9.9.9 ---\n",
),
0o755,
); err != nil {
t.Fatal(err)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
code := run(nil, root, &stdout, &stderr, nil)
if code != 1 {
t.Fatalf("exit code = %d, want 1", code)
}
if !strings.Contains(
stdout.String(),
".githooks/pre-push END marker: 9.9.9 (expected 1.1.0)",
) {
t.Fatalf("stdout = %q, want tracked-hook mismatch", stdout.String())
}
if !strings.Contains(
stderr.String(),
".githooks/pre-push END marker: 9.9.9 (expected 1.1.0)",
) {
t.Fatalf("stderr = %q, want tracked-hook mismatch summary", stderr.String())
}
if strings.Contains(stdout.String(), "Version files in sync at:") {
t.Fatal("tracked-hook mismatch emitted a success result")
}
}
// On surface drift cmd/bd/version.go already holds the canonical version, so
// scripts/update-versions.sh with that version leaves the drifted file as it
// was. Status 1 must not offer that re-run as the fix; it names the remedies
// that do work.
func TestRunDriftRemedyDoesNotPrescribeUpdateVersionsAlone(t *testing.T) {
root := writeReleaseFixture(t, "1.1.0")
if err := os.WriteFile(
filepath.Join(root, "plugins", "beads", ".copilot-plugin", "plugin.json"),
[]byte(`{"version":"1.0.9"}`),
0o644,
); err != nil {
t.Fatal(err)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
code := run(nil, root, &stdout, &stderr, nil)
if code != 1 {
t.Fatalf("exit code = %d, want 1\nstderr:\n%s", code, stderr.String())
}
for _, want := range []string{
"Copilot plugin.json: 1.0.9 (expected 1.1.0)",
"Re-running scripts/update-versions.sh 1.1.0 fixes only .githooks markers",
"- one file drifted: edit it to the expected value above",
"- several drifted: set version.go to the previous version, then\n" +
" scripts/update-versions.sh 1.1.0",
"- version.go is wrong: scripts/update-versions.sh <intended-version>",
} {
if !strings.Contains(stderr.String(), want) {
t.Errorf("stderr lacks %q:\n%s", want, stderr.String())
}
}
for _, bare := range []string{
"Run: scripts/update-versions.sh 1.1.0",
"Or manually update the mismatched files",
} {
if strings.Contains(stderr.String(), bare) {
t.Errorf("stderr still prescribes %q:\n%s", bare, stderr.String())
}
}
}
func writeReleaseFixture(t *testing.T, version string) string {
t.Helper()
root := t.TempDir()
// The Windows PE numeric fields must be purely numeric, so they carry the
// prerelease-stripped form.
base, _, _ := strings.Cut(version, "-")
files := map[string]string{
"go.mod": "module github.com/steveyegge/beads\n\ngo 1.26\n",
"cmd/bd/version.go": "package main\n\nvar Version = \"" + version + "\"\n",
"scripts/update-versions.sh": "#!/bin/sh\n",
"integrations/beads-mcp/pyproject.toml": "[project]\nversion = \"" +
version + "\"\n",
"integrations/beads-mcp/src/beads_mcp/__init__.py": "__version__ = \"" +
version + "\"\n",
"plugins/beads/.claude-plugin/plugin.json": `{"version":"` + version + `"}`,
"plugins/beads/.codex-plugin/plugin.json": `{"version":"` + version + `"}`,
".claude-plugin/marketplace.json": `{"plugins":[{"version":"` +
version + `"}]}`,
"npm-package/package.json": `{"version":"` + version + `"}`,
"plugins/beads/.copilot-plugin/plugin.json": `{"version":"` + version + `"}`,
"cmd/bd/winres/winres.json": `{"RT_VERSION":{"#1":{"0000":{` +
`"fixed":{"file_version":"` + base + `","product_version":"` + base + `"},` +
`"info":{"0409":{"FileVersion":"` + version +
`","ProductVersion":"` + version + `"}}}}}}`,
"cmd/bd/winres/manifest.xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<assembly>\n <assemblyIdentity\n type=\"win32\"\n" +
" version=\"" + base + ".0\"\n processorArchitecture=\"*\"/>\n</assembly>\n",
"integrations/beads-mcp/uv.lock": "version = 1\nrevision = 3\n\n" +
"[[package]]\nname = \"beads-mcp\"\nversion = \"" + version + "\"\n",
".githooks/pre-push": "# --- BEGIN BEADS INTEGRATION v" + version + " ---\n" +
"body\n# --- END BEADS INTEGRATION v" + version + " ---\n",
}
for relativePath, content := range files {
path := filepath.Join(root, filepath.FromSlash(relativePath))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("create parent for %s: %v", relativePath, err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", relativePath, err)
}
}
return root
}