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:
parent
65a8e96779
commit
3fd9e0f306
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user