Add publication recovery handoff to CLI
This commit is contained in:
138
cmd/releasekit/publication_handoff_test.go
Normal file
138
cmd/releasekit/publication_handoff_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
"go.maximumdirect.net/releasekit/internal/publish"
|
||||
)
|
||||
|
||||
func TestPublishRecordOverrideWarningDoesNotChangeSuccess(t *testing.T) {
|
||||
repository, _ := newSimplePublishRepository(t, true)
|
||||
var stdout, stderr bytes.Buffer
|
||||
status := run(t.Context(), []string{"publish", "--repo", repository, "--record-dir", "record-blocker", "v1.2.3"}, &stdout, &stderr)
|
||||
if status != 0 || !strings.Contains(stdout.String(), "published v1.2.3") {
|
||||
t.Fatalf("status = %d, stdout = %q, stderr = %q", status, stdout.String(), stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "publication record warning:") || strings.Contains(stderr.String(), "publication record saved:") || strings.Contains(stderr.String(), "recovery command:") {
|
||||
t.Fatalf("recording warning or success handoff output = %q", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuiltPublishRecoveryCommandSelectsItsRecordAmongOtherAttempts(t *testing.T) {
|
||||
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
||||
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
|
||||
t.Setenv("GIT_TERMINAL_PROMPT", "0")
|
||||
repository, remote := newSimplePublishRepository(t, false)
|
||||
store := filepath.Join(t.TempDir(), "attempt records with ' apostrophe")
|
||||
hook := filepath.Join(remote, "hooks", "pre-receive")
|
||||
if err := os.WriteFile(hook, []byte("#!/bin/sh\nexit 1\n"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
goBinary := filepath.Join(runtime.GOROOT(), "bin", "go")
|
||||
executable := filepath.Join(t.TempDir(), "releasekit's CLI")
|
||||
build := exec.Command(goBinary, "build", "-o", executable, "./cmd/releasekit")
|
||||
build.Dir = repositoryRoot(t)
|
||||
if output, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build CLI: %v\n%s", err, output)
|
||||
}
|
||||
|
||||
command := exec.Command(executable, "publish", "--repo", repository, "--record-dir", store, "v1.2.3")
|
||||
command.Dir = repositoryRoot(t)
|
||||
output, err := command.CombinedOutput()
|
||||
var exitError *exec.ExitError
|
||||
if !errors.As(err, &exitError) || exitError.ExitCode() != 1 {
|
||||
t.Fatalf("publication exit = %v, output = %q; want status 1", err, output)
|
||||
}
|
||||
text := string(output)
|
||||
if !strings.Contains(text, "publication state=not_published phase=reconciliation") {
|
||||
t.Fatalf("publication failure classification changed: %q", text)
|
||||
}
|
||||
recordPath := outputLineValue(text, "publication record saved: ")
|
||||
if !filepath.IsAbs(recordPath) || !strings.Contains(recordPath, "attempt records with ' apostrophe") {
|
||||
t.Fatalf("saved record path = %q, output = %q", recordPath, text)
|
||||
}
|
||||
recoveryCommand := outputLineValue(text, "recovery command: ")
|
||||
if recoveryCommand == "" || !strings.Contains(recoveryCommand, shellQuote(executable)) || !strings.Contains(recoveryCommand, shellQuote(recordPath)) {
|
||||
t.Fatalf("copyable recovery command = %q, output = %q", recoveryCommand, text)
|
||||
}
|
||||
if !strings.Contains(text, "selected supported ReleaseKit CLI") {
|
||||
t.Fatalf("output omits guidance for an unavailable original executable: %q", text)
|
||||
}
|
||||
|
||||
for _, decoy := range []publish.Evidence{
|
||||
{RepositoryDir: repository, ModulePath: "example.com/other-module", Version: "v8.8.8", Tag: "v8.8.8", Endpoint: remote, Remote: "origin"},
|
||||
{RepositoryDir: repository, ModulePath: "example.com/retry", Version: "v1.2.4", Tag: "v1.2.4", Endpoint: remote, Remote: "origin"},
|
||||
} {
|
||||
if _, err := publish.SaveEvidence(repository, store, decoy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
recovery := exec.Command("/bin/sh", "-c", recoveryCommand)
|
||||
recovery.Dir = t.TempDir()
|
||||
recoveryOutput, recoveryErr := recovery.CombinedOutput()
|
||||
if !errors.As(recoveryErr, &exitError) || exitError.ExitCode() != 1 {
|
||||
t.Fatalf("recovery command exit = %v, output = %q; want known absence status 1", recoveryErr, recoveryOutput)
|
||||
}
|
||||
recoveryText := string(recoveryOutput)
|
||||
if !strings.Contains(recoveryText, "module=example.com/cli-candidate tag=v1.2.3 state=not_published") || strings.Contains(recoveryText, "example.com/other-module") || strings.Contains(recoveryText, "example.com/retry") {
|
||||
t.Fatalf("printed command selected the wrong attempt: %q", recoveryText)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellQuoteHandlesApostrophesAndSpaces(t *testing.T) {
|
||||
got := shellQuote("/tmp/ReleaseKit's recovery record.json")
|
||||
if got != "'/tmp/ReleaseKit'\"'\"'s recovery record.json'" {
|
||||
t.Fatalf("shellQuote() = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func newSimplePublishRepository(t *testing.T, recordBlocker bool) (string, string) {
|
||||
t.Helper()
|
||||
repository := newCLIRepository(t)
|
||||
settings, err := json.Marshal(cli.Settings{Product: "Example Product", Remote: "origin"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeRepositoryFile(t, repository, "releasekit.json", string(settings))
|
||||
files := []string{"releasekit.json"}
|
||||
if recordBlocker {
|
||||
writeRepositoryFile(t, repository, "record-blocker", "tracked file\n")
|
||||
files = append(files, "record-blocker")
|
||||
}
|
||||
cliGitRun(t, repository, append([]string{"add"}, files...)...)
|
||||
cliGitRun(t, repository, "commit", "-m", "configure source release")
|
||||
remote := filepath.Join(t.TempDir(), "origin.git")
|
||||
cliGitRun(t, repository, "init", "--bare", "--initial-branch=main", remote)
|
||||
cliGitRun(t, repository, "remote", "add", "origin", remote)
|
||||
cliGitRun(t, repository, "push", "--set-upstream", "origin", "main")
|
||||
return repository, remote
|
||||
}
|
||||
|
||||
func outputLineValue(output, prefix string) string {
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
if strings.HasPrefix(line, prefix) {
|
||||
return strings.TrimPrefix(line, prefix)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func repositoryRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
directory, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return filepath.Clean(filepath.Join(directory, "..", ".."))
|
||||
}
|
||||
Reference in New Issue
Block a user