Add read-only release readiness validation
This commit is contained in:
@@ -17,12 +17,13 @@ import (
|
||||
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
||||
)
|
||||
|
||||
const Usage = "usage: releasekit version\n releasekit check [--repo DIR] [--config FILE] VERSION\n releasekit publish [--repo DIR] [--config FILE] [--record-dir DIR] [--expected-module DIR] [--expected-source DIR] VERSION\n releasekit build [--repo DIR] [--config FILE] [--output DIR] VERSION\n releasekit deliver [--repo DIR] [--config FILE] [--assets DIR] VERSION\n releasekit ci-deliver [--repo DIR] [--config FILE] [--remote NAME] [--prepared] [--expected-module DIR] [--expected-source DIR] --commit OID TAG\n releasekit reconcile --record FILE [--json]\n releasekit publication-attempts [--repo DIR] [--record-dir DIR] [--json]"
|
||||
const Usage = "usage: releasekit version\n releasekit validate [--repo DIR] [--config FILE] [--context local|ci] [--operation NAME ...] [--offline] [--assets DIR] [--json]\n releasekit check [--repo DIR] [--config FILE] VERSION\n releasekit publish [--repo DIR] [--config FILE] [--record-dir DIR] [--expected-module DIR] [--expected-source DIR] VERSION\n releasekit build [--repo DIR] [--config FILE] [--output DIR] VERSION\n releasekit deliver [--repo DIR] [--config FILE] [--assets DIR] VERSION\n releasekit ci-deliver [--repo DIR] [--config FILE] [--remote NAME] [--prepared] [--expected-module DIR] [--expected-source DIR] --commit OID TAG\n releasekit reconcile --record FILE [--json]\n releasekit publication-attempts [--repo DIR] [--record-dir DIR] [--json]"
|
||||
|
||||
type Command string
|
||||
|
||||
const (
|
||||
VersionCommand Command = "version"
|
||||
ValidateAction Command = "validate"
|
||||
CheckAction Command = "check"
|
||||
PublishAction Command = "publish"
|
||||
BuildAction Command = "build"
|
||||
@@ -49,6 +50,27 @@ type Invocation struct {
|
||||
RecordPath string
|
||||
RecordDir string
|
||||
JSON bool
|
||||
Context string
|
||||
Operations []string
|
||||
Offline bool
|
||||
}
|
||||
|
||||
type operationFlags []string
|
||||
|
||||
func (values *operationFlags) String() string { return strings.Join(*values, ",") }
|
||||
func (values *operationFlags) Set(value string) error {
|
||||
switch value {
|
||||
case "publication", "go", "build", "delivery", "rehearsal":
|
||||
default:
|
||||
return fmt.Errorf("unknown validation operation %q", value)
|
||||
}
|
||||
for _, existing := range *values {
|
||||
if existing == value {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
*values = append(*values, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parse accepts only commands that have been implemented by the executable.
|
||||
@@ -57,6 +79,27 @@ func Parse(args []string) (Invocation, error) {
|
||||
return Invocation{}, errors.New("a command is required")
|
||||
}
|
||||
switch args[0] {
|
||||
case string(ValidateAction):
|
||||
flags := flag.NewFlagSet(string(ValidateAction), flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
repository := flags.String("repo", ".", "repository directory")
|
||||
configPath := flags.String("config", "", "profile path")
|
||||
credentialContext := flags.String("context", "local", "credential context")
|
||||
var operations operationFlags
|
||||
flags.Var(&operations, "operation", "selected readiness operation")
|
||||
offline := flags.Bool("offline", false, "omit remote observations")
|
||||
assets := flags.String("assets", "", "existing build assets directory")
|
||||
structured := flags.Bool("json", false, "write structured JSON")
|
||||
if err := flags.Parse(args[1:]); err != nil {
|
||||
return Invocation{}, err
|
||||
}
|
||||
if flags.NArg() != 0 {
|
||||
return Invocation{}, errors.New("validate takes no positional arguments")
|
||||
}
|
||||
if *credentialContext != "local" && *credentialContext != "ci" {
|
||||
return Invocation{}, errors.New("validate --context must be local or ci")
|
||||
}
|
||||
return Invocation{Command: ValidateAction, Repository: *repository, ConfigPath: *configPath, Context: *credentialContext, Operations: operations, Offline: *offline, AssetsDir: *assets, JSON: *structured}, nil
|
||||
case string(ReconcileAction), string(PublicationAttemptsAction):
|
||||
action := Command(args[0])
|
||||
flags := flag.NewFlagSet(string(action), flag.ContinueOnError)
|
||||
@@ -181,6 +224,12 @@ type Settings struct {
|
||||
Validation *ValidationSettings `json:"validation,omitempty"`
|
||||
Build *BuildSettings `json:"build,omitempty"`
|
||||
Delivery *DeliverySettings `json:"delivery,omitempty"`
|
||||
Readiness *ReadinessSettings `json:"readiness,omitempty"`
|
||||
}
|
||||
|
||||
// ReadinessSettings lists files deliberately required for setup diagnostics.
|
||||
type ReadinessSettings struct {
|
||||
RequiredFiles []string `json:"required_files,omitempty"`
|
||||
}
|
||||
|
||||
type SourceSettings struct {
|
||||
|
||||
@@ -157,6 +157,18 @@ func TestLoadSettingsResolvesPathsAndPreservesArguments(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseValidateSelectsAndDeduplicatesOperations(t *testing.T) {
|
||||
invocation, err := Parse([]string{"validate", "--context", "ci", "--operation", "delivery", "--operation", "go", "--operation", "delivery", "--offline", "--assets", "dist", "--json"})
|
||||
if err != nil || invocation.Command != ValidateAction || invocation.Context != "ci" || !invocation.Offline || !invocation.JSON || invocation.AssetsDir != "dist" || len(invocation.Operations) != 2 || invocation.Operations[0] != "delivery" || invocation.Operations[1] != "go" {
|
||||
t.Fatalf("invocation = %+v, error = %v", invocation, err)
|
||||
}
|
||||
for _, args := range [][]string{{"validate", "v1.0.0"}, {"validate", "--context", "other"}, {"validate", "--operation", "other"}} {
|
||||
if _, err := Parse(args); err == nil {
|
||||
t.Errorf("Parse(%q) accepted invalid arguments", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadSettingsReportsObsoleteDeliveryCredentials(t *testing.T) {
|
||||
for _, test := range []struct{ field, replacement string }{
|
||||
{"token_file", "delivery.credentials.local.token_file"},
|
||||
|
||||
@@ -38,6 +38,9 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) int {
|
||||
if invocation.Command == cli.ReconcileAction || invocation.Command == cli.PublicationAttemptsAction {
|
||||
return runRecovery(ctx, invocation, stdout, stderr)
|
||||
}
|
||||
if invocation.Command == cli.ValidateAction {
|
||||
return runValidate(ctx, invocation, stdout)
|
||||
}
|
||||
if invocation.Command != cli.VersionCommand {
|
||||
invocation.Repository, err = git.RepositoryRoot(ctx, invocation.Repository)
|
||||
if err != nil {
|
||||
|
||||
162
cmd/releasekit/readiness.go
Normal file
162
cmd/releasekit/readiness.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.maximumdirect.net/releasekit"
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
"go.maximumdirect.net/releasekit/internal/artifact"
|
||||
"go.maximumdirect.net/releasekit/internal/git"
|
||||
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
||||
)
|
||||
|
||||
type readinessCheckDocument struct {
|
||||
ID string `json:"id"`
|
||||
Operation releasekit.ReadinessOperation `json:"operation"`
|
||||
Status releasekit.ReadinessStatus `json:"status"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
type readinessCredentialDocument struct {
|
||||
Context string `json:"context"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Declared bool `json:"declared"`
|
||||
}
|
||||
type readinessDocument struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
Command string `json:"command"`
|
||||
InputSource string `json:"input_source"`
|
||||
RepositoryDir string `json:"repository_dir,omitempty"`
|
||||
Operations []releasekit.ReadinessOperation `json:"operations"`
|
||||
Context string `json:"context"`
|
||||
Offline bool `json:"offline"`
|
||||
GitDestination string `json:"git_destination,omitempty"`
|
||||
DeliveryDestination string `json:"delivery_destination,omitempty"`
|
||||
Credentials []readinessCredentialDocument `json:"credentials,omitempty"`
|
||||
Checks []readinessCheckDocument `json:"checks"`
|
||||
}
|
||||
|
||||
func runValidate(ctx context.Context, invocation cli.Invocation, output io.Writer) int {
|
||||
report := releasekit.ValidationReport{InputSource: "working", Context: invocation.Context, Offline: invocation.Offline}
|
||||
for _, operation := range invocation.Operations {
|
||||
report.Operations = append(report.Operations, releasekit.ReadinessOperation(operation))
|
||||
}
|
||||
var reportErr error
|
||||
root, err := git.RepositoryRoot(ctx, invocation.Repository)
|
||||
if err != nil {
|
||||
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "repository", Operation: "configuration", Status: releasekit.ReadinessFailed, Reason: "required_input", Message: git.DisplayDiagnostic(err.Error()), Cause: err})
|
||||
reportErr = err
|
||||
} else {
|
||||
invocation.Repository = root
|
||||
settings, loadErr := cli.LoadSettings(root, invocation.ConfigPath)
|
||||
if loadErr != nil {
|
||||
report.RepositoryDir = root
|
||||
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "profile", Operation: "configuration", Status: releasekit.ReadinessFailed, Reason: "configuration", Message: git.DisplayDiagnostic(loadErr.Error()), Cause: loadErr})
|
||||
reportErr = loadErr
|
||||
} else {
|
||||
request := releasekit.ValidateRequest{RepositoryDir: root, ModuleDir: settings.ModuleDir, Source: sourceSpec(settings), Product: settings.Product, Remote: settings.Remote, ExpectedPushURL: settings.ExpectedPushURL, Context: invocation.Context, Offline: invocation.Offline, Build: configuredBuildSpec(settings.Build)}
|
||||
for _, operation := range invocation.Operations {
|
||||
request.Operations = append(request.Operations, releasekit.ReadinessOperation(operation))
|
||||
}
|
||||
if settings.Readiness != nil {
|
||||
request.RequiredFiles = append([]string(nil), settings.Readiness.RequiredFiles...)
|
||||
}
|
||||
wantsDelivery := len(invocation.Operations) == 0 && settings.Delivery != nil
|
||||
for _, operation := range invocation.Operations {
|
||||
if operation == "delivery" || operation == "rehearsal" {
|
||||
wantsDelivery = true
|
||||
}
|
||||
}
|
||||
if delivery := settings.Delivery; delivery != nil {
|
||||
request.Destination = &releasekit.DeliveryDestination{Forge: releasekit.Forge(delivery.Forge), BaseURL: delivery.BaseURL, Owner: delivery.Owner, Repository: delivery.Repository}
|
||||
if credentials := delivery.Credentials; credentials != nil {
|
||||
if credentials.Local != nil {
|
||||
request.Credentials = append(request.Credentials, releasekit.ReadinessCredential{Context: "local", Source: git.DisplayDiagnostic(credentials.Local.TokenFile), Declared: true})
|
||||
}
|
||||
if credentials.CI != nil {
|
||||
name := credentials.CI.TokenEnvironmentVariable
|
||||
if strings.TrimSpace(name) == "" {
|
||||
name = "RELEASEKIT_DELIVERY_TOKEN"
|
||||
}
|
||||
request.Credentials = append(request.Credentials, releasekit.ReadinessCredential{Context: "ci", Source: git.DisplayDiagnostic(name), Declared: true})
|
||||
}
|
||||
}
|
||||
if wantsDelivery {
|
||||
token, _, tokenErr := cli.ResolveDeliveryCredential(root, delivery.Credentials, cli.CredentialContext(invocation.Context))
|
||||
request.Destination.Token = token
|
||||
request.CredentialError = tokenErr
|
||||
}
|
||||
}
|
||||
var assetErr error
|
||||
assetSkipped := false
|
||||
if invocation.AssetsDir != "" {
|
||||
if !wantsDelivery {
|
||||
assetErr = fmt.Errorf("--assets requires delivery selection")
|
||||
} else {
|
||||
selection, selectionErr := releaseidentity.ResolveWorkingSource(root, settings.ModuleDir, settings.SourceSpec())
|
||||
if selectionErr != nil {
|
||||
assetSkipped = true
|
||||
} else {
|
||||
directory := artifact.OutputDirectory(selection.ModuleDir, invocation.AssetsDir)
|
||||
receipt, receiptErr := cli.LoadBuildReceipt(filepath.Join(directory, cli.BuildReceiptName))
|
||||
if receiptErr != nil {
|
||||
assetErr = receiptErr
|
||||
} else {
|
||||
request.Prebuilt = buildResultFromReceipt(receipt)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
report, reportErr = releasekit.Validate(ctx, request)
|
||||
if assetSkipped {
|
||||
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "prebuilt", Operation: releasekit.ReadinessDelivery, Status: releasekit.ReadinessSkipped, Reason: "dependency", Message: "selected source unavailable"})
|
||||
}
|
||||
if assetErr != nil {
|
||||
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "prebuilt", Operation: releasekit.ReadinessDelivery, Status: releasekit.ReadinessFailed, Reason: "configuration", Message: git.DisplayDiagnostic(assetErr.Error()), Cause: assetErr})
|
||||
reportErr = assetErr
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := writeReadinessReport(output, invocation.JSON, report); err != nil {
|
||||
return 1
|
||||
}
|
||||
if reportErr == nil {
|
||||
return 0
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return 1
|
||||
}
|
||||
for _, check := range report.Checks {
|
||||
if check.Status == releasekit.ReadinessFailed && (check.Reason == "configuration" || check.Reason == "required_input") {
|
||||
return 2
|
||||
}
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func writeReadinessReport(output io.Writer, structured bool, report releasekit.ValidationReport) error {
|
||||
document := readinessDocument{SchemaVersion: 1, Command: "validate", InputSource: report.InputSource, RepositoryDir: git.DisplayDiagnostic(report.RepositoryDir), Operations: append([]releasekit.ReadinessOperation{}, report.Operations...), Context: report.Context, Offline: report.Offline, GitDestination: git.DisplayURL(report.GitDestination), DeliveryDestination: git.DisplayDiagnostic(report.DeliveryDestination), Checks: make([]readinessCheckDocument, 0, len(report.Checks))}
|
||||
for _, credential := range report.Credentials {
|
||||
document.Credentials = append(document.Credentials, readinessCredentialDocument{Context: credential.Context, Source: git.DisplayDiagnostic(credential.Source), Declared: credential.Declared})
|
||||
}
|
||||
for _, check := range report.Checks {
|
||||
document.Checks = append(document.Checks, readinessCheckDocument{ID: check.ID, Operation: check.Operation, Status: check.Status, Reason: check.Reason, Message: git.DisplayDiagnostic(check.Message)})
|
||||
}
|
||||
if structured {
|
||||
return json.NewEncoder(output).Encode(document)
|
||||
}
|
||||
if _, err := fmt.Fprintf(output, "readiness source=%s context=%s offline=%t operations=%v\n", document.InputSource, document.Context, document.Offline, document.Operations); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, check := range document.Checks {
|
||||
if _, err := fmt.Fprintf(output, "%s %s [%s]: %s\n", check.Status, check.ID, check.Operation, check.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
246
cmd/releasekit/readiness_test.go
Normal file
246
cmd/releasekit/readiness_test.go
Normal file
@@ -0,0 +1,246 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
)
|
||||
|
||||
func TestValidateReportsUnavailableOnlineObservation(t *testing.T) {
|
||||
repo, _ := newSimplePublishRepository(t, false)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("unexpected remote write %s", r.Method)
|
||||
}
|
||||
http.Error(w, "unavailable", http.StatusServiceUnavailable)
|
||||
}))
|
||||
defer server.Close()
|
||||
profile := fmt.Sprintf(`{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":%q,"owner":"owner","repository":"example","credentials":{"ci":{}}}}`, server.URL)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
||||
t.Setenv("RELEASEKIT_DELIVERY_TOKEN", "fixture-token")
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 1 || !strings.Contains(output.String(), `"reason":"remote"`) || strings.Contains(output.String(), "fixture-token") {
|
||||
t.Fatalf("status=%d output=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAggregatesDestinationAndCredentialFailures(t *testing.T) {
|
||||
repo, _ := newSimplePublishRepository(t, false)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":"ssh://invalid","owner":"owner","repository":"example","credentials":{"local":{"token_file":"missing"}}}}`)
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "delivery", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 2 || !strings.Contains(output.String(), `"id":"delivery-inputs","operation":"delivery","status":"failed"`) || !strings.Contains(output.String(), `"id":"delivery-credential","operation":"delivery","status":"failed"`) || !strings.Contains(output.String(), `"id":"delivery-access","operation":"delivery","status":"skipped"`) {
|
||||
t.Fatalf("status=%d output=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePrebuiltInputsWithoutWorkingReleaseNote(t *testing.T) {
|
||||
repo := newCLIRepository(t)
|
||||
build := cli.BuildSettings{MainPackage: ".", BinaryName: "helper", Targets: []string{"linux/amd64"}}
|
||||
writeDeliveryProfile(t, repo, cli.Settings{Product: "Example Product", Build: &build, Delivery: &cli.DeliverySettings{Forge: "gitea", BaseURL: "https://gitea.example", Owner: "owner", Repository: "example"}})
|
||||
assets, binary := writeDeliveryReceipt(t, repo, build)
|
||||
if err := os.Remove(filepath.Join(repo, "docs", "releases", "v1.2.3.md")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
args := []string{"validate", "--repo", repo, "--operation", "delivery", "--offline", "--assets", assets, "--json"}
|
||||
if status := run(context.Background(), args, &output, &bytes.Buffer{}); status != 0 {
|
||||
t.Fatalf("status=%d report=%s", status, &output)
|
||||
}
|
||||
if err := os.WriteFile(binary, []byte("tampered artifact"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output.Reset()
|
||||
if status := run(context.Background(), args, &output, &bytes.Buffer{}); status != 2 || !strings.Contains(output.String(), `"id":"prebuilt"`) {
|
||||
t.Fatalf("tampered status=%d report=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateOnlineUsesReadOnlyDeliveryPreflight(t *testing.T) {
|
||||
repo, _ := newSimplePublishRepository(t, false)
|
||||
state := &deliveryServerState{}
|
||||
server := newDeliveryServer(t, state)
|
||||
defer server.Close()
|
||||
profile := fmt.Sprintf(`{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":%q,"owner":"owner","repository":"example","credentials":{"ci":{"token_environment_variable":"READINESS_TEST_TOKEN"}}}}`, server.URL)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
||||
t.Setenv("READINESS_TEST_TOKEN", "fixture-token")
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 0 || state.snapshot().apiRequests != 2 || state.snapshot().releaseCreates != 0 || state.snapshot().assetUploads != 0 || strings.Contains(output.String(), "fixture-token") {
|
||||
t.Fatalf("status=%d requests=%d creates=%d output=%s", status, state.snapshot().apiRequests, state.snapshot().releaseCreates, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateBuildChecksInputsWithoutBuilding(t *testing.T) {
|
||||
repo := newCLIRepository(t)
|
||||
writeRepositoryFile(t, repo, "main.go", "package main\nfunc main() {}\n")
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Example Product","build":{"main_package":".","binary_name":"helper","targets":["linux/amd64"]},"checks":[{"args":["false"]}]}`)
|
||||
shim := t.TempDir()
|
||||
log := filepath.Join(shim, "calls")
|
||||
script := "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$READINESS_GO_LOG\"\nif [ \"$1\" = version ]; then echo 'go version go1.27.4 linux/amd64'; exit 0; fi\nexit 99\n"
|
||||
if err := os.WriteFile(filepath.Join(shim, "go"), []byte(script), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("READINESS_GO_LOG", log)
|
||||
t.Setenv("PATH", shim+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "build", "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
calls, err := os.ReadFile(log)
|
||||
if err != nil || status != 0 || string(calls) != "version\n" || !strings.Contains(output.String(), `"id":"go-dependencies","operation":"go","status":"unchecked"`) {
|
||||
t.Fatalf("status=%d calls=%q error=%v output=%s", status, calls, err, &output)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(repo, "dist")); !os.IsNotExist(err) {
|
||||
t.Fatalf("validation created build outputs: %v", err)
|
||||
}
|
||||
writeRepositoryFile(t, repo, "go.mod", "module example.com/cli-candidate\n\ngo 1.28.0\n")
|
||||
output.Reset()
|
||||
status = run(context.Background(), []string{"validate", "--repo", repo, "--operation", "build", "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 2 || !strings.Contains(output.String(), "older than declared Go 1.28.0") {
|
||||
t.Fatalf("toolchain status=%d report=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateOfflineInspectsWorkingSourceWithoutReleaseNote(t *testing.T) {
|
||||
repo, remote := newSimplePublishRepository(t, false)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{},"readiness":{"required_files":["required.txt"]}}`)
|
||||
writeRepositoryFile(t, repo, "required.txt", "local input\n")
|
||||
before := cliGitOutput(t, remote, "show-ref", "--heads")
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
var report readinessDocument
|
||||
if err := json.Unmarshal(output.Bytes(), &report); err != nil {
|
||||
t.Fatalf("decode: %v: %s", err, output.String())
|
||||
}
|
||||
if status != 0 || report.SchemaVersion != 1 || report.Command != "validate" || report.InputSource != "working" || !report.Offline || len(report.Operations) != 1 || report.Operations[0] != "publication" {
|
||||
t.Fatalf("status=%d report=%+v", status, report)
|
||||
}
|
||||
found := false
|
||||
for _, check := range report.Checks {
|
||||
if check.ID == "git-main" && check.Status == "unchecked" && check.Reason == "offline" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("missing offline evidence: %+v", report.Checks)
|
||||
}
|
||||
if got := cliGitOutput(t, remote, "show-ref", "--heads"); got != before {
|
||||
t.Fatalf("validation changed remote refs: %s != %s", got, before)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePublicationReadsMainWithoutRequiringCurrentCommit(t *testing.T) {
|
||||
repo, remote := newSimplePublishRepository(t, false)
|
||||
before := cliGitOutput(t, remote, "rev-parse", "refs/heads/main")
|
||||
writeRepositoryFile(t, repo, "new-file", "unpublished\n")
|
||||
cliGitRun(t, repo, "add", "new-file")
|
||||
cliGitRun(t, repo, "commit", "-m", "unpublished work")
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{}}`)
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "publication", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 0 || !strings.Contains(output.String(), `"id":"git-main","operation":"publication","status":"passed"`) {
|
||||
t.Fatalf("status=%d output=%s", status, &output)
|
||||
}
|
||||
if got := cliGitOutput(t, remote, "rev-parse", "refs/heads/main"); got != before {
|
||||
t.Fatalf("validation changed remote main to %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateOfflineChecksGitIdentity(t *testing.T) {
|
||||
repo, _ := newSimplePublishRepository(t, false)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{}}`)
|
||||
cliGitRun(t, repo, "config", "--unset", "user.name")
|
||||
cliGitRun(t, repo, "config", "--unset", "user.email")
|
||||
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
|
||||
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
||||
t.Setenv("GIT_COMMITTER_NAME", "")
|
||||
t.Setenv("GIT_COMMITTER_EMAIL", "")
|
||||
t.Setenv("EMAIL", "")
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 2 || !strings.Contains(output.String(), `"id":"git-identity","operation":"publication","status":"failed"`) {
|
||||
t.Fatalf("status=%d output=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAggregatesInputsAndKeepsCredentialContextsSeparate(t *testing.T) {
|
||||
repo, _ := newSimplePublishRepository(t, false)
|
||||
profile := `{"product":"Config","source":{},"readiness":{"required_files":["absent"]},"delivery":{"forge":"gitea","base_url":"https://gitea.example","owner":"owner","repository":"project","credentials":{"local":{"token_file":"absent-token"},"ci":{"token_environment_variable":"READINESS_TEST_TOKEN"}}}}`
|
||||
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
||||
t.Setenv("READINESS_TEST_TOKEN", "secret-value")
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
var report readinessDocument
|
||||
if err := json.Unmarshal(output.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if status != 2 || strings.Contains(output.String(), "secret-value") {
|
||||
t.Fatalf("status=%d report=%s", status, &output)
|
||||
}
|
||||
required, destination := false, false
|
||||
for _, check := range report.Checks {
|
||||
if check.ID == "required-file" && check.Status == "failed" {
|
||||
required = true
|
||||
}
|
||||
if check.ID == "delivery-inputs" && check.Status == "passed" {
|
||||
destination = true
|
||||
}
|
||||
}
|
||||
if !required || !destination {
|
||||
t.Fatalf("checks = %+v", report.Checks)
|
||||
}
|
||||
writeRepositoryFile(t, repo, "absent", "present\n")
|
||||
output.Reset()
|
||||
status = run(context.Background(), []string{"validate", "--repo", repo, "--context", "local", "--operation", "delivery", "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 2 || strings.Contains(output.String(), "secret-value") || !strings.Contains(output.String(), "absent-token") {
|
||||
t.Fatalf("local status=%d report=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateReportsMalformedProfileAsJSON(t *testing.T) {
|
||||
repo := newCLIRepository(t)
|
||||
if err := os.WriteFile(filepath.Join(repo, "releasekit.json"), []byte("{"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--json"}, &output, &bytes.Buffer{})
|
||||
var report readinessDocument
|
||||
if err := json.Unmarshal(output.Bytes(), &report); err != nil || status != 2 || len(report.Checks) != 1 || report.Checks[0].ID != "profile" {
|
||||
t.Fatalf("status=%d report=%s err=%v", status, &output, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateSkipsAssetsWhenSourceIsUnavailable(t *testing.T) {
|
||||
repo := newCLIRepository(t)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Example","source":{"dir":"missing"},"delivery":{"forge":"gitea","base_url":"https://gitea.example","owner":"owner","repository":"example","credentials":{"ci":{}}}}`)
|
||||
t.Setenv("RELEASEKIT_DELIVERY_TOKEN", "fixture-token")
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--offline", "--assets", "dist", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 2 || !strings.Contains(output.String(), `"id":"prebuilt","operation":"delivery","status":"skipped"`) {
|
||||
t.Fatalf("status=%d output=%s", status, &output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRejectsInconsistentSourceSelection(t *testing.T) {
|
||||
repo := newCLIRepository(t)
|
||||
for _, profile := range []string{
|
||||
`{"product":"Example","go":{},"publication":{"mode":"go-module"},"version":{"format":"date"}}`,
|
||||
`{"product":"Example","source":{"tag_prefix":"bad..tag"}}`,
|
||||
} {
|
||||
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
||||
var output bytes.Buffer
|
||||
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "go", "--offline", "--json"}, &output, &bytes.Buffer{})
|
||||
if status != 2 || !strings.Contains(output.String(), `"id":"source","operation":"configuration","status":"failed"`) {
|
||||
t.Fatalf("status=%d output=%s profile=%s", status, &output, profile)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,8 @@ func TestReleaseKitSourceDeliveryWorkflowFromTaggedCheckout(t *testing.T) {
|
||||
cliGitRun(t, repository, "config", "user.email", "releasekit@example.test")
|
||||
// The workflow builds the tagged CLI, including changes under test that may
|
||||
// not yet be committed in the repository from which this fixture was cloned.
|
||||
for _, name := range []string{"cmd/releasekit/main.go", "cmd/releasekit/internal/cli/config.go", "cmd/releasekit/internal/cli/credentials.go"} {
|
||||
files := []string{"cmd/releasekit/main.go", "cmd/releasekit/readiness.go", "cmd/releasekit/internal/cli/config.go", "cmd/releasekit/internal/cli/credentials.go", "readiness.go", "internal/readiness/readiness.go", "internal/git/remote.go", "internal/git/source.go", "internal/gitea/backend.go", "internal/gotool/gotool.go"}
|
||||
for _, name := range files {
|
||||
contents, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(name)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -56,7 +57,7 @@ func TestReleaseKitSourceDeliveryWorkflowFromTaggedCheckout(t *testing.T) {
|
||||
if err := os.WriteFile(notePath, []byte(note), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cliGitRun(t, repository, "add", "docs/releases/"+version+".md", "cmd/releasekit/main.go", "cmd/releasekit/internal/cli/config.go", "cmd/releasekit/internal/cli/credentials.go")
|
||||
cliGitRun(t, repository, append([]string{"add", "docs/releases/" + version + ".md"}, files...)...)
|
||||
cliGitRun(t, repository, "commit", "-m", "prepare source-only delivery")
|
||||
|
||||
state := &deliveryServerState{data: deliveryServerData{product: "ReleaseKit"}}
|
||||
|
||||
Reference in New Issue
Block a user