2017-03-19 01:40:26 +02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2017-03-26 02:29:38 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-26 01:24:38 +02:00
|
|
|
"runtime"
|
2017-03-19 01:40:26 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-03-26 01:24:38 +02:00
|
|
|
"github.com/goreleaser/goreleaser/config"
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2017-07-07 00:49:21 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/buildtarget"
|
2017-11-18 18:14:39 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2017-03-19 01:40:26 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-05-11 15:44:44 +02:00
|
|
|
var emptyEnv []string
|
|
|
|
|
2017-03-26 01:49:29 +02:00
|
|
|
func TestPipeDescription(t *testing.T) {
|
2017-12-02 23:53:19 +02:00
|
|
|
assert.NotEmpty(t, Pipe{}.String())
|
2017-03-26 01:43:42 +02:00
|
|
|
}
|
|
|
|
|
2017-03-26 01:24:38 +02:00
|
|
|
func TestRun(t *testing.T) {
|
2017-07-07 00:49:21 +02:00
|
|
|
assert.NoError(t, run(buildtarget.Runtime, []string{"go", "list", "./..."}, emptyEnv))
|
2017-03-26 01:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunInvalidCommand(t *testing.T) {
|
2017-07-07 00:49:21 +02:00
|
|
|
assert.Error(t, run(buildtarget.Runtime, []string{"gggggo", "nope"}, emptyEnv))
|
2017-03-26 01:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuild(t *testing.T) {
|
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "testing",
|
|
|
|
Flags: "-n",
|
|
|
|
Env: []string{"BLAH=1"},
|
|
|
|
},
|
2017-03-26 01:24:38 +02:00
|
|
|
},
|
|
|
|
}
|
2017-07-15 21:49:52 +02:00
|
|
|
var ctx = context.New(config)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, doBuild(ctx, ctx.Config.Builds[0], buildtarget.Runtime))
|
2017-03-19 01:40:26 +02:00
|
|
|
}
|
2017-03-26 02:29:38 +02:00
|
|
|
|
|
|
|
func TestRunFullPipe(t *testing.T) {
|
2017-11-18 18:14:39 +02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeGoodMain(t, folder)
|
2017-12-18 00:25:54 +02:00
|
|
|
var binary = filepath.Join(folder, buildtarget.Runtime.String(), "testing")
|
2017-03-26 02:29:38 +02:00
|
|
|
var pre = filepath.Join(folder, "pre")
|
|
|
|
var post = filepath.Join(folder, "post")
|
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "testing",
|
|
|
|
Flags: "-v",
|
|
|
|
Ldflags: "-X main.test=testing",
|
|
|
|
Hooks: config.Hooks{
|
|
|
|
Pre: "touch " + pre,
|
|
|
|
Post: "touch " + post,
|
|
|
|
},
|
|
|
|
Goos: []string{
|
|
|
|
runtime.GOOS,
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
runtime.GOARCH,
|
|
|
|
},
|
2017-03-26 02:29:38 +02:00
|
|
|
},
|
|
|
|
},
|
2017-12-03 14:27:04 +02:00
|
|
|
Archive: config.Archive{
|
|
|
|
Replacements: map[string]string{
|
|
|
|
"linux": "linuxx",
|
|
|
|
"darwin": "darwinn",
|
|
|
|
},
|
|
|
|
},
|
2017-03-26 02:29:38 +02:00
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, Pipe{}.Run(context.New(config)))
|
|
|
|
assert.True(t, exists(binary), binary)
|
|
|
|
assert.True(t, exists(pre), pre)
|
|
|
|
assert.True(t, exists(post), post)
|
2017-03-26 02:29:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 19:29:40 +02:00
|
|
|
func TestRunPipeArmBuilds(t *testing.T) {
|
2017-11-18 18:14:39 +02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeGoodMain(t, folder)
|
2017-12-18 00:25:54 +02:00
|
|
|
var binary = filepath.Join(folder, "linuxarm6", "armtesting")
|
2017-04-24 19:27:21 +02:00
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "armtesting",
|
|
|
|
Flags: "-v",
|
|
|
|
Ldflags: "-X main.test=armtesting",
|
|
|
|
Goos: []string{
|
|
|
|
"linux",
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
"arm",
|
|
|
|
"arm64",
|
|
|
|
},
|
|
|
|
Goarm: []string{
|
|
|
|
"6",
|
|
|
|
},
|
2017-04-24 19:27:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, Pipe{}.Run(context.New(config)))
|
|
|
|
assert.True(t, exists(binary), binary)
|
2017-04-24 19:27:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:50:20 +02:00
|
|
|
func TestBuildFailed(t *testing.T) {
|
2017-11-18 18:14:39 +02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeGoodMain(t, folder)
|
2017-04-14 17:50:20 +02:00
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Flags: "-flag-that-dont-exists-to-force-failure",
|
|
|
|
Goos: []string{
|
|
|
|
runtime.GOOS,
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
runtime.GOARCH,
|
|
|
|
},
|
2017-04-14 17:50:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-11-18 18:14:39 +02:00
|
|
|
assertContainsError(t, Pipe{}.Run(context.New(config)), `flag provided but not defined: -flag-that-dont-exists-to-force-failure`)
|
2017-04-14 17:50:20 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:38:30 +02:00
|
|
|
func TestRunPipeWithInvalidOS(t *testing.T) {
|
2017-11-18 18:14:39 +02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeGoodMain(t, folder)
|
2017-04-14 17:38:30 +02:00
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Flags: "-v",
|
|
|
|
Goos: []string{
|
|
|
|
"windows",
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
"arm",
|
|
|
|
},
|
2017-04-14 17:38:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, Pipe{}.Run(context.New(config)))
|
2017-04-14 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 01:16:52 +02:00
|
|
|
func TestRunInvalidLdflags(t *testing.T) {
|
2017-11-18 18:14:39 +02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeGoodMain(t, folder)
|
2017-07-15 21:49:52 +02:00
|
|
|
var config = config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "nametest",
|
|
|
|
Flags: "-v",
|
|
|
|
Ldflags: "-s -w -X main.version={{.Version}",
|
|
|
|
Goos: []string{
|
|
|
|
runtime.GOOS,
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
runtime.GOARCH,
|
2017-04-27 01:16:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-11-18 18:14:39 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(context.New(config)), `template: ldflags:1: unexpected "}" in operand`)
|
2017-04-27 01:16:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:45:38 +02:00
|
|
|
func TestRunPipeFailingHooks(t *testing.T) {
|
2017-11-19 19:26:21 +02:00
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeGoodMain(t, folder)
|
|
|
|
var config = config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "hooks",
|
|
|
|
Hooks: config.Hooks{},
|
|
|
|
Goos: []string{
|
|
|
|
runtime.GOOS,
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
runtime.GOARCH,
|
2017-11-18 18:14:39 +02:00
|
|
|
},
|
|
|
|
},
|
2017-11-19 19:26:21 +02:00
|
|
|
},
|
2017-11-18 18:14:39 +02:00
|
|
|
}
|
|
|
|
t.Run("pre-hook", func(t *testing.T) {
|
2017-11-19 19:26:21 +02:00
|
|
|
var ctx = context.New(config)
|
2017-11-18 18:14:39 +02:00
|
|
|
ctx.Config.Builds[0].Hooks.Pre = "exit 1"
|
2017-11-19 19:26:21 +02:00
|
|
|
ctx.Config.Builds[0].Hooks.Post = "echo post"
|
2017-11-18 18:14:39 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `pre hook failed: `)
|
|
|
|
})
|
|
|
|
t.Run("post-hook", func(t *testing.T) {
|
2017-11-19 19:26:21 +02:00
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Config.Builds[0].Hooks.Pre = "echo pre"
|
2017-11-18 18:14:39 +02:00
|
|
|
ctx.Config.Builds[0].Hooks.Post = "exit 1"
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `post hook failed: `)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunPipeWithouMainFunc(t *testing.T) {
|
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
writeMainWithoutMainFunc(t, folder)
|
2017-04-14 17:45:38 +02:00
|
|
|
var config = config.Project{
|
2017-07-01 18:21:07 +02:00
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
2017-11-18 18:14:39 +02:00
|
|
|
Binary: "no-main",
|
|
|
|
Hooks: config.Hooks{},
|
2017-07-01 18:21:07 +02:00
|
|
|
Goos: []string{
|
|
|
|
runtime.GOOS,
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
runtime.GOARCH,
|
|
|
|
},
|
2017-04-14 17:45:38 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-07-15 21:49:52 +02:00
|
|
|
var ctx = context.New(config)
|
2017-11-19 19:26:21 +02:00
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = ""
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `build for no-main does not contain a main function`)
|
|
|
|
})
|
2017-11-19 20:06:26 +02:00
|
|
|
t.Run("not main.go", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = "foo.go"
|
2017-11-21 02:12:50 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `could not open foo.go: stat foo.go: no such file or directory`)
|
2017-11-19 20:06:26 +02:00
|
|
|
})
|
2017-11-18 18:14:39 +02:00
|
|
|
t.Run("glob", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = "."
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `build for no-main does not contain a main function`)
|
2017-04-14 17:45:38 +02:00
|
|
|
})
|
2017-11-18 18:14:39 +02:00
|
|
|
t.Run("fixed main.go", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = "main.go"
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `build for no-main does not contain a main function`)
|
2017-04-14 17:45:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-20 14:49:57 +02:00
|
|
|
func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
|
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
assert.NoError(t, ioutil.WriteFile(
|
|
|
|
filepath.Join(folder, "foo.go"),
|
|
|
|
[]byte("package main\nfunc main() {println(0)}"),
|
|
|
|
0644,
|
|
|
|
))
|
|
|
|
var config = config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "foo",
|
|
|
|
Hooks: config.Hooks{},
|
|
|
|
Goos: []string{
|
|
|
|
runtime.GOOS,
|
|
|
|
},
|
|
|
|
Goarch: []string{
|
|
|
|
runtime.GOARCH,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var ctx = context.New(config)
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = ""
|
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
})
|
|
|
|
t.Run("foo.go", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = "foo.go"
|
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
})
|
|
|
|
t.Run("glob", func(t *testing.T) {
|
|
|
|
ctx.Config.Builds[0].Main = "."
|
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-03 14: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 16: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")
|
|
|
|
}
|
|
|
|
|
2017-03-26 02:29:38 +02:00
|
|
|
func exists(file string) bool {
|
|
|
|
_, err := os.Stat(file)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
2017-11-18 18:14:39 +02:00
|
|
|
|
|
|
|
func writeMainWithoutMainFunc(t *testing.T, folder string) {
|
2017-11-20 14:49:57 +02:00
|
|
|
assert.NoError(t, ioutil.WriteFile(
|
|
|
|
filepath.Join(folder, "main.go"),
|
|
|
|
[]byte("package main\nconst a = 2\nfunc notMain() {println(0)}"),
|
|
|
|
0644,
|
|
|
|
))
|
2017-11-18 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeGoodMain(t *testing.T, folder string) {
|
|
|
|
assert.NoError(t, ioutil.WriteFile(
|
2017-11-20 14:49:57 +02:00
|
|
|
filepath.Join(folder, "main.go"),
|
|
|
|
[]byte("package main\nvar a = 1\nfunc main() {println(0)}"),
|
|
|
|
0644,
|
|
|
|
))
|
2017-11-18 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertContainsError(t *testing.T, err error, s string) {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), s)
|
|
|
|
}
|