2017-03-18 20:40:26 -03:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2018-01-26 18:59:38 -02:00
|
|
|
"errors"
|
2017-03-25 21:29:38 -03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-18 20:40:26 -03:00
|
|
|
"testing"
|
|
|
|
|
2018-01-22 01:10:17 -02:00
|
|
|
api "github.com/goreleaser/goreleaser/build"
|
2017-03-25 20:24:38 -03:00
|
|
|
"github.com/goreleaser/goreleaser/config"
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2018-01-23 00:22:16 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-11-18 14:14:39 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2017-03-18 20:40:26 -03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-01-26 18:59:38 -02:00
|
|
|
type fakeBuilder struct {
|
|
|
|
fail bool
|
|
|
|
}
|
2018-01-22 01:10:17 -02:00
|
|
|
|
2018-01-26 19:35:12 -02:00
|
|
|
func (*fakeBuilder) WithDefaults(build config.Build) config.Build {
|
2018-01-22 01:10:17 -02:00
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
2018-01-26 18:59:38 -02:00
|
|
|
var errFailedBuild = errors.New("fake builder failed")
|
|
|
|
|
|
|
|
func (f *fakeBuilder) Build(ctx *context.Context, build config.Build, options api.Options) error {
|
|
|
|
if f.fail {
|
|
|
|
return errFailedBuild
|
|
|
|
}
|
2018-01-23 00:22:16 -02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{})
|
2018-01-22 01:10:17 -02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
api.Register("fake", &fakeBuilder{})
|
2018-01-26 18:59:38 -02:00
|
|
|
api.Register("fakeFail", &fakeBuilder{
|
|
|
|
fail: true,
|
|
|
|
})
|
2018-01-22 01:10:17 -02:00
|
|
|
}
|
|
|
|
|
2017-03-25 20:49:29 -03:00
|
|
|
func TestPipeDescription(t *testing.T) {
|
2017-12-02 19:53:19 -02:00
|
|
|
assert.NotEmpty(t, Pipe{}.String())
|
2017-03-25 20:43:42 -03:00
|
|
|
}
|
|
|
|
|
2017-03-25 20:24:38 -03:00
|
|
|
func TestBuild(t *testing.T) {
|
|
|
|
var config = config.Project{
|
2017-07-01 13:21:07 -03:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
2018-01-22 01:10:17 -02:00
|
|
|
Lang: "fake",
|
2017-07-01 13:21:07 -03:00
|
|
|
Binary: "testing",
|
|
|
|
Flags: "-n",
|
|
|
|
Env: []string{"BLAH=1"},
|
|
|
|
},
|
2017-03-25 20:24:38 -03:00
|
|
|
},
|
|
|
|
}
|
2017-07-15 16:49:52 -03:00
|
|
|
var ctx = context.New(config)
|
2018-01-22 01:10:17 -02:00
|
|
|
assert.NoError(t, doBuild(ctx, ctx.Config.Builds[0], "darwin_amd64"))
|
2017-03-18 20:40:26 -03:00
|
|
|
}
|
2017-03-25 21:29:38 -03:00
|
|
|
|
2018-01-26 18:59:38 -02:00
|
|
|
func TestRunPipe(t *testing.T) {
|
|
|
|
var config = config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Lang: "fake",
|
|
|
|
Binary: "testing",
|
|
|
|
Flags: "-v",
|
|
|
|
Ldflags: "-X main.test=testing",
|
|
|
|
Targets: []string{"whatever"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var ctx = context.New(config)
|
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
assert.Len(t, ctx.Artifacts.List(), 1)
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:29:38 -03:00
|
|
|
func TestRunFullPipe(t *testing.T) {
|
2017-11-18 14:14:39 -02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
2017-03-25 21:29:38 -03:00
|
|
|
var pre = filepath.Join(folder, "pre")
|
|
|
|
var post = filepath.Join(folder, "post")
|
|
|
|
var config = config.Project{
|
2017-07-01 13:21:07 -03:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
2018-01-22 01:10:17 -02:00
|
|
|
Lang: "fake",
|
2017-07-01 13:21:07 -03:00
|
|
|
Binary: "testing",
|
|
|
|
Flags: "-v",
|
|
|
|
Ldflags: "-X main.test=testing",
|
|
|
|
Hooks: config.Hooks{
|
|
|
|
Pre: "touch " + pre,
|
|
|
|
Post: "touch " + post,
|
|
|
|
},
|
2018-01-23 00:22:16 -02:00
|
|
|
Targets: []string{"whatever"},
|
2017-03-25 21:29:38 -03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-12-18 21:04:25 -02:00
|
|
|
var ctx = context.New(config)
|
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
assert.Len(t, ctx.Artifacts.List(), 1)
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.True(t, exists(pre), pre)
|
|
|
|
assert.True(t, exists(post), post)
|
2017-03-25 21:29:38 -03:00
|
|
|
}
|
|
|
|
|
2018-01-26 18:59:38 -02:00
|
|
|
func TestRunFullPipeFail(t *testing.T) {
|
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
var pre = filepath.Join(folder, "pre")
|
|
|
|
var post = filepath.Join(folder, "post")
|
|
|
|
var config = config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Lang: "fakeFail",
|
|
|
|
Binary: "testing",
|
|
|
|
Flags: "-v",
|
|
|
|
Ldflags: "-X main.test=testing",
|
|
|
|
Hooks: config.Hooks{
|
|
|
|
Pre: "touch " + pre,
|
|
|
|
Post: "touch " + post,
|
|
|
|
},
|
|
|
|
Targets: []string{"whatever"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var ctx = context.New(config)
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), errFailedBuild.Error())
|
|
|
|
assert.Len(t, ctx.Artifacts.List(), 0)
|
|
|
|
assert.True(t, exists(pre), pre)
|
|
|
|
assert.False(t, exists(post), post)
|
|
|
|
}
|
|
|
|
|
2017-04-14 12:45:38 -03:00
|
|
|
func TestRunPipeFailingHooks(t *testing.T) {
|
2017-11-19 15:26:21 -02:00
|
|
|
var config = config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
2018-01-23 00:22:16 -02:00
|
|
|
Lang: "fake",
|
|
|
|
Binary: "hooks",
|
|
|
|
Hooks: config.Hooks{},
|
|
|
|
Targets: []string{"whatever"},
|
2017-11-18 14:14:39 -02:00
|
|
|
},
|
2017-11-19 15:26:21 -02:00
|
|
|
},
|
2017-11-18 14:14:39 -02:00
|
|
|
}
|
|
|
|
t.Run("pre-hook", func(t *testing.T) {
|
2017-11-19 15:26:21 -02:00
|
|
|
var ctx = context.New(config)
|
2017-11-18 14:14:39 -02:00
|
|
|
ctx.Config.Builds[0].Hooks.Pre = "exit 1"
|
2017-11-19 15:26:21 -02:00
|
|
|
ctx.Config.Builds[0].Hooks.Post = "echo post"
|
2017-11-18 14:14:39 -02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `pre hook failed: `)
|
|
|
|
})
|
|
|
|
t.Run("post-hook", func(t *testing.T) {
|
2017-11-19 15:26:21 -02:00
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Config.Builds[0].Hooks.Pre = "echo pre"
|
2017-11-18 14:14:39 -02:00
|
|
|
ctx.Config.Builds[0].Hooks.Post = "exit 1"
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `post hook failed: `)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-03 10:27:04 -02:00
|
|
|
func TestDefaultNoBuilds(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultEmptyBuild(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Builds: []config.Build{
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
var build = ctx.Config.Builds[0]
|
|
|
|
assert.Equal(t, ctx.Config.Release.GitHub.Name, build.Binary)
|
|
|
|
assert.Equal(t, ".", build.Main)
|
|
|
|
assert.Equal(t, []string{"linux", "darwin"}, build.Goos)
|
|
|
|
assert.Equal(t, []string{"amd64", "386"}, build.Goarch)
|
|
|
|
assert.Equal(t, []string{"6"}, build.Goarm)
|
|
|
|
assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}", build.Ldflags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultPartialBuilds(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "bar",
|
|
|
|
Goos: []string{"linux"},
|
|
|
|
Main: "./cmd/main.go",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Binary: "foo",
|
|
|
|
Ldflags: "-s -w",
|
|
|
|
Goarch: []string{"386"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
t.Run("build0", func(t *testing.T) {
|
|
|
|
var build = ctx.Config.Builds[0]
|
|
|
|
assert.Equal(t, "bar", build.Binary)
|
|
|
|
assert.Equal(t, "./cmd/main.go", build.Main)
|
|
|
|
assert.Equal(t, []string{"linux"}, build.Goos)
|
|
|
|
assert.Equal(t, []string{"amd64", "386"}, build.Goarch)
|
|
|
|
assert.Equal(t, []string{"6"}, build.Goarm)
|
|
|
|
assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}", build.Ldflags)
|
|
|
|
})
|
|
|
|
t.Run("build1", func(t *testing.T) {
|
|
|
|
var build = ctx.Config.Builds[1]
|
|
|
|
assert.Equal(t, "foo", build.Binary)
|
|
|
|
assert.Equal(t, ".", build.Main)
|
|
|
|
assert.Equal(t, []string{"linux", "darwin"}, build.Goos)
|
|
|
|
assert.Equal(t, []string{"386"}, build.Goarch)
|
|
|
|
assert.Equal(t, []string{"6"}, build.Goarm)
|
|
|
|
assert.Equal(t, "-s -w", build.Ldflags)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-03 12:27:26 -02:00
|
|
|
func TestDefaultFillSingleBuild(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SingleBuild: config.Build{
|
|
|
|
Main: "testreleaser",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Len(t, ctx.Config.Builds, 1)
|
|
|
|
assert.Equal(t, ctx.Config.Builds[0].Binary, "foo")
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:44:06 -02:00
|
|
|
func TestExtWindows(t *testing.T) {
|
|
|
|
assert.Equal(t, ".exe", extFor("windows_amd64"))
|
|
|
|
assert.Equal(t, ".exe", extFor("windows_386"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtOthers(t *testing.T) {
|
|
|
|
assert.Empty(t, "", extFor("linux_amd64"))
|
|
|
|
assert.Empty(t, "", extFor("linuxwin_386"))
|
|
|
|
assert.Empty(t, "", extFor("winasdasd_sad"))
|
|
|
|
}
|
2018-01-22 01:10:17 -02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Helpers
|
|
|
|
//
|
|
|
|
|
|
|
|
func exists(file string) bool {
|
|
|
|
_, err := os.Stat(file)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|