1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00

fix: build --single-target (#4370)

closes #4369
This commit is contained in:
Carlos Alexandro Becker 2023-10-14 18:58:03 -03:00 committed by GitHub
parent c72b730258
commit 95c990b166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)
type buildCmd struct {
@ -236,18 +237,34 @@ func setupBuildSingleTarget(ctx *context.Context) {
if len(ctx.Config.Builds) == 0 {
ctx.Config.Builds = append(ctx.Config.Builds, config.Build{})
}
for i := range ctx.Config.Builds {
build := &ctx.Config.Builds[i]
var keep []config.Build
for _, build := range ctx.Config.Builds {
if !shouldBuild(build, goos, goarch) {
continue
}
build.Goos = []string{goos}
build.Goarch = []string{goarch}
build.Goarm = nil
build.Gomips = nil
build.Goamd64 = nil
build.Targets = nil
keep = append(keep, build)
}
ctx.Config.Builds = keep
ctx.Config.UniversalBinaries = nil
}
func shouldBuild(build config.Build, goos, goarch string) bool {
fmt.Println("AQUI", build)
if len(build.Targets) > 0 {
return slices.ContainsFunc(build.Targets, func(e string) bool {
return strings.HasPrefix(e, fmt.Sprintf("%s_%s", goos, goarch))
})
}
return slices.Contains(build.Goos, goos) &&
slices.Contains(build.Goarch, goarch)
}
func setupBuildID(ctx *context.Context, ids []string) error {
if len(ctx.Config.Builds) < 2 {
log.Warn("single build in config, '--id' ignored")

View File

@ -133,7 +133,17 @@ func TestSetupPipeline(t *testing.T) {
func TestBuildFlags(t *testing.T) {
setup := func(opts buildOpts) *context.Context {
ctx := testctx.New()
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
Goos: []string{runtime.GOOS},
Goarch: []string{runtime.GOARCH},
},
{
Targets: []string{"linux_arm64"},
},
},
})
require.NoError(t, setupBuildContext(ctx, opts))
return ctx
}
@ -186,6 +196,7 @@ func TestBuildFlags(t *testing.T) {
t.Run("runtime", func(t *testing.T) {
result := setup(opts)
require.Len(t, result.Config.Builds, 1)
require.Equal(t, []string{runtime.GOOS}, result.Config.Builds[0].Goos)
require.Equal(t, []string{runtime.GOARCH}, result.Config.Builds[0].Goarch)
})
@ -194,6 +205,7 @@ func TestBuildFlags(t *testing.T) {
t.Setenv("GOOS", "linux")
t.Setenv("GOARCH", "arm64")
result := setup(opts)
require.Len(t, result.Config.Builds, 1)
require.Equal(t, []string{"linux"}, result.Config.Builds[0].Goos)
require.Equal(t, []string{"arm64"}, result.Config.Builds[0].Goarch)
})
@ -306,6 +318,7 @@ func TestBuildSingleTargetWithSpecificTargets(t *testing.T) {
t.Setenv("GOOS", "darwin")
t.Setenv("GOARCH", "amd64")
setupBuildSingleTarget(ctx)
require.Len(t, ctx.Config.Builds, 1)
require.Equal(t, config.Build{
Goos: []string{"darwin"},
Goarch: []string{"amd64"},
@ -313,6 +326,26 @@ func TestBuildSingleTargetWithSpecificTargets(t *testing.T) {
require.Nil(t, ctx.Config.UniversalBinaries)
}
func TestBuildSingleTargetNoMatch(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",
Builds: []config.Build{
{
Goos: []string{"linux", "darwin"},
Goarch: []string{"amd64", "arm64"},
Goamd64: []string{"v1", "v2"},
Goarm: []string{"6"},
Gomips: []string{"anything"},
},
},
})
t.Setenv("GOOS", "windows")
t.Setenv("GOARCH", "amd64")
setupBuildSingleTarget(ctx)
require.Empty(t, ctx.Config.Builds)
}
func TestBuildSingleTargetRemoveOtherOptions(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",