104 lines
4.6 KiB
Go
104 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/ReleaseKit/cmd/releasekit/internal/cli"
|
|
)
|
|
|
|
func TestBuildAndPrebuiltDeliveryUseSameDirectoryBase(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name, module string
|
|
absolute bool
|
|
alias bool
|
|
}{
|
|
{name: "root relative"},
|
|
{name: "nested relative", module: "apps/widget"},
|
|
{name: "nested absolute", module: "apps/widget", absolute: true},
|
|
{name: "nested checkout alias", module: "apps/widget", absolute: true, alias: true},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
repository := newDeliveryBuildRepository(t)
|
|
moduleDirectory := repository
|
|
tag := "v1.2.3"
|
|
if tt.module != "" {
|
|
moduleDirectory = filepath.Join(repository, filepath.FromSlash(tt.module))
|
|
tag = tt.module + "/v1.2.3"
|
|
for _, name := range []string{"go.mod", "main.go", "docs/releases/v1.2.3.md"} {
|
|
contents, err := os.ReadFile(filepath.Join(repository, filepath.FromSlash(name)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writeRepositoryFile(t, repository, tt.module+"/"+name, string(contents))
|
|
}
|
|
}
|
|
writeRepositoryFile(t, repository, ".gitignore", "dist/\n")
|
|
state := &deliveryServerState{data: deliveryServerData{tag: tag}}
|
|
server := newDeliveryServer(t, state)
|
|
defer server.Close()
|
|
settings := cli.Settings{Product: "Example Product", ModuleDir: tt.module,
|
|
Build: &cli.BuildSettings{MainPackage: ".", BinaryName: "helper", Targets: []string{"linux/amd64"}},
|
|
Delivery: &cli.DeliverySettings{BaseURL: server.URL, Owner: "owner", Repository: "example"},
|
|
}
|
|
writeDeliveryProfile(t, repository, settings)
|
|
cliGitRun(t, repository, "add", ".")
|
|
cliGitRun(t, repository, "commit", "-m", "Prepare module source and output exclusion")
|
|
state.update(func(state *deliveryServerData) { state.commit = cliGitOutput(t, repository, "rev-parse", "HEAD") })
|
|
cliGitRun(t, repository, "tag", tag)
|
|
output, resolved := "dist", filepath.Join(moduleDirectory, "dist")
|
|
if tt.absolute {
|
|
output = filepath.Join(t.TempDir(), "assets")
|
|
resolved = output
|
|
}
|
|
var stdout, stderr bytes.Buffer
|
|
invocationRepository := repository
|
|
if tt.alias {
|
|
invocationRepository = filepath.Join(t.TempDir(), "checkout-alias")
|
|
if err := os.Symlink(repository, invocationRepository); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if status := run(context.Background(), []string{"build", "--repo", invocationRepository, "--output", output, "v1.2.3"}, &stdout, &stderr); status != 0 {
|
|
t.Fatalf("build status=%d, stderr=%s", status, stderr.String())
|
|
}
|
|
receipt, err := cli.LoadBuildReceipt(filepath.Join(resolved, cli.BuildReceiptName))
|
|
if err != nil || receipt.ModuleDir != moduleDirectory || receipt.Tag != tag {
|
|
t.Fatalf("build receipt=%#v, error=%v", receipt, err)
|
|
}
|
|
t.Setenv("RELEASEKIT_GITEA_TOKEN", "fixture-token")
|
|
args := []string{"deliver", "--repo", invocationRepository, "--assets", output, "v1.2.3"}
|
|
stdout.Reset()
|
|
stderr.Reset()
|
|
if status := run(context.Background(), args, &stdout, &stderr); status != 0 || state.snapshot().releaseCreates != 1 || state.snapshot().assetUploads != 2 {
|
|
t.Fatalf("delivery status=%d, creates=%d, uploads=%d, stderr=%s", status, state.snapshot().releaseCreates, state.snapshot().assetUploads, stderr.String())
|
|
}
|
|
if tt.alias {
|
|
if status := run(context.Background(), []string{"deliver", "--repo", repository, "--assets", output, "v1.2.3"}, &stdout, &stderr); status != 0 || state.snapshot().assetUploads != 2 {
|
|
t.Fatalf("physical-path retry status=%d, uploads=%d, stderr=%s", status, state.snapshot().assetUploads, stderr.String())
|
|
}
|
|
}
|
|
if tt.module != "" {
|
|
cliGitRun(t, repository, "tag", "v1.2.3")
|
|
wrongModule := settings
|
|
wrongModule.ModuleDir = ""
|
|
invocation := cli.Invocation{Repository: repository, AssetsDir: resolved, Version: "v1.2.3"}
|
|
requestsBefore := state.snapshot().apiRequests
|
|
if status := deliverWithSettings(context.Background(), invocation, wrongModule, &stdout, &stderr); status != 1 || state.snapshot().apiRequests != requestsBefore {
|
|
t.Fatalf("wrong module status=%d, requests=%d->%d, stderr=%s", status, requestsBefore, state.snapshot().apiRequests, stderr.String())
|
|
}
|
|
}
|
|
if err := os.WriteFile(receipt.Artifacts[0].Path, []byte("changed"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
requestsBefore := state.snapshot().apiRequests
|
|
if status := run(context.Background(), args, &stdout, &stderr); status != 1 || state.snapshot().apiRequests != requestsBefore {
|
|
t.Fatalf("changed content status=%d, requests=%d->%d, stderr=%s", status, requestsBefore, state.snapshot().apiRequests, stderr.String())
|
|
}
|
|
})
|
|
}
|
|
}
|