From 6217d1e90d5113ab7aa5d9090e6d458d13485d30 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 21 Aug 2018 15:55:35 -0300 Subject: [PATCH] refactor: artifact checksum (#771) --- Makefile | 2 +- internal/artifact/artifact.go | 20 +++++++++++ internal/artifact/artifact_test.go | 27 ++++++++++++++ internal/checksum/checksum.go | 33 ----------------- internal/checksum/checksum_test.go | 38 -------------------- internal/pipeline/artifactory/artifactory.go | 1 - internal/pipeline/brew/brew.go | 3 +- internal/pipeline/checksums/checksums.go | 3 +- 8 files changed, 50 insertions(+), 77 deletions(-) delete mode 100644 internal/checksum/checksum.go delete mode 100644 internal/checksum/checksum_test.go diff --git a/Makefile b/Makefile index b422e359b..334700ad7 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/internal/artifact/artifact.go b/internal/artifact/artifact.go index bb3dddd75..6767018e7 100644 --- a/internal/artifact/artifact.go +++ b/internal/artifact/artifact.go @@ -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 diff --git a/internal/artifact/artifact_test.go b/internal/artifact/artifact_test.go index d1e24d2a6..8381b16e3 100644 --- a/internal/artifact/artifact_test.go +++ b/internal/artifact/artifact_test.go @@ -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) +} diff --git a/internal/checksum/checksum.go b/internal/checksum/checksum.go deleted file mode 100644 index 6a4b1b850..000000000 --- a/internal/checksum/checksum.go +++ /dev/null @@ -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 -} diff --git a/internal/checksum/checksum_test.go b/internal/checksum/checksum_test.go deleted file mode 100644 index 58f953416..000000000 --- a/internal/checksum/checksum_test.go +++ /dev/null @@ -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) -} diff --git a/internal/pipeline/artifactory/artifactory.go b/internal/pipeline/artifactory/artifactory.go index d8764c8b4..03138860c 100644 --- a/internal/pipeline/artifactory/artifactory.go +++ b/internal/pipeline/artifactory/artifactory.go @@ -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") } diff --git a/internal/pipeline/brew/brew.go b/internal/pipeline/brew/brew.go index 27dfc369e..87f9cc508 100644 --- a/internal/pipeline/brew/brew.go +++ b/internal/pipeline/brew/brew.go @@ -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 } diff --git a/internal/pipeline/checksums/checksums.go b/internal/pipeline/checksums/checksums.go index bb089a8a8..b769daf97 100644 --- a/internal/pipeline/checksums/checksums.go +++ b/internal/pipeline/checksums/checksums.go @@ -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 }