2017-04-14 13:31:47 -03:00
|
|
|
package checksums
|
|
|
|
|
|
|
|
import (
|
2017-04-14 14:06:50 -03:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2017-04-14 13:31:47 -03:00
|
|
|
"testing"
|
|
|
|
|
2017-12-18 00:53:48 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2020-08-16 11:58:59 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-10-06 09:48:04 -03:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-14 13:31:47 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDescription(t *testing.T) {
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NotEmpty(t, Pipe{}.String())
|
2017-04-14 13:31:47 -03:00
|
|
|
}
|
|
|
|
|
2017-04-14 14:21:37 -03:00
|
|
|
func TestPipe(t *testing.T) {
|
2017-04-17 10:52:26 -03:00
|
|
|
var binary = "binary"
|
2017-12-19 19:34:33 -02:00
|
|
|
var checksums = binary + "_bar_checksums.txt"
|
2017-04-15 14:18:17 -03:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2017-04-17 10:52:26 -03:00
|
|
|
var file = filepath.Join(folder, binary)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0644))
|
2017-12-17 20:33:28 -02:00
|
|
|
var ctx = context.New(
|
|
|
|
config.Project{
|
2017-07-01 22:42:10 -03:00
|
|
|
Dist: folder,
|
|
|
|
ProjectName: binary,
|
2017-08-27 22:08:53 -03:00
|
|
|
Checksum: config.Checksum{
|
2017-12-19 19:34:33 -02:00
|
|
|
NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
|
2019-02-04 17:27:51 -02:00
|
|
|
Algorithm: "sha256",
|
2017-08-27 22:08:53 -03:00
|
|
|
},
|
2017-04-14 14:06:50 -03:00
|
|
|
},
|
2017-12-17 20:33:28 -02:00
|
|
|
)
|
2018-07-08 21:05:38 -07:00
|
|
|
ctx.Git.CurrentTag = "1.2.3"
|
2017-12-19 19:34:33 -02:00
|
|
|
ctx.Env = map[string]string{"FOO": "bar"}
|
2019-08-12 17:44:48 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
2017-12-17 20:33:28 -02:00
|
|
|
Name: binary,
|
|
|
|
Path: file,
|
|
|
|
Type: artifact.UploadableBinary,
|
|
|
|
})
|
2019-08-12 17:44:48 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
2017-12-19 19:50:03 -02:00
|
|
|
Name: binary + ".tar.gz",
|
|
|
|
Path: file,
|
|
|
|
Type: artifact.UploadableArchive,
|
|
|
|
})
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
2017-12-17 20:33:28 -02:00
|
|
|
var artifacts []string
|
|
|
|
for _, a := range ctx.Artifacts.List() {
|
|
|
|
artifacts = append(artifacts, a.Name)
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Contains(t, artifacts, checksums, binary)
|
2017-04-17 10:47:03 -03:00
|
|
|
bts, err := ioutil.ReadFile(filepath.Join(folder, checksums))
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary")
|
|
|
|
require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary.tar.gz")
|
2017-04-14 13:31:47 -03:00
|
|
|
}
|
2017-04-14 15:39:32 -03:00
|
|
|
|
2020-08-16 11:58:59 -03:00
|
|
|
func TestPipeSkipTrue(t *testing.T) {
|
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2020-08-16 11:58:59 -03:00
|
|
|
var ctx = context.New(
|
|
|
|
config.Project{
|
|
|
|
Dist: folder,
|
|
|
|
Checksum: config.Checksum{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
err = Pipe{}.Run(ctx)
|
|
|
|
testlib.AssertSkipped(t, err)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.EqualError(t, err, `checksum.disable is set`)
|
2020-08-16 11:58:59 -03:00
|
|
|
}
|
|
|
|
|
2017-04-14 15:39:32 -03:00
|
|
|
func TestPipeFileNotExist(t *testing.T) {
|
2017-04-15 14:18:17 -03:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2017-12-17 20:33:28 -02:00
|
|
|
var ctx = context.New(
|
|
|
|
config.Project{
|
2017-04-14 15:39:32 -03:00
|
|
|
Dist: folder,
|
2017-08-27 22:08:53 -03:00
|
|
|
Checksum: config.Checksum{
|
|
|
|
NameTemplate: "checksums.txt",
|
|
|
|
},
|
2017-04-14 15:39:32 -03:00
|
|
|
},
|
2017-12-17 20:33:28 -02:00
|
|
|
)
|
2018-07-08 21:05:38 -07:00
|
|
|
ctx.Git.CurrentTag = "1.2.3"
|
2019-08-12 17:44:48 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
2017-12-17 20:33:28 -02:00
|
|
|
Name: "nope",
|
|
|
|
Path: "/nope",
|
|
|
|
Type: artifact.UploadableBinary,
|
|
|
|
})
|
2017-08-27 22:08:53 -03:00
|
|
|
err = Pipe{}.Run(ctx)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "/nope: no such file or directory")
|
2017-08-27 22:08:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipeInvalidNameTemplate(t *testing.T) {
|
2020-07-06 16:09:22 -04:00
|
|
|
binFile, err := ioutil.TempFile("", "goreleasertest-bin")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2020-07-06 16:09:22 -04:00
|
|
|
_, err = binFile.WriteString("fake artifact")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2020-07-06 16:09:22 -04:00
|
|
|
|
2018-01-02 18:44:36 -02:00
|
|
|
for template, eerr := range map[string]string{
|
2018-07-08 21:05:38 -07:00
|
|
|
"{{ .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"`,
|
2018-01-02 18:44:36 -02:00
|
|
|
} {
|
|
|
|
t.Run(template, func(tt *testing.T) {
|
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(tt, err)
|
2018-01-02 18:44:36 -02:00
|
|
|
var ctx = context.New(
|
|
|
|
config.Project{
|
|
|
|
Dist: folder,
|
|
|
|
ProjectName: "name",
|
|
|
|
Checksum: config.Checksum{
|
|
|
|
NameTemplate: template,
|
2020-07-06 16:09:22 -04:00
|
|
|
Algorithm: "sha256",
|
2018-01-02 18:44:36 -02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2018-07-08 21:05:38 -07:00
|
|
|
ctx.Git.CurrentTag = "1.2.3"
|
2019-08-12 17:44:48 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
2018-01-02 18:44:36 -02:00
|
|
|
Name: "whatever",
|
|
|
|
Type: artifact.UploadableBinary,
|
2020-07-06 16:09:22 -04:00
|
|
|
Path: binFile.Name(),
|
2018-01-02 18:44:36 -02:00
|
|
|
})
|
|
|
|
err = Pipe{}.Run(ctx)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(tt, err)
|
|
|
|
require.Equal(tt, eerr, err.Error())
|
2018-01-02 18:44:36 -02:00
|
|
|
})
|
|
|
|
}
|
2017-08-27 22:08:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
|
2020-07-06 16:09:22 -04:00
|
|
|
binFile, err := ioutil.TempFile("", "goreleasertest-bin")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2020-07-06 16:09:22 -04:00
|
|
|
_, err = binFile.WriteString("fake artifact")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2020-07-06 16:09:22 -04:00
|
|
|
|
2017-08-27 22:08:53 -03:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2017-08-27 22:08:53 -03:00
|
|
|
var file = filepath.Join(folder, "checksums.txt")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0000))
|
2017-12-17 20:33:28 -02:00
|
|
|
var ctx = context.New(
|
|
|
|
config.Project{
|
2017-08-27 22:08:53 -03:00
|
|
|
Dist: folder,
|
|
|
|
Checksum: config.Checksum{
|
|
|
|
NameTemplate: "checksums.txt",
|
2020-07-06 16:09:22 -04:00
|
|
|
Algorithm: "sha256",
|
2017-08-27 22:08:53 -03:00
|
|
|
},
|
|
|
|
},
|
2017-12-17 20:33:28 -02:00
|
|
|
)
|
2018-07-08 21:05:38 -07:00
|
|
|
ctx.Git.CurrentTag = "1.2.3"
|
2019-08-12 17:44:48 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
2017-12-17 20:33:28 -02:00
|
|
|
Name: "whatever",
|
|
|
|
Type: artifact.UploadableBinary,
|
2020-07-06 16:09:22 -04:00
|
|
|
Path: binFile.Name(),
|
2017-12-17 20:33:28 -02:00
|
|
|
})
|
2017-08-27 22:08:53 -03:00
|
|
|
err = Pipe{}.Run(ctx)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "/checksums.txt: permission denied")
|
2017-04-14 15:39:32 -03:00
|
|
|
}
|
2017-12-02 20:18:30 -02:00
|
|
|
|
2020-04-02 15:18:05 +02:00
|
|
|
func TestPipeWhenNoArtifacts(t *testing.T) {
|
|
|
|
var ctx = &context.Context{}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
require.Len(t, ctx.Artifacts.List(), 0)
|
2020-04-02 15:18:05 +02:00
|
|
|
}
|
|
|
|
|
2017-12-02 20:18:30 -02:00
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Checksum: config.Checksum{},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(
|
2017-12-02 20:18:30 -02:00
|
|
|
t,
|
|
|
|
"{{ .ProjectName }}_{{ .Version }}_checksums.txt",
|
|
|
|
ctx.Config.Checksum.NameTemplate,
|
|
|
|
)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Equal(t, "sha256", ctx.Config.Checksum.Algorithm)
|
2017-12-02 20:18:30 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultSet(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Checksum: config.Checksum{
|
|
|
|
NameTemplate: "checksums.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(t, "checksums.txt", ctx.Config.Checksum.NameTemplate)
|
2017-12-02 20:18:30 -02:00
|
|
|
}
|
2020-04-12 11:47:46 -03:00
|
|
|
|
|
|
|
// TODO: add tests for LinuxPackage and UploadableSourceArchive
|