121 lines
5.3 KiB
Go
121 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
releasekit "go.maximumdirect.net/releasekit"
|
|
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
|
)
|
|
|
|
func TestSourceSelectionMapsToRequest(t *testing.T) {
|
|
empty := ""
|
|
settings := cli.Settings{
|
|
ModuleDir: "app",
|
|
Source: &cli.SourceSettings{TagPrefix: &empty},
|
|
Go: &struct{}{},
|
|
Version: &cli.VersionSettings{Format: "date", Prefix: &empty},
|
|
}
|
|
request := candidateRequest(cli.Invocation{}, settings, nil)
|
|
if request.Source == nil || !request.Source.GoEnabled || request.Source.Dir != "" || string(request.Source.Publication) != "" || string(request.Source.VersionFormat) != "date" || request.Source.VersionPrefix == nil || *request.Source.VersionPrefix != "" || request.Source.TagPrefix == nil || *request.Source.TagPrefix != "" || request.ModuleDir != "app" {
|
|
t.Fatalf("mapped selection = %+v, module directory = %q", request.Source, request.ModuleDir)
|
|
}
|
|
if sourceSpec(cli.Settings{}) != nil {
|
|
t.Fatal("legacy profile was converted to an explicit source selection")
|
|
}
|
|
}
|
|
|
|
func TestCIDeliveryChecksFullCalendarTagAgainstProfile(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
writeRepositoryFile(t, repo, "config/docs/releases/2026-12-01.md", "# Config 2026-12-01\n")
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{"dir":"config"},"version":{"format":"date"}}`)
|
|
cliGitRun(t, repo, "add", "config", "releasekit.json")
|
|
cliGitRun(t, repo, "commit", "-m", "configure source release")
|
|
commit := cliGitOutput(t, repo, "rev-parse", "HEAD")
|
|
cliGitRun(t, repo, "tag", "config/2026-12-01")
|
|
cliGitRun(t, repo, "tag", "other/2026-12-01")
|
|
for _, tt := range []struct {
|
|
tag, message string
|
|
status int
|
|
}{
|
|
{tag: "other/2026-12-01", status: 1, message: "does not match"},
|
|
{tag: "config/2026-12-01", status: 2, message: "delivery.forge"},
|
|
} {
|
|
t.Run(tt.tag, func(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
status := run(context.Background(), []string{"ci-deliver", "--repo", repo, "--prepared", "--expected-source", "config", "--commit", commit, tt.tag}, &stdout, &stderr)
|
|
if status != tt.status || !strings.Contains(stderr.String(), tt.message) {
|
|
t.Fatalf("status = %d, stdout = %q, stderr = %q", status, stdout.String(), stderr.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceCheckCommandUsesSelectedDirectoryAndEnvironment(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("POSIX shell check")
|
|
}
|
|
repo := t.TempDir()
|
|
source := filepath.Join(repo, "config")
|
|
if err := os.Mkdir(source, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := filepath.Join(t.TempDir(), "context")
|
|
settings := cli.Settings{Checks: []cli.CheckCommand{{
|
|
Args: []string{"sh", "-c", `printf '%s\n' "$PWD" "$RELEASEKIT_SOURCE_DIR" "$RELEASEKIT_MODULE_DIR" "$RELEASEKIT_MODULE_PATH" "$RELEASEKIT_TAG" > "$RESULT"`},
|
|
Env: map[string]string{"RESULT": result},
|
|
}}}
|
|
request := candidateRequest(cli.Invocation{}, settings, nil)
|
|
if err := request.AdditionalChecks(context.Background(), releasekit.Candidate{RepositoryDir: repo, SourceDir: source, Tag: "config/v1.0.0"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(result)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := strings.Join([]string{source, source, "", "", "config/v1.0.0", ""}, "\n")
|
|
if string(data) != want {
|
|
t.Fatalf("check context = %q, want %q", data, want)
|
|
}
|
|
}
|
|
|
|
func TestExpectedSourceChecksTheProfileSelection(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
writeRepositoryFile(t, repo, "config/data.json", "{}")
|
|
writeRepositoryFile(t, repo, "other/data.json", "{}")
|
|
cliGitRun(t, repo, "add", "config", "other")
|
|
cliGitRun(t, repo, "commit", "-m", "add source directories")
|
|
settings := cli.Settings{Source: &cli.SourceSettings{Dir: "config"}}
|
|
if err := checkExpectedSelection(context.Background(), repo, "config", "config", settings); err != nil {
|
|
t.Fatalf("matching expected source: %v", err)
|
|
}
|
|
if err := checkExpectedSelection(context.Background(), repo, "other", "config", settings); err == nil {
|
|
t.Fatal("accepted conflicting expected directories")
|
|
}
|
|
}
|
|
|
|
func TestCalendarBuildWritesIdentityReceipt(t *testing.T) {
|
|
repo := newCLIBuildRepository(t, `package main
|
|
import ("fmt"; "os"; "go.maximumdirect.net/releasekit/buildinfo")
|
|
func main() { if len(os.Args) == 2 && os.Args[1] == "version" { fmt.Println("fixture", buildinfo.Version()) } }
|
|
`)
|
|
profile := fmt.Sprintf(`{"product":"Example Product","go":{},"version":{"format":"date"},"build":{"main_package":"./cmd/app","binary_name":"fixture","targets":[%q]}}`, runtime.GOOS+"/"+runtime.GOARCH)
|
|
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
|
output := t.TempDir()
|
|
var stdout, stderr bytes.Buffer
|
|
status := run(context.Background(), []string{"build", "--repo", repo, "--output", output, "2026-12-01.1"}, &stdout, &stderr)
|
|
if status != 0 {
|
|
t.Fatalf("build status = %d, stdout = %q, stderr = %q", status, stdout.String(), stderr.String())
|
|
}
|
|
receipt, err := cli.LoadBuildReceipt(filepath.Join(output, cli.BuildReceiptName))
|
|
if err != nil || receipt.SchemaVersion != 2 || receipt.SourceDir != repo || receipt.SourceSubdir != "." || !receipt.GoEnabled || receipt.Publication != "source" || receipt.VersionFormat != "date" || receipt.VersionPrefix != "" || receipt.IdentityKind != "production" || receipt.Version != "2026-12-01.1" || receipt.Tag != "2026-12-01.1" {
|
|
t.Fatalf("calendar receipt = %+v, error = %v", receipt, err)
|
|
}
|
|
}
|