1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00
goreleaser/pipeline/archive/archive_test.go

198 lines
5.4 KiB
Go
Raw Normal View History

2017-04-15 15:26:05 -03:00
package archive
import (
"archive/tar"
"compress/gzip"
"io"
2017-04-15 15:26:05 -03:00
"os"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
2017-07-23 16:27:46 -03:00
"github.com/goreleaser/goreleaser/internal/testlib"
2017-04-15 15:26:05 -03:00
"github.com/stretchr/testify/assert"
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.Description())
}
func TestRunPipe(t *testing.T) {
2017-07-23 16:27:46 -03:00
folder, back := testlib.Mktmp(t)
defer back()
2017-04-15 15:26:05 -03:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin_darwin_amd64"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin_windows_amd64"), 0755))
2017-07-23 16:27:46 -03:00
_, err := os.Create(filepath.Join(dist, "mybin_darwin_amd64", "mybin"))
assert.NoError(t, err)
2017-07-01 13:13:44 -03:00
_, err = os.Create(filepath.Join(dist, "mybin_windows_amd64", "mybin.exe"))
assert.NoError(t, err)
2017-05-11 00:05:51 -03:00
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
2017-04-15 15:26:05 -03:00
var ctx = &context.Context{
Config: config.Project{
Dist: dist,
Archive: config.Archive{
Files: []string{
2017-05-11 00:05:51 -03:00
"README.*",
2017-04-15 15:26:05 -03:00
},
FormatOverrides: []config.FormatOverride{
{
Goos: "windows",
Format: "zip",
},
},
2017-04-15 15:26:05 -03:00
},
},
}
2017-07-13 20:43:12 -03:00
ctx.AddBinary("darwinamd64", "mybin_darwin_amd64", "mybin", filepath.Join(dist, "mybin_darwin_amd64", "mybin"))
ctx.AddBinary("windowsamd64", "mybin_windows_amd64", "mybin.exe", filepath.Join(dist, "mybin_windows_amd64", "mybin.exe"))
2017-04-15 15:26:05 -03:00
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(t *testing.T) {
ctx.Config.Archive.Format = format
assert.NoError(t, Pipe{}.Run(ctx))
2017-04-15 15:26:05 -03:00
})
}
// Check archive contents
f, err := os.Open(filepath.Join(dist, "mybin_darwin_amd64.tar.gz"))
assert.NoError(t, err)
defer func() { assert.NoError(t, f.Close()) }()
gr, err := gzip.NewReader(f)
assert.NoError(t, err)
defer func() { assert.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, n, h.Name)
}
2017-04-15 15:26:05 -03:00
}
2017-04-21 16:03:36 -03:00
func TestRunPipeBinary(t *testing.T) {
2017-07-23 16:27:46 -03:00
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, "mybin_darwin"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin_win"), 0755))
2017-07-23 16:27:46 -03:00
_, err := os.Create(filepath.Join(dist, "mybin_darwin", "mybin"))
assert.NoError(t, err)
2017-07-03 01:08:14 -03:00
_, err = os.Create(filepath.Join(dist, "mybin_win", "mybin.exe"))
assert.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
var ctx = &context.Context{
Config: config.Project{
Dist: dist,
2017-07-03 01:08:14 -03:00
Builds: []config.Build{
{Binary: "mybin"},
},
Archive: config.Archive{
Format: "binary",
},
},
}
2017-07-13 20:43:12 -03:00
ctx.AddBinary("darwinamd64", "mybin_darwin", "mybin", filepath.Join(dist, "mybin_darwin", "mybin"))
ctx.AddBinary("windowsamd64", "mybin_win", "mybin.exe", filepath.Join(dist, "mybin_win", "mybin.exe"))
assert.NoError(t, Pipe{}.Run(ctx))
assert.Contains(t, ctx.Artifacts, "mybin_darwin/mybin")
assert.Contains(t, ctx.Artifacts, "mybin_win/mybin.exe")
assert.Len(t, ctx.Artifacts, 2)
}
2017-04-22 10:29:53 -03:00
func TestRunPipeDistRemoved(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Dist: "/path/nope",
Archive: config.Archive{
Format: "zip",
},
},
}
2017-07-13 20:43:12 -03:00
ctx.AddBinary("windowsamd64", "nope", "no", "blah")
assert.Error(t, Pipe{}.Run(ctx))
2017-04-22 10:29:53 -03:00
}
2017-05-11 00:05:51 -03:00
func TestRunPipeInvalidGlob(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Dist: "/tmp",
Archive: config.Archive{
Files: []string{
"[x-]",
},
},
},
}
2017-07-13 20:43:12 -03:00
ctx.AddBinary("windowsamd64", "whatever", "foo", "bar")
assert.Error(t, Pipe{}.Run(ctx))
2017-05-11 00:05:51 -03:00
}
func TestRunPipeGlobFailsToAdd(t *testing.T) {
2017-07-23 16:27:46 -03:00
folder, back := testlib.Mktmp(t)
defer back()
assert.NoError(t, os.MkdirAll(filepath.Join(folder, "folder", "another"), 0755))
2017-05-11 00:05:51 -03:00
var ctx = &context.Context{
Config: config.Project{
Dist: folder,
Archive: config.Archive{
Files: []string{
"folder",
},
},
},
}
2017-07-13 20:43:12 -03:00
ctx.AddBinary("windows386", "mybin", "mybin", "dist/mybin")
assert.Error(t, Pipe{}.Run(ctx))
2017-05-11 00:05:51 -03:00
}
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, "mybin_darwin_amd64"), 0755))
_, err := os.Create(filepath.Join(dist, "mybin_darwin_amd64", "mybin"))
assert.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
var ctx = &context.Context{
Config: config.Project{
Dist: dist,
Archive: config.Archive{
WrapInDirectory: true,
Format: "tar.gz",
Files: []string{
"README.*",
},
},
},
}
ctx.AddBinary("darwinamd64", "mybin_darwin_amd64", "mybin", filepath.Join(dist, "mybin_darwin_amd64", "mybin"))
assert.NoError(t, Pipe{}.Run(ctx))
// Check archive contents
f, err := os.Open(filepath.Join(dist, "mybin_darwin_amd64.tar.gz"))
assert.NoError(t, err)
defer func() { assert.NoError(t, f.Close()) }()
gr, err := gzip.NewReader(f)
assert.NoError(t, err)
defer func() { assert.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("mybin_darwin_amd64", n), h.Name)
}
}