1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-18 03:56:52 +02:00

fix: template main with gomod.proxy (#2834)

* refactor: rename file

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: template main with gomod.proxy

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2022-01-14 10:33:11 -03:00 committed by GitHub
parent b940cc276c
commit a6a6e11cc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 52 deletions

View File

@ -71,22 +71,6 @@ func (*Builder) WithDefaults(build config.Build) (config.Build, error) {
// Build builds a golang build.
func (*Builder) Build(ctx *context.Context, build config.Build, options api.Options) error {
tpl := tmpl.New(ctx)
if build.Main != "" {
m, err := tpl.Apply(build.Main)
if err != nil {
return err
}
build.Main = m
}
if build.UnproxiedMain != "" {
m, err := tpl.Apply(build.UnproxiedMain)
if err != nil {
return err
}
build.UnproxiedMain = m
}
if err := checkMain(build); err != nil {
return err
}

View File

@ -615,29 +615,6 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
Target: runtimeTarget,
}), errNoMain{"no-main"}.Error())
})
t.Run("using gomod.proxy and template", func(t *testing.T) {
ctx := newCtx(t)
ctx.Config.GoMod.Proxy = true
ctx.Env["Main"] = "."
ctx.Config.Builds[0].Dir = "dist/proxy/test"
ctx.Config.Builds[0].Main = "github.com/caarlos0/test"
ctx.Config.Builds[0].UnproxiedDir = "."
ctx.Config.Builds[0].UnproxiedMain = "{{ .Env.Main }}"
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
}), errNoMain{"no-main"}.Error())
})
t.Run("using gomod.proxy and invalid template", func(t *testing.T) {
ctx := newCtx(t)
ctx.Config.GoMod.Proxy = true
ctx.Config.Builds[0].Dir = "dist/proxy/test"
ctx.Config.Builds[0].Main = "github.com/caarlos0/test"
ctx.Config.Builds[0].UnproxiedDir = "."
ctx.Config.Builds[0].UnproxiedMain = "{{ .Env.NOPE }}"
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
}), `template: tmpl:1:7: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`)
})
}
func TestRunPipeWithProxiedRepo(t *testing.T) {
@ -731,19 +708,6 @@ func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
Target: runtimeTarget,
}))
})
t.Run("with template", func(t *testing.T) {
ctx.Config.Builds[0].Main = "{{ .Env.Main }}"
ctx.Env["Main"] = "."
require.NoError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
}))
})
t.Run("with invalid template", func(t *testing.T) {
ctx.Config.Builds[0].Main = "{{ .Env.NOPE }}"
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
}), `template: tmpl:1:7: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`)
})
}
func TestLdFlagsFullTemplate(t *testing.T) {

View File

@ -0,0 +1,30 @@
// Package prebuild provides a pipe that runs before the build and gomod pipes, mainly to resolve common templates.
package prebuild
import (
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe for build.
type Pipe struct{}
func (Pipe) String() string {
return "build prerequisites"
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
tpl := tmpl.New(ctx)
for i := range ctx.Config.Builds {
m, err := tpl.Apply(ctx.Config.Builds[i].Main)
if err != nil {
return err
}
if m == "" {
m = "."
}
ctx.Config.Builds[i].Main = m
}
return nil
}

View File

@ -0,0 +1,40 @@
package prebuild
import (
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
t.Run("good", func(t *testing.T) {
ctx := context.New(config.Project{
Env: []string{"FOO=bar"},
Builds: []config.Build{{Main: "{{ .Env.FOO }}"}},
})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "bar", ctx.Config.Builds[0].Main)
})
t.Run("empty", func(t *testing.T) {
ctx := context.New(config.Project{
Env: []string{"FOO="},
Builds: []config.Build{{Main: "{{ .Env.FOO }}"}},
})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, ".", ctx.Config.Builds[0].Main)
})
t.Run("bad", func(t *testing.T) {
ctx := context.New(config.Project{
Builds: []config.Build{{Main: "{{ .Env.FOO }}"}},
})
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1:7: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
})
}
func TestString(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
}

View File

@ -22,6 +22,7 @@ import (
"github.com/goreleaser/goreleaser/internal/pipe/gomod"
"github.com/goreleaser/goreleaser/internal/pipe/krew"
"github.com/goreleaser/goreleaser/internal/pipe/nfpm"
"github.com/goreleaser/goreleaser/internal/pipe/prebuild"
"github.com/goreleaser/goreleaser/internal/pipe/publish"
"github.com/goreleaser/goreleaser/internal/pipe/sbom"
"github.com/goreleaser/goreleaser/internal/pipe/scoop"
@ -53,6 +54,7 @@ var BuildPipeline = []Piper{
snapshot.Pipe{}, // snapshot version handling
dist.Pipe{}, // ensure ./dist is clean
gomod.Pipe{}, // setup gomod-related stuff
prebuild.Pipe{}, // run prebuild stuff
gomod.ProxyPipe{}, // proxy gomod if needed
effectiveconfig.Pipe{}, // writes the actual config (with defaults et al set) to dist
changelog.Pipe{}, // builds the release changelog