1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

Merge pull request #288 from goreleaser/cleanup

GoMetalinter + Code improvements
This commit is contained in:
Carlos Alexandro Becker 2017-07-09 13:22:40 -03:00 committed by GitHub
commit d115d4bd6c
17 changed files with 321 additions and 298 deletions

View File

@ -35,7 +35,7 @@ lint: ## Run all the linters
--deadline=10m \
./...
ci: lint test ## Run all the tests and code checks
ci: test lint ## Run all the tests and code checks
build: ## Build a beta version of goreleaser
go build

View File

@ -56,9 +56,11 @@ func TestConfigFlagNotSetButExists(t *testing.T) {
var assert = assert.New(t)
folder, back := setup(t)
defer back()
os.Rename(
filepath.Join(folder, "goreleaser.yml"),
filepath.Join(folder, ".goreleaser.yml"),
assert.NoError(
os.Rename(
filepath.Join(folder, "goreleaser.yml"),
filepath.Join(folder, ".goreleaser.yml"),
),
)
var flags = fakeFlags{
flags: map[string]string{},

View File

@ -0,0 +1,37 @@
package buildtarget
import (
"fmt"
"runtime"
)
// Runtime is the current runtime buildTarget
var Runtime = Target{runtime.GOOS, runtime.GOARCH, ""}
// New builtarget
func New(goos, goarch, goarm string) Target {
return Target{goos, goarch, goarm}
}
// Target is a build target
type Target struct {
OS, Arch, Arm string
}
// Env returns the current Target as environment variables
func (t Target) Env() []string {
return []string{
"GOOS=" + t.OS,
"GOARCH=" + t.Arch,
"GOARM=" + t.Arm,
}
}
func (t Target) String() string {
return fmt.Sprintf("%v%v%v", t.OS, t.Arch, t.Arm)
}
// PrettyString is a prettier version of the String method.
func (t Target) PrettyString() string {
return fmt.Sprintf("%v/%v%v", t.OS, t.Arch, t.Arm)
}

View File

@ -0,0 +1,31 @@
package buildtarget
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEnv(t *testing.T) {
var assert = assert.New(t)
assert.Equal(
[]string{"GOOS=linux", "GOARCH=arm64", "GOARM=6"},
New("linux", "arm64", "6").Env(),
)
}
func TestString(t *testing.T) {
var assert = assert.New(t)
assert.Equal(
"linuxarm7",
New("linux", "arm", "7").String(),
)
}
func TestPrettyString(t *testing.T) {
var assert = assert.New(t)
assert.Equal(
"linux/arm646",
New("linux", "arm64", "6").PrettyString(),
)
}

View File

@ -0,0 +1,93 @@
package buildtarget
import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/config"
)
// All returns all valid build targets for a given build
func All(build config.Build) (targets []Target) {
for _, target := range allBuildTargets(build) {
if !valid(target) {
log.WithField("target", target.PrettyString()).
Warn("skipped invalid build")
continue
}
if ignored(build, target) {
log.WithField("target", target.PrettyString()).
Warn("skipped ignored build")
continue
}
targets = append(targets, target)
}
return
}
func allBuildTargets(build config.Build) (targets []Target) {
for _, goos := range build.Goos {
for _, goarch := range build.Goarch {
if goarch == "arm" {
for _, goarm := range build.Goarm {
targets = append(targets, New(goos, goarch, goarm))
}
continue
}
targets = append(targets, New(goos, goarch, ""))
}
}
return
}
func ignored(build config.Build, target Target) bool {
for _, ig := range build.Ignore {
var ignored = New(ig.Goos, ig.Goarch, ig.Goarm)
if ignored == target {
return true
}
}
return false
}
func valid(target Target) bool {
var s = target.OS + target.Arch
for _, a := range validTargets {
if a == s {
return true
}
}
return false
}
// list from https://golang.org/doc/install/source#environment
var validTargets = []string{
"androidarm",
"darwin386",
"darwinamd64",
// "darwinarm", - requires admin rights and other ios stuff
// "darwinarm64", - requires admin rights and other ios stuff
"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",
}

View File

@ -0,0 +1,99 @@
package buildtarget
import (
"fmt"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/stretchr/testify/assert"
)
func TestAllBuildTargets(t *testing.T) {
var assert = assert.New(t)
var build = config.Build{
Goos: []string{
"linux",
"darwin",
"freebsd",
},
Goarch: []string{
"386",
"amd64",
"arm",
"arm64",
},
Goarm: []string{
"6",
"7",
},
Ignore: []config.IgnoredBuild{
{
Goos: "darwin",
Goarch: "386",
}, {
Goos: "linux",
Goarch: "arm",
Goarm: "7",
},
},
}
assert.Equal([]Target{
New("linux", "386", ""),
New("linux", "amd64", ""),
New("linux", "arm", "6"),
New("linux", "arm64", ""),
New("darwin", "amd64", ""),
New("freebsd", "386", ""),
New("freebsd", "amd64", ""),
New("freebsd", "arm", "6"),
New("freebsd", "arm", "7"),
}, All(build))
}
func TestGoosGoarchCombos(t *testing.T) {
var platforms = []struct {
os string
arch string
valid bool
}{
// valid targets:
{"android", "arm", true},
{"darwin", "386", true},
{"darwin", "amd64", true},
{"dragonfly", "amd64", true},
{"freebsd", "386", true},
{"freebsd", "amd64", true},
{"freebsd", "arm", true},
{"linux", "386", true},
{"linux", "amd64", true},
{"linux", "arm", true},
{"linux", "arm64", true},
{"linux", "mips", true},
{"linux", "mipsle", true},
{"linux", "mips64", true},
{"linux", "mips64le", true},
{"linux", "ppc64", true},
{"linux", "ppc64le", true},
{"netbsd", "386", true},
{"netbsd", "amd64", true},
{"netbsd", "arm", true},
{"openbsd", "386", true},
{"openbsd", "amd64", true},
{"openbsd", "arm", true},
{"plan9", "386", true},
{"plan9", "amd64", true},
{"solaris", "amd64", true},
{"windows", "386", true},
{"windows", "amd64", true},
// invalid targets
{"darwin", "arm", false},
{"darwin", "arm64", false},
{"windows", "arm", false},
{"windows", "arm64", false},
}
for _, p := range platforms {
t.Run(fmt.Sprintf("%v %v valid=%v", p.os, p.arch, p.valid), func(t *testing.T) {
assert.Equal(t, p.valid, valid(New(p.os, p.arch, "")))
})
}
}

View File

@ -1,10 +1,10 @@
package ext
import "strings"
import "github.com/goreleaser/goreleaser/internal/buildtarget"
// For returns the binary extension for the given platform
func For(platform string) (ext string) {
if strings.HasPrefix(platform, "windows") {
func For(target buildtarget.Target) (ext string) {
if target.OS == "windows" {
ext = ".exe"
}
return

View File

@ -3,16 +3,17 @@ package ext
import (
"testing"
"github.com/goreleaser/goreleaser/internal/buildtarget"
"github.com/stretchr/testify/assert"
)
func TestExtWindows(t *testing.T) {
assert.Equal(t, ".exe", For("windows"))
assert.Equal(t, ".exe", For("windowsamd64"))
assert.Equal(t, ".exe", For(buildtarget.New("windows", "", "")))
assert.Equal(t, ".exe", For(buildtarget.New("windows", "adm64", "")))
}
func TestExtOthers(t *testing.T) {
assert.Empty(t, "", For("linux"))
assert.Empty(t, "", For("linuxwin"))
assert.Empty(t, "", For("winasdasd"))
assert.Empty(t, "", For(buildtarget.New("linux", "", "")))
assert.Empty(t, "", For(buildtarget.New("linuxwin", "", "")))
assert.Empty(t, "", For(buildtarget.New("winasdasd", "sad", "6")))
}

View File

@ -8,6 +8,7 @@ import (
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/buildtarget"
)
type nameData struct {
@ -22,12 +23,12 @@ type nameData struct {
// ForBuild return the name for the given context, goos, goarch, goarm and
// build, using the build.Binary property instead of project_name.
func ForBuild(ctx *context.Context, build config.Build, goos, goarch, goarm string) (string, error) {
func ForBuild(ctx *context.Context, build config.Build, target buildtarget.Target) (string, error) {
return apply(
nameData{
Os: replace(ctx.Config.Archive.Replacements, goos),
Arch: replace(ctx.Config.Archive.Replacements, goarch),
Arm: replace(ctx.Config.Archive.Replacements, goarm),
Os: replace(ctx.Config.Archive.Replacements, target.OS),
Arch: replace(ctx.Config.Archive.Replacements, target.Arch),
Arm: replace(ctx.Config.Archive.Replacements, target.Arm),
Version: ctx.Version,
Tag: ctx.Git.CurrentTag,
Binary: build.Binary,
@ -38,12 +39,12 @@ func ForBuild(ctx *context.Context, build config.Build, goos, goarch, goarm stri
}
// For returns the name for the given context, goos, goarch and goarm.
func For(ctx *context.Context, goos, goarch, goarm string) (string, error) {
func For(ctx *context.Context, target buildtarget.Target) (string, error) {
return apply(
nameData{
Os: replace(ctx.Config.Archive.Replacements, goos),
Arch: replace(ctx.Config.Archive.Replacements, goarch),
Arm: replace(ctx.Config.Archive.Replacements, goarm),
Os: replace(ctx.Config.Archive.Replacements, target.OS),
Arch: replace(ctx.Config.Archive.Replacements, target.Arch),
Arm: replace(ctx.Config.Archive.Replacements, target.Arm),
Version: ctx.Version,
Tag: ctx.Git.CurrentTag,
Binary: ctx.Config.ProjectName,

View File

@ -5,6 +5,7 @@ import (
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/buildtarget"
"github.com/goreleaser/goreleaser/pipeline/defaults"
"github.com/stretchr/testify/assert"
)
@ -30,7 +31,7 @@ func TestNameFor(t *testing.T) {
},
}
name, err := For(ctx, "darwin", "amd64", "")
name, err := For(ctx, buildtarget.New("darwin", "amd64", ""))
assert.NoError(err)
assert.Equal("test_Darwin_x86_64_v1.2.3_1.2.3", name)
}
@ -55,7 +56,11 @@ func TestNameForBuild(t *testing.T) {
},
}
name, err := ForBuild(ctx, config.Build{Binary: "foo"}, "darwin", "amd64", "")
name, err := ForBuild(
ctx,
config.Build{Binary: "foo"},
buildtarget.New("darwin", "amd64", ""),
)
assert.NoError(err)
assert.Equal("foo_Darwin_x86_64_v1.2.3_1.2.3", name)
}
@ -74,7 +79,7 @@ func TestInvalidNameTemplate(t *testing.T) {
},
}
_, err := For(ctx, "darwin", "amd64", "")
_, err := For(ctx, buildtarget.New("darwin", "amd64", ""))
assert.Error(err)
}
@ -92,13 +97,13 @@ func TestNameDefaltTemplate(t *testing.T) {
type buildTarget struct {
goos, goarch, goarm string
}
for key, target := range map[string]buildTarget{
"test_1.2.3_darwin_amd64": {"darwin", "amd64", ""},
"test_1.2.3_linux_arm64": {"linux", "arm64", ""},
"test_1.2.3_linux_armv7": {"linux", "arm", "7"},
for key, target := range map[string]buildtarget.Target{
"test_1.2.3_darwin_amd64": buildtarget.New("darwin", "amd64", ""),
"test_1.2.3_linux_arm64": buildtarget.New("linux", "arm64", ""),
"test_1.2.3_linux_armv7": buildtarget.New("linux", "arm", "7"),
} {
t.Run(key, func(t *testing.T) {
name, err := For(ctx, target.goos, target.goarch, target.goarm)
name, err := For(ctx, target)
assert.NoError(err)
assert.Equal(key, name)
})

View File

@ -61,13 +61,14 @@ func create(ctx *context.Context, platform, name string) error {
return err
}
}
var path = filepath.Join(ctx.Config.Dist, name)
binaries, err := ioutil.ReadDir(path)
var basepath = filepath.Join(ctx.Config.Dist, name)
binaries, err := ioutil.ReadDir(basepath)
if err != nil {
return err
}
for _, binary := range binaries {
if err := archive.Add(binary.Name(), filepath.Join(path, binary.Name())); err != nil {
var path = filepath.Join(basepath, binary.Name())
if err := archive.Add(binary.Name(), path); err != nil {
return err
}
}

View File

@ -88,8 +88,6 @@ func TestRunPipe(t *testing.T) {
assert := assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
_, err = os.Create(filepath.Join(folder, "bin.tar.gz"))
assert.NoError(err)
var ctx = &context.Context{
Config: config.Project{
Dist: folder,
@ -109,6 +107,11 @@ func TestRunPipe(t *testing.T) {
Publish: true,
}
client := &DummyClient{}
assert.Error(doRun(ctx, client))
assert.False(client.CreatedFile)
_, err = os.Create(filepath.Join(folder, "bin.tar.gz"))
assert.NoError(err)
assert.NoError(doRun(ctx, client))
assert.True(client.CreatedFile)
}
@ -149,33 +152,6 @@ func TestRunPipeFormatOverride(t *testing.T) {
assert.Contains(client.Content, "bin.zip")
}
func TestRunPipeArchiveDoesntExist(t *testing.T) {
assert := assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
var ctx = &context.Context{
Config: config.Project{
Dist: folder,
Archive: config.Archive{
Format: "tar.gz",
},
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
},
Folders: map[string]string{
"darwinamd64": "bin",
},
Publish: true,
}
client := &DummyClient{}
assert.Error(doRun(ctx, client))
assert.False(client.CreatedFile)
}
func TestRunPipeNoDarwin64Build(t *testing.T) {
assert := assert.New(t)
var ctx = &context.Context{

View File

@ -12,6 +12,7 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/buildtarget"
"github.com/goreleaser/goreleaser/internal/ext"
"github.com/goreleaser/goreleaser/internal/name"
"golang.org/x/sync/errgroup"
@ -42,7 +43,7 @@ func runPipeOnBuild(ctx *context.Context, build config.Build) error {
}
sem := make(chan bool, 4)
var g errgroup.Group
for _, target := range buildTargets(build) {
for _, target := range buildtarget.All(build) {
sem <- true
target := target
build := build
@ -65,31 +66,24 @@ func runHook(env []string, hook string) error {
}
log.WithField("hook", hook).Info("running hook")
cmd := strings.Fields(hook)
return run(runtimeTarget, cmd, env)
return run(buildtarget.Runtime, cmd, env)
}
func doBuild(ctx *context.Context, build config.Build, target buildTarget) error {
folder, err := name.For(ctx, target.goos, target.goarch, target.goarm)
func doBuild(ctx *context.Context, build config.Build, target buildtarget.Target) error {
folder, err := name.For(ctx, target)
if err != nil {
return err
}
ctx.AddFolder(target.String(), folder)
var binary = filepath.Join(
ctx.Config.Dist,
folder,
build.Binary+ext.For(target.goos),
)
var binaryName = build.Binary + ext.For(target)
if ctx.Config.Archive.Format == "binary" {
bin, err := name.ForBuild(ctx, build, target.goos, target.goarch, target.goarm)
binaryName, err = name.ForBuild(ctx, build, target)
if err != nil {
return err
}
binary = filepath.Join(
ctx.Config.Dist,
folder,
bin+ext.For(target.goos),
)
binaryName = binaryName + ext.For(target)
}
var binary = filepath.Join(ctx.Config.Dist, folder, binaryName)
log.WithField("binary", binary).Info("building")
cmd := []string{"go", "build"}
if build.Flags != "" {
@ -103,9 +97,9 @@ func doBuild(ctx *context.Context, build config.Build, target buildTarget) error
return run(target, cmd, build.Env)
}
func run(target buildTarget, command, env []string) error {
func run(target buildtarget.Target, command, env []string) error {
var cmd = exec.Command(command[0], command[1:]...)
env = append(env, "GOOS="+target.goos, "GOARCH="+target.goarch, "GOARM="+target.goarm)
env = append(env, target.Env()...)
var log = log.WithField("target", target.PrettyString()).
WithField("env", env).
WithField("cmd", command)

View File

@ -9,6 +9,7 @@ import (
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/buildtarget"
"github.com/stretchr/testify/assert"
)
@ -19,11 +20,11 @@ func TestPipeDescription(t *testing.T) {
}
func TestRun(t *testing.T) {
assert.NoError(t, run(runtimeTarget, []string{"go", "list", "./..."}, emptyEnv))
assert.NoError(t, run(buildtarget.Runtime, []string{"go", "list", "./..."}, emptyEnv))
}
func TestRunInvalidCommand(t *testing.T) {
assert.Error(t, run(runtimeTarget, []string{"gggggo", "nope"}, emptyEnv))
assert.Error(t, run(buildtarget.Runtime, []string{"gggggo", "nope"}, emptyEnv))
}
func TestBuild(t *testing.T) {
@ -41,7 +42,7 @@ func TestBuild(t *testing.T) {
Config: config,
Folders: map[string]string{},
}
assert.NoError(doBuild(ctx, ctx.Config.Builds[0], runtimeTarget))
assert.NoError(doBuild(ctx, ctx.Config.Builds[0], buildtarget.Runtime))
}
func TestRunFullPipe(t *testing.T) {

View File

@ -1,76 +0,0 @@
package build
import (
"fmt"
"runtime"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/config"
)
var runtimeTarget = buildTarget{runtime.GOOS, runtime.GOARCH, ""}
// a build target
type buildTarget struct {
goos, goarch, goarm string
}
func (t buildTarget) String() string {
return fmt.Sprintf("%v%v%v", t.goos, t.goarch, t.goarm)
}
func (t buildTarget) PrettyString() string {
return fmt.Sprintf("%v/%v%v", t.goos, t.goarch, t.goarm)
}
func buildTargets(build config.Build) (targets []buildTarget) {
for _, target := range allBuildTargets(build) {
if !valid(target) {
log.WithField("target", target.PrettyString()).
Warn("skipped invalid build")
continue
}
if ignored(build, target) {
log.WithField("target", target.PrettyString()).
Warn("skipped ignored build")
continue
}
targets = append(targets, target)
}
return
}
func allBuildTargets(build config.Build) (targets []buildTarget) {
for _, goos := range build.Goos {
for _, goarch := range build.Goarch {
if goarch == "arm" {
for _, goarm := range build.Goarm {
targets = append(targets, buildTarget{goos, goarch, goarm})
}
continue
}
targets = append(targets, buildTarget{goos, goarch, ""})
}
}
return
}
func ignored(build config.Build, target buildTarget) bool {
for _, ig := range build.Ignore {
var ignored = buildTarget{ig.Goos, ig.Goarch, ig.Goarm}
if ignored == target {
return true
}
}
return false
}
func valid(target buildTarget) bool {
var s = target.goos + target.goarch
for _, a := range valids {
if a == s {
return true
}
}
return false
}

View File

@ -1,107 +0,0 @@
package build
import (
"fmt"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/stretchr/testify/assert"
)
func TestAllBuildTargets(t *testing.T) {
var assert = assert.New(t)
var build = config.Build{
Goos: []string{
"linux",
"darwin",
"freebsd",
},
Goarch: []string{
"386",
"amd64",
"arm",
"arm64",
},
Goarm: []string{
"6",
"7",
},
Ignore: []config.IgnoredBuild{
{
Goos: "darwin",
Goarch: "386",
}, {
Goos: "linux",
Goarch: "arm",
Goarm: "7",
},
},
}
assert.Equal([]buildTarget{
{"linux", "386", ""},
{"linux", "amd64", ""},
{"linux", "arm", "6"},
{"linux", "arm64", ""},
{"darwin", "amd64", ""},
{"freebsd", "386", ""},
{"freebsd", "amd64", ""},
{"freebsd", "arm", "6"},
{"freebsd", "arm", "7"},
}, buildTargets(build))
}
func TestValidGoosGoarchCombos(t *testing.T) {
var platforms = []struct {
os, arch string
}{
{"android", "arm"},
{"darwin", "386"},
{"darwin", "amd64"},
{"dragonfly", "amd64"},
{"freebsd", "386"},
{"freebsd", "amd64"},
{"freebsd", "arm"},
{"linux", "386"},
{"linux", "amd64"},
{"linux", "arm"},
{"linux", "arm64"},
{"linux", "mips"},
{"linux", "mipsle"},
{"linux", "mips64"},
{"linux", "mips64le"},
{"linux", "ppc64"},
{"linux", "ppc64le"},
{"netbsd", "386"},
{"netbsd", "amd64"},
{"netbsd", "arm"},
{"openbsd", "386"},
{"openbsd", "amd64"},
{"openbsd", "arm"},
{"plan9", "386"},
{"plan9", "amd64"},
{"solaris", "amd64"},
{"windows", "386"},
{"windows", "amd64"},
}
for _, p := range platforms {
t.Run(fmt.Sprintf("%v %v is valid", p.os, p.arch), func(t *testing.T) {
assert.True(t, valid(buildTarget{p.os, p.arch, ""}))
})
}
}
func TestInvalidGoosGoarchCombos(t *testing.T) {
var platforms = []struct {
os, arch string
}{
{"darwin", "arm"},
{"darwin", "arm64"},
{"windows", "arm"},
{"windows", "arm64"},
}
for _, p := range platforms {
t.Run(fmt.Sprintf("%v %v is invalid", p.os, p.arch), func(t *testing.T) {
assert.False(t, valid(buildTarget{p.os, p.arch, ""}))
})
}
}

View File

@ -1,35 +0,0 @@
package build
// list from https://golang.org/doc/install/source#environment
var valids = []string{
"androidarm",
"darwin386",
"darwinamd64",
// "darwinarm", - requires admin rights and other ios stuff
// "darwinarm64", - requires admin rights and other ios stuff
"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",
}