Build from the selected module
This commit is contained in:
157
build_test.go
157
build_test.go
@@ -115,7 +115,7 @@ func main() {
|
||||
t.Setenv("GOARCH", "arm64")
|
||||
result, err := releasekit.Build(context.Background(), releasekit.BuildRequest{
|
||||
RepositoryDir: repository,
|
||||
Version: "v2.3.4",
|
||||
Version: "v1.2.3",
|
||||
Spec: releasekit.BuildSpec{
|
||||
MainPackage: "./cmd/app",
|
||||
BinaryName: "fixture",
|
||||
@@ -126,7 +126,7 @@ func main() {
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v; partial result = %#v", err, result)
|
||||
}
|
||||
if len(result.Artifacts) != 1 || result.Artifacts[0].Filename != "fixture-v2.3.4-windows-arm64.exe" {
|
||||
if len(result.Artifacts) != 1 || result.Artifacts[0].Filename != "fixture-v1.2.3-windows-arm64.exe" {
|
||||
t.Fatalf("build artifacts = %#v", result.Artifacts)
|
||||
}
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func main() { if len(os.Args) == 2 && os.Args[1] == "version" { fmt.Printf("fixt
|
||||
|
||||
result, err := releasekit.Build(context.Background(), releasekit.BuildRequest{
|
||||
RepositoryDir: repository,
|
||||
Version: "v2.3.4",
|
||||
Version: "v1.2.3",
|
||||
Spec: releasekit.BuildSpec{
|
||||
MainPackage: "./cmd/app",
|
||||
BinaryName: "fixture",
|
||||
@@ -242,6 +242,145 @@ func main() {}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildUsesNestedWorkingTreeModuleAndModuleRelativeOutput(t *testing.T) {
|
||||
repository := newBuildConsumer(t, "package main\nfunc main() {}\n")
|
||||
moduleDir := filepath.Join(repository, "integrations", "nested")
|
||||
writeNestedBuildConsumer(t, repository, "integrations/nested", "example.com/nestedbuild", `package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/buildinfo"
|
||||
)
|
||||
func main() {
|
||||
if len(os.Args) == 2 && os.Args[1] == "version" {
|
||||
if logPath := os.Getenv("RELEASEKIT_SMOKE_LOG"); logPath != "" {
|
||||
if executable, err := os.Executable(); err == nil {
|
||||
_ = os.WriteFile(logPath, []byte(executable), 0o600)
|
||||
}
|
||||
}
|
||||
fmt.Printf("nested-app %s\n", buildinfo.Version())
|
||||
}
|
||||
}
|
||||
`)
|
||||
outputDirectory := "artifacts"
|
||||
crossTarget := releasekit.Target{OS: "darwin", Arch: "arm64"}
|
||||
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
|
||||
crossTarget = releasekit.Target{OS: "linux", Arch: "amd64"}
|
||||
}
|
||||
nativeTarget := releasekit.Target{OS: runtime.GOOS, Arch: runtime.GOARCH}
|
||||
result, err := releasekit.Build(context.Background(), releasekit.BuildRequest{
|
||||
RepositoryDir: repository,
|
||||
ModuleDir: "integrations/nested",
|
||||
Version: "v1.2.3",
|
||||
Spec: releasekit.BuildSpec{
|
||||
MainPackage: "./cmd/app",
|
||||
BinaryName: "nested-app",
|
||||
Targets: []releasekit.Target{nativeTarget, crossTarget},
|
||||
},
|
||||
OutputDir: outputDirectory,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v; result = %#v", err, result)
|
||||
}
|
||||
if result.ModuleDir != moduleDir || result.ModulePath != "example.com/nestedbuild" || result.Tag != "integrations/nested/v1.2.3" {
|
||||
t.Fatalf("nested build identity = %#v", result)
|
||||
}
|
||||
wantOutput := filepath.Join(moduleDir, outputDirectory)
|
||||
if result.OutputDir != wantOutput || len(result.Artifacts) != 2 {
|
||||
t.Fatalf("nested build output = %#v, want directory %q and two artifacts", result, wantOutput)
|
||||
}
|
||||
var native string
|
||||
for _, artifact := range result.Artifacts {
|
||||
if filepath.Dir(artifact.Path) != wantOutput || !strings.Contains(artifact.Filename, "nested-app-v1.2.3-") {
|
||||
t.Fatalf("nested artifact = %#v", artifact)
|
||||
}
|
||||
if artifact.Target == nativeTarget {
|
||||
native = artifact.Path
|
||||
}
|
||||
}
|
||||
if native == "" {
|
||||
t.Fatalf("native artifact missing from %#v", result.Artifacts)
|
||||
}
|
||||
if output, err := exec.Command(native, "version").CombinedOutput(); err != nil || string(output) != "nested-app v1.2.3\n" {
|
||||
t.Fatalf("nested native version = %q, error = %v", output, err)
|
||||
}
|
||||
|
||||
localTemporary := filepath.Join(repository, "source-temporary")
|
||||
if err := os.Mkdir(localTemporary, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
crossOutput := filepath.Join(t.TempDir(), "cross-artifacts")
|
||||
smokeLog := filepath.Join(t.TempDir(), "smoke-executable")
|
||||
t.Setenv("RELEASEKIT_SMOKE_LOG", smokeLog)
|
||||
t.Setenv("TMPDIR", localTemporary)
|
||||
smokeResult, err := releasekit.Build(context.Background(), releasekit.BuildRequest{
|
||||
RepositoryDir: repository,
|
||||
ModuleDir: "integrations/nested",
|
||||
Version: "v1.2.3",
|
||||
Spec: releasekit.BuildSpec{
|
||||
MainPackage: "./cmd/app",
|
||||
BinaryName: "nested-app",
|
||||
Targets: []releasekit.Target{crossTarget},
|
||||
},
|
||||
OutputDir: crossOutput,
|
||||
})
|
||||
if err != nil || len(smokeResult.Artifacts) != 1 {
|
||||
t.Fatalf("nested cross build with source-local TMPDIR = %#v, %v", smokeResult, err)
|
||||
}
|
||||
smokePathBytes, err := os.ReadFile(smokeLog)
|
||||
if err != nil {
|
||||
t.Fatalf("read native smoke executable path: %v", err)
|
||||
}
|
||||
smokePath := string(smokePathBytes)
|
||||
relativeSmokePath, err := filepath.Rel(repository, smokePath)
|
||||
if err != nil || relativeSmokePath == "." || (!filepath.IsAbs(relativeSmokePath) && relativeSmokePath != ".." && !strings.HasPrefix(relativeSmokePath, ".."+string(filepath.Separator))) {
|
||||
t.Fatalf("native smoke output path %q is inside repository %q (relative %q, error %v)", smokePath, repository, relativeSmokePath, err)
|
||||
}
|
||||
if _, err := os.Stat(smokePath); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("native smoke output %q was not removed: %v", smokePath, err)
|
||||
}
|
||||
remaining, err := os.ReadDir(localTemporary)
|
||||
if err != nil || len(remaining) != 0 {
|
||||
t.Fatalf("native smoke output remained in source-local TMPDIR: %v %v", remaining, err)
|
||||
}
|
||||
|
||||
emptyResult, err := releasekit.Build(context.Background(), releasekit.BuildRequest{
|
||||
RepositoryDir: repository,
|
||||
ModuleDir: "integrations/nested",
|
||||
Version: "v1.2.3",
|
||||
})
|
||||
if err != nil || emptyResult.Compiler != "" || emptyResult.OutputDir != filepath.Join(moduleDir, "dist") {
|
||||
t.Fatalf("nested empty build = %#v, %v", emptyResult, err)
|
||||
}
|
||||
if _, err := os.Stat(emptyResult.OutputDir); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("empty nested build created dist: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildResolvesDirtyWorkingTreeDeclaration(t *testing.T) {
|
||||
repository := newBuildConsumer(t, "package main\nfunc main() {}\n")
|
||||
declarationPath := filepath.Join(repository, "go.mod")
|
||||
contents, err := os.ReadFile(declarationPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updated := strings.Replace(string(contents), "module example.com/buildfixture", "module example.com/changedbuild", 1)
|
||||
if updated == string(contents) {
|
||||
t.Fatal("fixture module declaration was not replaced")
|
||||
}
|
||||
writeBuildFixture(t, repository, "go.mod", updated)
|
||||
result, err := releasekit.Build(context.Background(), releasekit.BuildRequest{
|
||||
RepositoryDir: repository,
|
||||
Version: "v1.2.3",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.ModulePath != "example.com/changedbuild" || result.Tag != "v1.2.3" {
|
||||
t.Fatalf("dirty declaration identity = %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRegeneratesOwnedOutputsAndPreservesUnrelatedFiles(t *testing.T) {
|
||||
const source = `package main
|
||||
import("fmt";"gitea.maximumdirect.net/eric/ReleaseKit/buildinfo")
|
||||
@@ -563,6 +702,18 @@ func newBuildConsumer(t *testing.T, mainSource string) string {
|
||||
return repository
|
||||
}
|
||||
|
||||
func writeNestedBuildConsumer(t *testing.T, repository, moduleDir, modulePath, mainSource string) string {
|
||||
t.Helper()
|
||||
root, err := filepath.Abs(".")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
module := fmt.Sprintf("module %s\n\ngo 1.26.0\n\nrequire gitea.maximumdirect.net/eric/ReleaseKit v0.0.0\n\nreplace gitea.maximumdirect.net/eric/ReleaseKit => %s\n", modulePath, filepath.ToSlash(root))
|
||||
writeBuildFixture(t, repository, filepath.ToSlash(filepath.Join(moduleDir, "go.mod")), module)
|
||||
writeBuildFixture(t, repository, filepath.ToSlash(filepath.Join(moduleDir, "cmd/app/main.go")), mainSource)
|
||||
return filepath.Join(repository, filepath.FromSlash(moduleDir))
|
||||
}
|
||||
|
||||
func prepareCandidateBuild(t *testing.T, repository, mainSource string) {
|
||||
t.Helper()
|
||||
root, err := filepath.Abs(".")
|
||||
|
||||
Reference in New Issue
Block a user