diff --git a/checksum/checksum.go b/checksum/checksum.go index 7cfbb6bbe..099a4d91d 100644 --- a/checksum/checksum.go +++ b/checksum/checksum.go @@ -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 } diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index f85a666bd..02404e687 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -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 diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index d4c890be9..3392a8094 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -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) +}