1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipe/checksums/checksums_test.go

168 lines
4.4 KiB
Go
Raw Normal View History

2017-04-14 18:31:47 +02:00
package checksums
import (
2017-04-14 19:06:50 +02:00
"io/ioutil"
"path/filepath"
2017-04-14 18:31:47 +02:00
"testing"
2017-12-18 04:53:48 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2017-04-14 18:31:47 +02:00
"github.com/stretchr/testify/assert"
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
2017-04-14 18:31:47 +02:00
}
2017-04-14 19:21:37 +02:00
func TestPipe(t *testing.T) {
2017-04-17 15:52:26 +02:00
var binary = "binary"
2017-12-19 23:34:33 +02:00
var checksums = binary + "_bar_checksums.txt"
2017-04-15 19:18:17 +02:00
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
2017-04-17 15:52:26 +02:00
var file = filepath.Join(folder, binary)
assert.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0644))
2017-12-18 00:33:28 +02:00
var ctx = context.New(
config.Project{
2017-07-02 03:42:10 +02:00
Dist: folder,
ProjectName: binary,
2017-08-28 03:08:53 +02:00
Checksum: config.Checksum{
2017-12-19 23:34:33 +02:00
NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
Algorithm: "sha256",
2017-08-28 03:08:53 +02:00
},
2017-04-14 19:06:50 +02:00
},
2017-12-18 00:33:28 +02:00
)
ctx.Git.CurrentTag = "1.2.3"
2017-12-19 23:34:33 +02:00
ctx.Env = map[string]string{"FOO": "bar"}
ctx.Artifacts.Add(&artifact.Artifact{
2017-12-18 00:33:28 +02:00
Name: binary,
Path: file,
Type: artifact.UploadableBinary,
})
ctx.Artifacts.Add(&artifact.Artifact{
2017-12-19 23:50:03 +02:00
Name: binary + ".tar.gz",
Path: file,
Type: artifact.UploadableArchive,
})
assert.NoError(t, Pipe{}.Run(ctx))
2017-12-18 00:33:28 +02:00
var artifacts []string
for _, a := range ctx.Artifacts.List() {
artifacts = append(artifacts, a.Name)
}
assert.Contains(t, artifacts, checksums, binary)
2017-04-17 15:47:03 +02:00
bts, err := ioutil.ReadFile(filepath.Join(folder, checksums))
assert.NoError(t, err)
2017-12-19 23:50:03 +02:00
assert.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary")
assert.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary.tar.gz")
2017-04-14 18:31:47 +02:00
}
2017-04-14 20:39:32 +02:00
func TestPipeFileNotExist(t *testing.T) {
2017-04-15 19:18:17 +02:00
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
2017-12-18 00:33:28 +02:00
var ctx = context.New(
config.Project{
2017-04-14 20:39:32 +02:00
Dist: folder,
2017-08-28 03:08:53 +02:00
Checksum: config.Checksum{
NameTemplate: "checksums.txt",
},
2017-04-14 20:39:32 +02:00
},
2017-12-18 00:33:28 +02:00
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Artifacts.Add(&artifact.Artifact{
2017-12-18 00:33:28 +02:00
Name: "nope",
Path: "/nope",
Type: artifact.UploadableBinary,
})
2017-08-28 03:08:53 +02:00
err = Pipe{}.Run(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "/nope: no such file or directory")
2017-08-28 03:08:53 +02:00
}
func TestPipeInvalidNameTemplate(t *testing.T) {
for template, eerr := range map[string]string{
"{{ .Pro }_checksums.txt": `template: tmpl:1: unexpected "}" in operand`,
"{{.Env.NOPE}}": `template: tmpl:1:6: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`,
} {
t.Run(template, func(tt *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(tt, err)
var ctx = context.New(
config.Project{
Dist: folder,
ProjectName: "name",
Checksum: config.Checksum{
NameTemplate: template,
},
},
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Artifacts.Add(&artifact.Artifact{
Name: "whatever",
Type: artifact.UploadableBinary,
})
err = Pipe{}.Run(ctx)
assert.Error(tt, err)
assert.Equal(tt, eerr, err.Error())
})
}
2017-08-28 03:08:53 +02:00
}
func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
2017-08-28 03:08:53 +02:00
var file = filepath.Join(folder, "checksums.txt")
assert.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0000))
2017-12-18 00:33:28 +02:00
var ctx = context.New(
config.Project{
2017-08-28 03:08:53 +02:00
Dist: folder,
Checksum: config.Checksum{
NameTemplate: "checksums.txt",
},
},
2017-12-18 00:33:28 +02:00
)
ctx.Git.CurrentTag = "1.2.3"
ctx.Artifacts.Add(&artifact.Artifact{
2017-12-18 00:33:28 +02:00
Name: "whatever",
Type: artifact.UploadableBinary,
})
2017-08-28 03:08:53 +02:00
err = Pipe{}.Run(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "/checksums.txt: permission denied")
2017-04-14 20:39:32 +02:00
}
func TestPipeWhenNoArtifacts(t *testing.T) {
var ctx = &context.Context{}
assert.NoError(t, Pipe{}.Run(ctx))
assert.Len(t, ctx.Artifacts.List(), 0)
}
func TestDefault(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Checksum: config.Checksum{},
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(
t,
"{{ .ProjectName }}_{{ .Version }}_checksums.txt",
ctx.Config.Checksum.NameTemplate,
)
assert.Equal(t, "sha256", ctx.Config.Checksum.Algorithm)
}
func TestDefaultSet(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Checksum: config.Checksum{
NameTemplate: "checksums.txt",
},
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "checksums.txt", ctx.Config.Checksum.NameTemplate)
}
// TODO: add tests for LinuxPackage and UploadableSourceArchive