1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00

refactor: artifact checksum (#771)

This commit is contained in:
Carlos Alexandro Becker 2018-08-21 15:55:35 -03:00 committed by GitHub
parent 02b70e1e9b
commit 6217d1e90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 50 additions and 77 deletions

View File

@ -28,7 +28,7 @@ check:
# Run all the tests
test:
go test $(TEST_OPTIONS) -v -failfast -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=2m
go test $(TEST_OPTIONS) -failfast -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=2m
.PHONY: test
# Run all the tests and opens the coverage report

View File

@ -2,6 +2,10 @@
package artifact
import (
"crypto/sha256"
"encoding/hex"
"io"
"os"
"sync"
"github.com/apex/log"
@ -39,6 +43,22 @@ type Artifact struct {
Extra map[string]string
}
// Checksum calculates the SHA256 checksum of the artifact.
func (a Artifact) Checksum() (string, error) {
log.Debugf("calculating sha256sum for %s", a.Path)
file, err := os.Open(a.Path)
if err != nil {
return "", err
}
defer file.Close() // nolint: errcheck
var hash = sha256.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
// Artifacts is a list of artifacts
type Artifacts struct {
items []Artifact

View File

@ -2,9 +2,12 @@ package artifact
import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
@ -135,3 +138,27 @@ func TestGroupByPlatform(t *testing.T) {
assert.Len(t, groups["linuxamd64"], 2)
assert.Len(t, groups["linuxarm6"], 1)
}
func TestChecksum(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
require.NoError(t, err)
var file = filepath.Join(folder, "subject")
require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644))
var artifact = Artifact{
Path: file,
}
sum, err := artifact.Checksum()
require.NoError(t, err)
require.Equal(t, "5e2bf57d3f40c4b6df69daf1936cb766f832374b4fc0259a7cbff06e2f70f269", sum)
}
func TestChecksumFileDoesntExist(t *testing.T) {
var artifact = Artifact{
Path: "/tmp/adasdasdas/asdasd/asdas",
}
sum, err := artifact.Checksum()
require.EqualError(t, err, `open /tmp/adasdasdas/asdasd/asdas: no such file or directory`)
require.Empty(t, sum)
}

View File

@ -1,33 +0,0 @@
// Package checksum contain algorithms to checksum files
package checksum
import (
"crypto/sha256"
"encoding/hex"
"hash"
"io"
"os"
)
// SHA256 sum of the given file
func SHA256(path string) (string, error) {
return calculate(sha256.New(), path)
}
func calculate(hash hash.Hash, path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close() // nolint: errcheck
return doCalculate(hash, file)
}
func doCalculate(hash hash.Hash, file io.Reader) (string, error) {
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}

View File

@ -1,38 +0,0 @@
package checksum
import (
"crypto/sha256"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestChecksums(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
var file = filepath.Join(folder, "subject")
assert.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644))
sum, err := SHA256(file)
assert.NoError(t, err)
assert.Equal(t, "5e2bf57d3f40c4b6df69daf1936cb766f832374b4fc0259a7cbff06e2f70f269", sum)
}
func TestOpenFailure(t *testing.T) {
sum, err := SHA256("/tmp/this-file-wont-exist-I-hope")
assert.Empty(t, sum)
assert.Error(t, err)
}
func TestFileDoesntExist(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
var path = filepath.Join(folder, "subject")
file, err := os.Create(path)
assert.NoError(t, err)
assert.NoError(t, file.Close())
_, err = doCalculate(sha256.New(), file)
assert.Error(t, err)
}

View File

@ -52,7 +52,6 @@ func (Pipe) Default(ctx *context.Context) error {
//
// Docs: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-Example-DeployinganArtifact
func (Pipe) Run(ctx *context.Context) error {
if len(ctx.Config.Artifactories) == 0 {
return pipeline.Skip("artifactory section is not configured")
}

View File

@ -12,7 +12,6 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/checksum"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/internal/tmpl"
@ -169,7 +168,7 @@ func doBuildFormula(data templateData) (out bytes.Buffer, err error) {
}
func dataFor(ctx *context.Context, artifact artifact.Artifact) (result templateData, err error) {
sum, err := checksum.SHA256(artifact.Path)
sum, err := artifact.Checksum()
if err != nil {
return
}

View File

@ -10,7 +10,6 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/checksum"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
@ -70,7 +69,7 @@ func (Pipe) Run(ctx *context.Context) (err error) {
func checksums(file *os.File, artifact artifact.Artifact) error {
log.WithField("file", artifact.Name).Info("checksumming")
sha, err := checksum.SHA256(artifact.Path)
sha, err := artifact.Checksum()
if err != nil {
return err
}