Files
releasekit/internal/readiness/committed_test.go

233 lines
8.8 KiB
Go

package readiness
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"go.maximumdirect.net/releasekit/internal/artifact"
"go.maximumdirect.net/releasekit/internal/releaseidentity"
)
func TestReadinessInspectsImmediateLiteralPackageInputs(t *testing.T) {
for _, test := range []struct {
name, mainPackage, link, target string
files []string
manyFiles, wantPassed bool
}{
{name: "root", mainPackage: ".", files: []string{"main.go"}, wantPassed: true},
{name: "nested", mainPackage: "./cmd/app", files: []string{"cmd/app/main.go"}, wantPassed: true},
{name: "literal path", mainPackage: "./cmd/[app]", files: []string{"cmd/[app]/main.go"}, wantPassed: true},
{name: "only tests and nested sources", mainPackage: "./cmd/app", files: []string{"cmd/app/main_test.go", "cmd/app/deeper/main.go"}},
{name: "directory with Go suffix", mainPackage: "./cmd/app", files: []string{"cmd/app/main.go/contents.txt"}},
{name: "missing directory", mainPackage: "./absent"},
{name: "unreadable symlink", mainPackage: ".", link: "main.go", target: "missing.go"},
{name: "readable input after broken symlink", mainPackage: ".", files: []string{"valid.go"}, link: "broken.go", target: "missing.go", wantPassed: true},
{name: "symlinked input", mainPackage: "./cmd/app", files: []string{"shared/source.data"}, link: "cmd/app/main.go", target: "../../shared/source.data", wantPassed: true},
{name: "symlinked directory", mainPackage: "./alias", files: []string{"cmd/app/main.go"}, link: "alias", target: "cmd/app", wantPassed: true},
{name: "several batches without Go inputs", mainPackage: ".", manyFiles: true},
} {
t.Run(test.name, func(t *testing.T) {
repo := t.TempDir()
run := func(args ...string) string {
t.Helper()
command := exec.Command("git", args...)
command.Dir = repo
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v: %s", args, err, output)
}
return strings.TrimSpace(string(output))
}
run("init", "--initial-branch=main")
run("config", "user.name", "ReleaseKit Test")
run("config", "user.email", "releasekit@example.test")
write := func(name, contents string) {
t.Helper()
filename := filepath.Join(repo, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filename, []byte(contents), 0600); err != nil {
t.Fatal(err)
}
}
write("go.mod", "module example.com/fixture\n\ngo 1.25.0\n")
for _, name := range test.files {
write(name, "package main\nfunc main() {}\n")
}
directory, count := "unrelated/subtree", 8
if test.manyFiles {
directory, count = ".", 260
}
for index := range count {
write(filepath.ToSlash(filepath.Join(directory, fmt.Sprintf("file-%03d.txt", index))), "ordinary input")
}
if test.link != "" {
filename := filepath.Join(repo, filepath.FromSlash(test.link))
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
t.Fatal(err)
}
if err := os.Symlink(test.target, filename); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
}
run("add", "--all")
run("commit", "-m", "capture package")
captured := run("rev-parse", "HEAD")
for _, commit := range []string{"", captured} {
request := Request{RepositoryDir: repo, Commit: commit, Product: "Example", Context: "local", Offline: true, Source: &releaseidentity.SourceSpec{GoEnabled: true}, Operations: []string{"build"}, Build: &artifact.Settings{MainPackage: test.mainPackage, BinaryName: "fixture", Targets: []artifact.Target{{OS: "linux", Arch: "amd64"}}}}
report := Run(t.Context(), request)
found := false
for _, check := range report.Checks {
if check.ID == "build-inputs" {
found = true
if (check.Status == "passed") != test.wantPassed {
t.Fatalf("commit=%q check=%+v", commit, check)
}
}
}
if !found {
t.Fatalf("build inputs were not inspected: %+v", report.Checks)
}
}
})
}
}
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)
}
}
for _, name := range []string{"./...", "example.com/fixture/..."} {
request.Build.MainPackage = name
report := Run(t.Context(), request)
unchecked := false
for _, check := range report.Checks {
if check.Status == "failed" {
t.Fatalf("pattern=%s check=%+v", name, check)
}
if check.ID == "main-package" && check.Status == "unchecked" {
unchecked = true
}
}
if !unchecked {
t.Fatalf("pattern resolution was not identified: %+v", report.Checks)
}
}
request.Build.MainPackage = "."
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)
}
}
}
})
}
}