1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2026-05-16 09:20:15 +02:00

Merge pull request #152 from goreleaser/tests

More tests
This commit is contained in:
Carlos Alexandro Becker
2017-03-25 21:33:20 -03:00
committed by GitHub
11 changed files with 145 additions and 55 deletions
+2 -2
View File
@@ -7,11 +7,11 @@ setup: ## Install all the build and lint dependencies
go get -u github.com/golang/dep/...
go get -u github.com/pierrre/gotestcover
go get -u golang.org/x/tools/cmd/cover
dep ensure
dep ensure
gometalinter --install
test: ## Run all the tests
gotestcover $(TEST_OPTIONS) -coverprofile=coverage.out $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=30s
gotestcover $(TEST_OPTIONS) -covermode=count -coverprofile=coverage.out $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=30s
fmt: ## gofmt and goimports all go files
find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done
+3
View File
@@ -75,6 +75,9 @@ type Project struct {
Build Build
Archive Archive
FPM FPM `yaml:"fpm"`
// test only property indicating the path to the dist folder
Dist string `yaml:"-"`
}
// Load config file
+1 -1
View File
@@ -39,7 +39,7 @@ type Archive interface {
}
func create(name string, ctx *context.Context) error {
folder := filepath.Join("dist", name)
folder := filepath.Join(ctx.Config.Dist, name)
file, err := os.Create(folder + "." + ctx.Config.Archive.Format)
log.Println("Creating", file.Name())
if err != nil {
+6 -1
View File
@@ -186,7 +186,12 @@ func dataFor(
if file == "" {
return result, ErrNoDarwin64Build
}
sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format)
sum, err := sha256sum.For(
filepath.Join(
ctx.Config.Dist,
file+"."+ctx.Config.Archive.Format,
),
)
if err != nil {
return
}
+6 -45
View File
@@ -5,6 +5,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
@@ -62,7 +63,11 @@ func (Pipe) Run(ctx *context.Context) error {
func build(name, goos, goarch string, ctx *context.Context) error {
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Version
output := "dist/" + name + "/" + ctx.Config.Build.Binary + extFor(goos)
output := filepath.Join(
ctx.Config.Dist,
name,
ctx.Config.Build.Binary+extFor(goos),
)
log.Println("Building", output)
cmd := []string{"go", "build"}
if ctx.Config.Build.Flags != "" {
@@ -84,47 +89,3 @@ func run(goos, goarch string, command []string) error {
}
return nil
}
// list from https://golang.org/doc/install/source#environment
var valids = []string{
"androidarm",
"darwin386",
"darwinamd64",
"darwinarm",
"darwinarm64",
"dragonflyamd64",
"freebsd386",
"freebsdamd64",
"freebsdarm",
"linux386",
"linuxamd64",
"linuxarm",
"linuxarm64",
"linuxppc64",
"linuxppc64le",
"linuxmips",
"linuxmipsle",
"linuxmips64",
"linuxmips64le",
"netbsd386",
"netbsdamd64",
"netbsdarm",
"openbsd386",
"openbsdamd64",
"openbsdarm",
"plan9386",
"plan9amd64",
"solarisamd64",
"windows386",
"windowsamd64",
}
func valid(goos, goarch string) bool {
var s = goos + goarch
for _, a := range valids {
if a == s {
return true
}
}
return false
}
+66 -4
View File
@@ -1,8 +1,14 @@
package build
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/stretchr/testify/assert"
)
@@ -10,8 +16,64 @@ func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.Description())
}
func TestValid(t *testing.T) {
assert.True(t, valid("windows", "386"))
assert.True(t, valid("linux", "386"))
assert.False(t, valid("windows", "arm"))
func TestRun(t *testing.T) {
assert.NoError(t, run(runtime.GOOS, runtime.GOARCH, []string{"go", "list", "./..."}))
}
func TestRunInvalidCommand(t *testing.T) {
assert.Error(t, run(runtime.GOOS, runtime.GOARCH, []string{"gggggo", "nope"}))
}
func TestBuild(t *testing.T) {
assert := assert.New(t)
var config = config.Project{
Build: config.Build{
Binary: "testing",
Flags: "-n",
},
}
var ctx = &context.Context{
Config: config,
}
assert.NoError(build("build_test", runtime.GOOS, runtime.GOARCH, ctx))
}
func TestRunFullPipe(t *testing.T) {
assert := assert.New(t)
folder, err := ioutil.TempDir("", "gorelasertest")
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{
Dist: folder,
Build: 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,
},
},
}
var ctx = &context.Context{
Config: config,
Archives: map[string]string{},
}
assert.NoError(Pipe{}.Run(ctx))
assert.True(exists(binary), binary)
assert.True(exists(pre), pre)
assert.True(exists(post), post)
}
func exists(file string) bool {
_, err := os.Stat(file)
return !os.IsNotExist(err)
}
+45
View File
@@ -0,0 +1,45 @@
package build
// list from https://golang.org/doc/install/source#environment
var valids = []string{
"androidarm",
"darwin386",
"darwinamd64",
"darwinarm",
"darwinarm64",
"dragonflyamd64",
"freebsd386",
"freebsdamd64",
"freebsdarm",
"linux386",
"linuxamd64",
"linuxarm",
"linuxarm64",
"linuxppc64",
"linuxppc64le",
"linuxmips",
"linuxmipsle",
"linuxmips64",
"linuxmips64le",
"netbsd386",
"netbsdamd64",
"netbsdarm",
"openbsd386",
"openbsdamd64",
"openbsdarm",
"plan9386",
"plan9amd64",
"solarisamd64",
"windows386",
"windowsamd64",
}
func valid(goos, goarch string) bool {
var s = goos + goarch
for _, a := range valids {
if a == s {
return true
}
}
return false
}
+13
View File
@@ -0,0 +1,13 @@
package build
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestValid(t *testing.T) {
assert.True(t, valid("windows", "386"))
assert.True(t, valid("linux", "386"))
assert.False(t, valid("windows", "arm"))
}
+1
View File
@@ -82,6 +82,7 @@ func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Brew.Install == "" {
ctx.Config.Brew.Install = "bin.install \"" + ctx.Config.Build.Binary + "\""
}
ctx.Config.Dist = "dist"
return nil
}
+1 -1
View File
@@ -54,7 +54,7 @@ func (Pipe) Run(ctx *context.Context) error {
}
func create(ctx *context.Context, format, archive, arch string) error {
var path = filepath.Join("dist", archive)
var path = filepath.Join(ctx.Config.Dist, archive)
var file = path + ".deb"
var name = ctx.Config.Build.Binary
log.Println("Creating", file)
+1 -1
View File
@@ -89,7 +89,7 @@ func description(diff string) string {
func upload(ctx *context.Context, client *github.Client, releaseID int, archive, format string) error {
archive = archive + "." + format
var path = filepath.Join("dist", archive)
var path = filepath.Join(ctx.Config.Dist, archive)
// In case the file doesn't exist, we just ignore it.
// We do this because we can get invalid combinations of archive+format here,
// like darwinamd64 + deb or something like that.