Files
releasekit/internal/readiness/committed_test.go

128 lines
4.5 KiB
Go

package readiness
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"go.maximumdirect.net/releasekit/internal/artifact"
"go.maximumdirect.net/releasekit/internal/releaseidentity"
)
func TestCommittedReadinessUsesCapturedModuleAndMainPackage(t *testing.T) {
repo := t.TempDir()
run := func(args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repo
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v: %s", args, err, out)
}
return strings.TrimSpace(string(out))
}
run("init", "--initial-branch=main")
run("config", "user.name", "ReleaseKit Test")
run("config", "user.email", "releasekit@example.test")
if err := os.WriteFile(filepath.Join(repo, ".gitignore"), []byte(".releasekit/\n"), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repo, "go.mod"), []byte("module example.com/fixture\n\ngo 1.25.0\n"), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repo, "main.go"), []byte("package main\nfunc main() {}\n"), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repo, "required.txt"), []byte("captured"), 0600); err != nil {
t.Fatal(err)
}
run("add", "--all")
run("commit", "-m", "capture inputs")
commit := run("rev-parse", "HEAD")
if err := os.Mkdir(filepath.Join(repo, ".releasekit"), 0700); err != nil {
t.Fatal(err)
}
key := filepath.Join(repo, ".releasekit", "ssh-key")
if err := os.WriteFile(key, []byte("local fixture key"), 0600); err != nil {
t.Fatal(err)
}
os.Remove(filepath.Join(repo, "main.go"))
os.WriteFile(filepath.Join(repo, "go.mod"), []byte("invalid working module"), 0600)
request := Request{RepositoryDir: repo, Commit: commit, Product: "Example", Context: "local", Source: &releaseidentity.SourceSpec{GoEnabled: true}, Operations: []string{"build"}, Build: &artifact.Settings{MainPackage: ".", BinaryName: "fixture", Targets: []artifact.Target{{OS: "linux", Arch: "amd64"}}}, RequiredFiles: []string{"required.txt", filepath.Join(repo, "required.txt")}}
request.RequiredFiles = append(request.RequiredFiles, ".releasekit/ssh-key", key)
report := Run(t.Context(), request)
for _, check := range report.Checks {
if check.Status == "failed" {
t.Fatalf("captured readiness failed: %+v", check)
}
}
if err := os.Remove(filepath.Join(repo, "required.txt")); err != nil {
t.Fatal(err)
}
report = Run(t.Context(), request)
missing := 0
for _, check := range report.Checks {
if check.ID == "required-file" && check.Status == "failed" {
missing++
}
if check.ID == "build-inputs" && check.Status != "passed" {
t.Fatalf("captured build inputs changed: %+v", check)
}
}
if missing != 2 {
t.Fatalf("missing local inputs were supplied by Git: %+v", report.Checks)
}
request.Commit = ""
report = Run(t.Context(), request)
failed := false
for _, check := range report.Checks {
if check.Status == "failed" {
failed = true
}
}
if !failed {
t.Fatal("working readiness unexpectedly accepted missing and invalid inputs")
}
}
func TestReadinessSupportsProductionModuleMajors(t *testing.T) {
for _, modulePath := range []string{"example.com/fixture", "example.com/fixture/v2", "gopkg.in/fixture.v2", "gopkg.in/fixture.v2-unstable"} {
t.Run(modulePath, func(t *testing.T) {
repo := t.TempDir()
for _, args := range [][]string{{"init", "--initial-branch=main"}, {"config", "user.name", "ReleaseKit Test"}, {"config", "user.email", "releasekit@example.test"}} {
command := exec.Command("git", args...)
command.Dir = repo
if out, err := command.CombinedOutput(); err != nil {
t.Fatalf("git: %v: %s", err, out)
}
}
if err := os.WriteFile(filepath.Join(repo, "go.mod"), []byte("module "+modulePath+"\n\ngo 1.25.0\n"), 0600); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{{"add", "go.mod"}, {"commit", "-m", "capture module"}} {
command := exec.Command("git", args...)
command.Dir = repo
if out, err := command.CombinedOutput(); err != nil {
t.Fatalf("git: %v: %s", err, out)
}
}
command := exec.Command("git", "rev-parse", "HEAD")
command.Dir = repo
out, err := command.Output()
if err != nil {
t.Fatal(err)
}
for _, commit := range []string{"", strings.TrimSpace(string(out))} {
report := Run(t.Context(), Request{RepositoryDir: repo, Commit: commit, Product: "Example", Context: "local", Offline: true, Operations: []string{"go"}})
for _, check := range report.Checks {
if check.Status == "failed" {
t.Fatalf("commit=%q check=%+v", commit, check)
}
}
}
})
}
}