1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/build/build_test.go

247 lines
5.0 KiB
Go
Raw Normal View History

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"
"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"
"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-03-26 01:43:42 +02:00
assert.NotEmpty(t, Pipe{}.Description())
}
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) {
assert := assert.New(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-07-07 00:49:21 +02:00
assert.NoError(doBuild(ctx, ctx.Config.Builds[0], buildtarget.Runtime))
}
2017-03-26 02:29:38 +02:00
func TestRunFullPipe(t *testing.T) {
assert := assert.New(t)
2017-04-15 19:18:17 +02:00
folder, err := ioutil.TempDir("", "goreleasertest")
2017-03-26 02:29:38 +02:00
assert.NoError(err)
var binary = filepath.Join(folder, "testing")
var pre = filepath.Join(folder, "pre")
var post = filepath.Join(folder, "post")
var config = config.Project{
2017-03-26 02:31:16 +02:00
Dist: folder,
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-07-15 21:49:52 +02:00
assert.NoError(Pipe{}.Run(context.New(config)))
2017-03-26 02:29:38 +02:00
assert.True(exists(binary), binary)
assert.True(exists(pre), pre)
assert.True(exists(post), post)
}
func TestRunPipeFormatBinary(t *testing.T) {
assert := assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
var binary = filepath.Join(folder, "binary-testing")
var config = config.Project{
2017-07-03 06:11:46 +02:00
ProjectName: "testing",
Dist: folder,
Builds: []config.Build{
{
Binary: "testing",
Goos: []string{
runtime.GOOS,
},
Goarch: []string{
runtime.GOARCH,
},
},
},
Archive: config.Archive{
Format: "binary",
NameTemplate: "binary-{{.Binary}}",
},
}
2017-07-15 21:49:52 +02:00
assert.NoError(Pipe{}.Run(context.New(config)))
assert.True(exists(binary))
}
2017-04-24 19:29:40 +02:00
func TestRunPipeArmBuilds(t *testing.T) {
2017-04-24 19:27:21 +02:00
assert := assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
var binary = filepath.Join(folder, "armtesting")
var config = config.Project{
Dist: folder,
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-07-15 21:49:52 +02:00
assert.NoError(Pipe{}.Run(context.New(config)))
2017-04-24 19:27:21 +02:00
assert.True(exists(binary), binary)
}
2017-04-14 17:50:20 +02:00
func TestBuildFailed(t *testing.T) {
assert := assert.New(t)
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-07-15 21:49:52 +02:00
assert.Error(Pipe{}.Run(context.New(config)))
2017-04-14 17:50:20 +02:00
}
2017-04-14 17:38:30 +02:00
func TestRunPipeWithInvalidOS(t *testing.T) {
assert := assert.New(t)
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-07-15 21:49:52 +02:00
assert.NoError(Pipe{}.Run(context.New(config)))
2017-04-14 17:38:30 +02:00
}
func TestRunInvalidNametemplate(t *testing.T) {
var assert = assert.New(t)
2017-07-03 06:31:57 +02:00
for _, format := range []string{"tar.gz", "zip", "binary"} {
2017-07-15 21:49:52 +02:00
var config = config.Project{
ProjectName: "nameeeee",
Builds: []config.Build{
{
Binary: "namet{{.est}",
Flags: "-v",
Goos: []string{
runtime.GOOS,
},
Goarch: []string{
runtime.GOARCH,
2017-07-01 18:21:07 +02:00
},
},
2017-07-15 21:49:52 +02:00
},
Archive: config.Archive{
Format: format,
NameTemplate: "{{.Binary}",
},
2017-07-03 06:31:57 +02:00
}
2017-07-15 21:49:52 +02:00
assert.Error(Pipe{}.Run(context.New(config)))
}
}
func TestRunInvalidLdflags(t *testing.T) {
var assert = assert.New(t)
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-07-15 21:49:52 +02:00
assert.Error(Pipe{}.Run(context.New(config)))
}
2017-04-14 17:45:38 +02:00
func TestRunPipeFailingHooks(t *testing.T) {
assert := assert.New(t)
var config = config.Project{
2017-07-01 18:21:07 +02:00
Builds: []config.Build{
{
Hooks: config.Hooks{},
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-04-14 17:45:38 +02:00
t.Run("pre-hook", func(t *testing.T) {
2017-07-01 18:21:07 +02:00
ctx.Config.Builds[0].Hooks.Pre = "exit 1"
2017-04-14 17:45:38 +02:00
assert.Error(Pipe{}.Run(ctx))
})
t.Run("post-hook", func(t *testing.T) {
2017-07-01 18:21:07 +02:00
ctx.Config.Builds[0].Hooks.Post = "exit 1"
2017-04-14 17:45:38 +02:00
assert.Error(Pipe{}.Run(ctx))
})
}
2017-03-26 02:29:38 +02:00
func exists(file string) bool {
_, err := os.Stat(file)
return !os.IsNotExist(err)
}