2017-03-26 01:24:38 +02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/config"
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLdFlagsFullTemplate(t *testing.T) {
|
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
2017-12-03 20:19:57 +02:00
|
|
|
Ldflags: `-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}} -X "main.foo={{.Env.FOO}}"`,
|
2017-07-01 18:21:07 +02:00
|
|
|
},
|
2017-03-26 01:24:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Git: context.GitInfo{
|
|
|
|
CurrentTag: "v1.2.3",
|
|
|
|
Commit: "123",
|
|
|
|
},
|
2017-04-30 22:44:52 +02:00
|
|
|
Version: "1.2.3",
|
|
|
|
Config: config,
|
2017-12-06 01:01:47 +02:00
|
|
|
Env: map[string]string{"FOO": "123"},
|
2017-03-26 01:24:38 +02:00
|
|
|
}
|
2017-07-01 18:21:07 +02:00
|
|
|
flags, err := ldflags(ctx, ctx.Config.Builds[0])
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, flags, "-s -w")
|
|
|
|
assert.Contains(t, flags, "-X main.version=1.2.3")
|
|
|
|
assert.Contains(t, flags, "-X main.tag=v1.2.3")
|
|
|
|
assert.Contains(t, flags, "-X main.commit=123")
|
|
|
|
assert.Contains(t, flags, "-X main.date=")
|
2017-12-03 20:19:57 +02:00
|
|
|
assert.Contains(t, flags, `-X "main.foo=123"`)
|
2017-03-26 01:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidTemplate(t *testing.T) {
|
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{Ldflags: "{invalid{.Template}}}{{}}}"},
|
2017-03-26 01:24:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config,
|
|
|
|
}
|
2017-07-01 18:21:07 +02:00
|
|
|
flags, err := ldflags(ctx, ctx.Config.Builds[0])
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, flags, "")
|
2017-03-26 01:24:38 +02:00
|
|
|
}
|