1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-07 13:31:37 +02:00

fix: checksums pipe

An if statement was wrong, fixed it and added tests
This commit is contained in:
Carlos Alexandro Becker 2017-12-02 20:18:30 -02:00 committed by Carlos Alexandro Becker
parent 65a8e96779
commit 3fd9e0f306
3 changed files with 34 additions and 10 deletions

View File

@ -12,14 +12,14 @@ import (
)
// SHA256 sum of the given file
func SHA256(path string) (result string, err error) {
func SHA256(path string) (string, error) {
return calculate(sha256.New(), path)
}
func calculate(hash hash.Hash, path string) (result string, err error) {
func calculate(hash hash.Hash, path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return
return "", err
}
defer func() {
if err := file.Close(); err != nil {
@ -30,12 +30,10 @@ func calculate(hash hash.Hash, path string) (result string, err error) {
return doCalculate(hash, file)
}
func doCalculate(hash hash.Hash, file io.Reader) (result string, err error) {
_, err = io.Copy(hash, file)
func doCalculate(hash hash.Hash, file io.Reader) (string, error) {
_, err := io.Copy(hash, file)
if err != nil {
return
return "", err
}
result = hex.EncodeToString(hash.Sum(nil))
return
return hex.EncodeToString(hash.Sum(nil)), nil
}

View File

@ -54,7 +54,7 @@ func (Pipe) Run(ctx *context.Context) (err error) {
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Checksum.NameTemplate != "" {
if ctx.Config.Checksum.NameTemplate == "" {
ctx.Config.Checksum.NameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
}
return nil

View File

@ -91,3 +91,29 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "/checksums.txt: permission denied")
}
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,
)
}
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)
}