1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-22 04:08:49 +02:00

test: fixed

This commit is contained in:
Carlos Alexandro Becker 2018-11-11 18:17:44 -02:00 committed by Carlos Alexandro Becker
parent 8867356965
commit 7a6de727dd
2 changed files with 106 additions and 101 deletions

View File

@ -26,10 +26,11 @@ const (
defaultBinaryNameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
)
// nolint: gochecknoglobals
var lock sync.Mutex
// Pipe for archive
type Pipe struct {
lock sync.Mutex
}
type Pipe struct{}
func (Pipe) String() string {
return "archives"
@ -63,7 +64,7 @@ func (Pipe) Default(ctx *context.Context) error {
}
// Run the pipe
func (p Pipe) Run(ctx *context.Context) error {
func (Pipe) Run(ctx *context.Context) error {
var g errgroup.Group // TODO: use semerrgroup here
var filtered = ctx.Artifacts.Filter(artifact.ByType(artifact.Binary))
for group, artifacts := range filtered.GroupByPlatform() {
@ -73,13 +74,13 @@ func (p Pipe) Run(ctx *context.Context) error {
if packageFormat(ctx, artifacts[0].Goos) == "binary" {
return skip(ctx, artifacts)
}
return p.create(ctx, artifacts)
return create(ctx, artifacts)
})
}
return g.Wait()
}
func (p Pipe) create(ctx *context.Context, binaries []artifact.Artifact) error {
func create(ctx *context.Context, binaries []artifact.Artifact) error {
var format = packageFormat(ctx, binaries[0].Goos)
folder, err := tmpl.New(ctx).
WithArtifact(binaries[0], ctx.Config.Archive.Replacements).
@ -88,15 +89,17 @@ func (p Pipe) create(ctx *context.Context, binaries []artifact.Artifact) error {
return err
}
archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
p.lock.Lock()
lock.Lock()
if _, err := os.Stat(archivePath); !os.IsNotExist(err) {
lock.Unlock()
return fmt.Errorf("archive named %s already exists. Check your archive name template", archivePath)
}
archiveFile, err := os.Create(archivePath)
if err != nil {
lock.Unlock()
return fmt.Errorf("failed to create directory %s: %s", archivePath, err.Error())
}
p.lock.Unlock()
lock.Unlock()
defer archiveFile.Close() // nolint: errcheck
var log = log.WithField("archive", archivePath)
log.Info("creating")

View File

@ -13,32 +13,31 @@ import (
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
require.NotEmpty(t, Pipe{}.String())
}
func TestRunPipe(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
assert.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
assert.NoError(t, os.MkdirAll(filepath.Join(folder, "foo", "bar", "foobar"), 0755))
_, err = os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
assert.NoError(t, err)
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(tt *testing.T) {
var dist = filepath.Join(folder, format+"_dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
require.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
require.NoError(t, err)
require.NoError(t, os.MkdirAll(filepath.Join(folder, "foo", "bar", "foobar"), 0755))
_, err = os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
require.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
@ -82,37 +81,41 @@ func TestRunPipe(t *testing.T) {
ctx.Version = "0.0.1"
ctx.Git.CurrentTag = "v0.0.1"
ctx.Config.Archive.Format = format
assert.NoError(tt, Pipe{}.Run(ctx))
require.NoError(tt, Pipe{}.Run(ctx))
var archives = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive))
require.Len(tt, archives.List(), 2)
darwin := archives.Filter(artifact.ByGoos("darwin")).List()[0]
windows := archives.Filter(artifact.ByGoos("windows")).List()[0]
assert.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name)
assert.Equal(tt, "foobar_0.0.1_windows_amd64.zip", windows.Name)
require.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name)
require.Equal(tt, "foobar_0.0.1_windows_amd64.zip", windows.Name)
if format == "tar.gz" {
// Check archive contents
require.Equal(
t,
[]string{
"README.md",
"foo/bar",
"foo/bar/foobar",
"foo/bar/foobar/blah.txt",
"mybin",
},
tarFiles(t, filepath.Join(dist, "foobar_0.0.1_darwin_amd64.tar.gz")),
)
}
if format == "zip" {
require.Equal(
t,
[]string{
"README.md",
"foo/bar/foobar/blah.txt",
"mybin.exe",
},
zipFiles(t, filepath.Join(dist, "foobar_0.0.1_windows_amd64.zip")),
)
}
})
}
// Check archive contents
assert.Equal(
t,
[]string{
"README.md",
"foo/bar",
"foo/bar/foobar",
"foo/bar/foobar/blah.txt",
"mybin",
},
tarFiles(t, filepath.Join(dist, "foobar_0.0.1_darwin_amd64.tar.gz")),
)
assert.Equal(
t,
[]string{
"README.md",
"foo/bar/foobar/blah.txt",
"mybin.exe",
},
zipFiles(t, filepath.Join(dist, "foobar_0.0.1_windows_amd64.zip")),
)
}
func zipFiles(t *testing.T, path string) []string {
@ -153,15 +156,15 @@ func TestRunPipeBinary(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
require.NoError(t, err)
_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
assert.NoError(t, err)
require.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
require.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
@ -194,13 +197,13 @@ func TestRunPipeBinary(t *testing.T) {
"Ext": ".exe",
},
})
assert.NoError(t, Pipe{}.Run(ctx))
require.NoError(t, Pipe{}.Run(ctx))
var binaries = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableBinary))
darwin := binaries.Filter(artifact.ByGoos("darwin")).List()[0]
windows := binaries.Filter(artifact.ByGoos("windows")).List()[0]
assert.Equal(t, "mybin_0.0.1_darwin_amd64", darwin.Name)
assert.Equal(t, "mybin_0.0.1_windows_amd64.exe", windows.Name)
assert.Len(t, binaries.List(), 2)
require.Equal(t, "mybin_0.0.1_darwin_amd64", darwin.Name)
require.Equal(t, "mybin_0.0.1_windows_amd64.exe", windows.Name)
require.Len(t, binaries.List(), 2)
}
func TestRunPipeDistRemoved(t *testing.T) {
@ -225,17 +228,17 @@ func TestRunPipeDistRemoved(t *testing.T) {
"Extension": ".exe",
},
})
assert.EqualError(t, Pipe{}.Run(ctx), `failed to create directory /path/nope/nope.zip: open /path/nope/nope.zip: no such file or directory`)
require.EqualError(t, Pipe{}.Run(ctx), `failed to create directory /path/nope/nope.zip: open /path/nope/nope.zip: no such file or directory`)
}
func TestRunPipeInvalidGlob(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
require.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
@ -259,19 +262,19 @@ func TestRunPipeInvalidGlob(t *testing.T) {
"Binary": "mybin",
},
})
assert.EqualError(t, Pipe{}.Run(ctx), `failed to find files to archive: globbing failed for pattern [x-]: file does not exist`)
require.EqualError(t, Pipe{}.Run(ctx), `failed to find files to archive: globbing failed for pattern [x-]: file does not exist`)
}
func TestRunPipeWrap(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
require.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
require.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
@ -296,23 +299,23 @@ func TestRunPipeWrap(t *testing.T) {
"Binary": "mybin",
},
})
assert.NoError(t, Pipe{}.Run(ctx))
require.NoError(t, Pipe{}.Run(ctx))
// Check archive contents
f, err := os.Open(filepath.Join(dist, "foo.tar.gz"))
assert.NoError(t, err)
defer func() { assert.NoError(t, f.Close()) }()
require.NoError(t, err)
defer func() { require.NoError(t, f.Close()) }()
gr, err := gzip.NewReader(f)
assert.NoError(t, err)
defer func() { assert.NoError(t, gr.Close()) }()
require.NoError(t, err)
defer func() { require.NoError(t, gr.Close()) }()
r := tar.NewReader(gr)
for _, n := range []string{"README.md", "mybin"} {
h, err := r.Next()
if err == io.EOF {
break
}
assert.NoError(t, err)
assert.Equal(t, filepath.Join("foo", n), h.Name)
require.NoError(t, err)
require.Equal(t, filepath.Join("foo", n), h.Name)
}
}
@ -322,10 +325,10 @@ func TestDefault(t *testing.T) {
Archive: config.Archive{},
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.NotEmpty(t, ctx.Config.Archive.NameTemplate)
assert.Equal(t, "tar.gz", ctx.Config.Archive.Format)
assert.NotEmpty(t, ctx.Config.Archive.Files)
require.NoError(t, Pipe{}.Default(ctx))
require.NotEmpty(t, ctx.Config.Archive.NameTemplate)
require.Equal(t, "tar.gz", ctx.Config.Archive.Format)
require.NotEmpty(t, ctx.Config.Archive.Files)
}
func TestDefaultSet(t *testing.T) {
@ -340,10 +343,10 @@ func TestDefaultSet(t *testing.T) {
},
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "foo", ctx.Config.Archive.NameTemplate)
assert.Equal(t, "zip", ctx.Config.Archive.Format)
assert.Equal(t, "foo", ctx.Config.Archive.Files[0])
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "foo", ctx.Config.Archive.NameTemplate)
require.Equal(t, "zip", ctx.Config.Archive.Format)
require.Equal(t, "foo", ctx.Config.Archive.Files[0])
}
func TestDefaultFormatBinary(t *testing.T) {
@ -354,8 +357,8 @@ func TestDefaultFormatBinary(t *testing.T) {
},
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, defaultBinaryNameTemplate, ctx.Config.Archive.NameTemplate)
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, defaultBinaryNameTemplate, ctx.Config.Archive.NameTemplate)
}
func TestFormatFor(t *testing.T) {
@ -372,23 +375,23 @@ func TestFormatFor(t *testing.T) {
},
},
}
assert.Equal(t, "zip", packageFormat(ctx, "windows"))
assert.Equal(t, "tar.gz", packageFormat(ctx, "linux"))
require.Equal(t, "zip", packageFormat(ctx, "windows"))
require.Equal(t, "tar.gz", packageFormat(ctx, "linux"))
}
func TestBinaryOverride(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
require.NoError(t, err)
_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
assert.NoError(t, err)
require.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
require.NoError(t, err)
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(tt *testing.T) {
var ctx = context.New(
@ -434,15 +437,14 @@ func TestBinaryOverride(t *testing.T) {
ctx.Version = "0.0.1"
ctx.Config.Archive.Format = format
assert.NoError(tt, Pipe{}.Run(ctx))
require.NoError(tt, Pipe{}.Run(ctx))
var archives = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive))
darwin := archives.Filter(artifact.ByGoos("darwin")).List()[0]
assert.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name)
require.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name)
archives = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableBinary))
windows := archives.Filter(artifact.ByGoos("windows")).List()[0]
assert.Equal(tt, "foobar_0.0.1_windows_amd64.exe", windows.Name)
require.Equal(tt, "foobar_0.0.1_windows_amd64.exe", windows.Name)
})
}
}
@ -451,13 +453,13 @@ func TestRunPipeSameArchiveFilename(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
require.NoError(t, err)
_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
assert.NoError(t, err)
require.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,