mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-24 04:16:27 +02:00
test: build tests
Added more tests to cover defaulter. Also moved some code around.
This commit is contained in:
parent
b702adfc61
commit
0c91f6e210
@ -1,12 +1,6 @@
|
||||
// Package build implements Pipe and can build Go projects for
|
||||
// several platforms, with pre and post hook support.
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@ -78,52 +72,6 @@ func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
|
||||
return build
|
||||
}
|
||||
|
||||
func checkMain(ctx *context.Context, build config.Build) error {
|
||||
var main = build.Main
|
||||
if main == "" {
|
||||
main = "."
|
||||
}
|
||||
stat, ferr := os.Stat(main)
|
||||
if os.IsNotExist(ferr) {
|
||||
return errors.Wrapf(ferr, "could not open %s", main)
|
||||
}
|
||||
if stat.IsDir() {
|
||||
packs, err := parser.ParseDir(token.NewFileSet(), main, nil, 0)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to parse dir: %s", main)
|
||||
}
|
||||
for _, pack := range packs {
|
||||
for _, file := range pack.Files {
|
||||
if hasMain(file) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
|
||||
}
|
||||
file, err := parser.ParseFile(token.NewFileSet(), build.Main, nil, 0)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to parse file: %s", build.Main)
|
||||
}
|
||||
if hasMain(file) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
|
||||
}
|
||||
|
||||
func hasMain(file *ast.File) bool {
|
||||
for _, decl := range file.Decls {
|
||||
fn, isFn := decl.(*ast.FuncDecl)
|
||||
if !isFn {
|
||||
continue
|
||||
}
|
||||
if fn.Name.Name == "main" && fn.Recv == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
|
||||
if err := runHook(build.Env, build.Hooks.Pre); err != nil {
|
||||
return errors.Wrap(err, "pre hook failed")
|
||||
@ -157,19 +105,19 @@ func runHook(env []string, hook string) error {
|
||||
}
|
||||
|
||||
func doBuild(ctx *context.Context, build config.Build, target buildtarget.Target) error {
|
||||
folder, err := nameFor(ctx, target, ctx.Config.ProjectName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var binaryName = build.Binary + ext.For(target)
|
||||
var prettyName = binaryName
|
||||
if ctx.Config.Archive.Format == "binary" {
|
||||
binaryName, err = nameFor(ctx, target, build.Binary)
|
||||
binaryName, err := nameFor(ctx, target, build.Binary)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
binaryName = binaryName + ext.For(target)
|
||||
}
|
||||
folder, err := nameFor(ctx, target, ctx.Config.ProjectName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var binary = filepath.Join(ctx.Config.Dist, folder, binaryName)
|
||||
ctx.AddBinary(target.String(), folder, prettyName, binary)
|
||||
log.WithField("binary", binary).Info("building")
|
||||
|
@ -67,6 +67,12 @@ func TestRunFullPipe(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Archive: config.Archive{
|
||||
Replacements: map[string]string{
|
||||
"linux": "linuxx",
|
||||
"darwin": "darwinn",
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.NoError(t, Pipe{}.Run(context.New(config)))
|
||||
assert.True(t, exists(binary), binary)
|
||||
@ -174,27 +180,33 @@ func TestRunInvalidNametemplate(t *testing.T) {
|
||||
folder, back := testlib.Mktmp(t)
|
||||
defer back()
|
||||
writeGoodMain(t, folder)
|
||||
for _, format := range []string{"tar.gz", "zip", "binary"} {
|
||||
var config = config.Project{
|
||||
ProjectName: "nameeeee",
|
||||
Builds: []config.Build{
|
||||
{
|
||||
Binary: "namet{{.est}",
|
||||
Flags: "-v",
|
||||
Goos: []string{
|
||||
runtime.GOOS,
|
||||
},
|
||||
Goarch: []string{
|
||||
runtime.GOARCH,
|
||||
for format, msg := range map[string]string{
|
||||
"binary": `template: bar:1: unexpected "}" in operand`,
|
||||
"tar.gz": `template: foo:1: unexpected "}" in operand`,
|
||||
"zip": `template: foo:1: unexpected "}" in operand`,
|
||||
} {
|
||||
t.Run(format, func(t *testing.T) {
|
||||
var config = config.Project{
|
||||
ProjectName: "foo",
|
||||
Builds: []config.Build{
|
||||
{
|
||||
Binary: "bar",
|
||||
Flags: "-v",
|
||||
Goos: []string{
|
||||
runtime.GOOS,
|
||||
},
|
||||
Goarch: []string{
|
||||
runtime.GOARCH,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Archive: config.Archive{
|
||||
Format: format,
|
||||
NameTemplate: "{{.Binary}",
|
||||
},
|
||||
}
|
||||
assert.EqualError(t, Pipe{}.Run(context.New(config)), `template: nameeeee:1: unexpected "}" in operand`)
|
||||
Archive: config.Archive{
|
||||
Format: format,
|
||||
NameTemplate: "{{.Binary}",
|
||||
},
|
||||
}
|
||||
assert.EqualError(t, Pipe{}.Run(context.New(config)), msg)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -326,6 +338,74 @@ func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
func exists(file string) bool {
|
||||
_, err := os.Stat(file)
|
||||
return !os.IsNotExist(err)
|
||||
|
59
pipeline/build/checkmain.go
Normal file
59
pipeline/build/checkmain.go
Normal file
@ -0,0 +1,59 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func checkMain(ctx *context.Context, build config.Build) error {
|
||||
var main = build.Main
|
||||
if main == "" {
|
||||
main = "."
|
||||
}
|
||||
stat, ferr := os.Stat(main)
|
||||
if os.IsNotExist(ferr) {
|
||||
return errors.Wrapf(ferr, "could not open %s", main)
|
||||
}
|
||||
if stat.IsDir() {
|
||||
packs, err := parser.ParseDir(token.NewFileSet(), main, nil, 0)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to parse dir: %s", main)
|
||||
}
|
||||
for _, pack := range packs {
|
||||
for _, file := range pack.Files {
|
||||
if hasMain(file) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
|
||||
}
|
||||
file, err := parser.ParseFile(token.NewFileSet(), build.Main, nil, 0)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to parse file: %s", build.Main)
|
||||
}
|
||||
if hasMain(file) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
|
||||
}
|
||||
|
||||
func hasMain(file *ast.File) bool {
|
||||
for _, decl := range file.Decls {
|
||||
fn, isFn := decl.(*ast.FuncDecl)
|
||||
if !isFn {
|
||||
continue
|
||||
}
|
||||
if fn.Name.Name == "main" && fn.Recv == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
5
pipeline/build/doc.go
Normal file
5
pipeline/build/doc.go
Normal file
@ -0,0 +1,5 @@
|
||||
// Package build implements Piper and Defaulter and can build Go projects for
|
||||
// several platforms, with pre and post hook support.
|
||||
// Build also checks wether the current project has a main function, parses
|
||||
// ldflags and other goodies.
|
||||
package build
|
Loading…
x
Reference in New Issue
Block a user