From 4dfc0a9ed8778573241524316a137b24d54022c6 Mon Sep 17 00:00:00 2001 From: gal-legit Date: Sun, 13 Nov 2022 09:21:02 +0200 Subject: [PATCH 1/6] output checksums to artifact into --- internal/pipe/checksums/checksums.go | 32 ++++++ internal/pipe/checksums/checksums_test.go | 118 +++++++++++++++------- 2 files changed, 116 insertions(+), 34 deletions(-) diff --git a/internal/pipe/checksums/checksums.go b/internal/pipe/checksums/checksums.go index b57837667d4..0769868c67f 100644 --- a/internal/pipe/checksums/checksums.go +++ b/internal/pipe/checksums/checksums.go @@ -19,6 +19,10 @@ import ( "github.com/goreleaser/goreleaser/pkg/context" ) +const ( + artifactChecksumExtra = "Checksum" +) + var ( errNoArtifacts = errors.New("there are no artifacts to sign") lock sync.Mutex @@ -121,6 +125,34 @@ func refresh(ctx *context.Context, filepath string) error { return err } + artifactsMapping := make(map[string]int) + for i, a := range artifactList { + if a.Type == artifact.UploadableFile { + continue // skip extra files + } + // Path is the only unique field for an artifact + artifactsMapping[a.Path] = i + } + + err = ctx.Artifacts.Visit(func(a *artifact.Artifact) error { + if index, ok := artifactsMapping[a.Path]; ok { + sumFields := strings.Fields(sumLines[index]) + if len(sumFields) == 0 { + return fmt.Errorf("missing checksum in sumline of %s: %s", a.Path, sumFields) + } + sum := sumFields[0] + if a.Extra == nil { + a.Extra = make(artifact.Extras) + } + a.Extra[artifactChecksumExtra] = fmt.Sprintf("%s:%s", ctx.Config.Checksum.Algorithm, sum) + } + + return nil + }) + if err != nil { + return err + } + file, err := os.OpenFile( filepath, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go index 6568672ee43..3dfb74dfa56 100644 --- a/internal/pipe/checksums/checksums_test.go +++ b/internal/pipe/checksums/checksums_test.go @@ -1,6 +1,7 @@ package checksums import ( + "fmt" "os" "path/filepath" "strings" @@ -276,6 +277,15 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { binary, }, }, + "default_plus_extra": { + extraFiles: []config.ExtraFile{ + {Glob: extraFileFooRelPath}, + }, + want: []string{ + binary, + extraFileFoo, + }, + }, "one extra file": { extraFiles: []config.ExtraFile{ {Glob: extraFileFooRelPath}, @@ -304,47 +314,87 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { }, }, } + checksumsMap := map[string]string{ + "crc32": "f94d3859", + "md5": "5ac749fbeec93607fc28d666be85e73a", + "sha1": "8b45e4bd1c6acb88bebf6407d16205f567e62a3e", + "sha224": "21bc225587d8768058837b68fe7e0341e87b972f02fd8fb0c236d1d3", + "sha256": "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc", + "sha384": "f6055a96a105d2fb5941a616964ffda8294fd415730cc4154a602062bc3d00e99d3c6f4a11af8c965a343de4afca3c2b", + "sha512": "14925e01a7a0cf0801aa95fe52d542b578af58ae7997ada66db3a6eae68a329d50600a5b7b442eabf4ea77ea8ef5fe40acf2ab31d47311b2a232c4f64009aac1", + } - for name, tt := range tests { - t.Run(name, func(t *testing.T) { - folder := t.TempDir() - file := filepath.Join(folder, binary) - require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644)) - ctx := context.New( - config.Project{ - Dist: folder, - ProjectName: binary, - Checksum: config.Checksum{ - Algorithm: "sha256", - NameTemplate: "checksums.txt", - ExtraFiles: tt.extraFiles, - IDs: tt.ids, + for algo, value := range checksumsMap { + algoTestCounter := 0 + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + folder := t.TempDir() + file := filepath.Join(folder, binary) + require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644)) + ctx := context.New( + config.Project{ + Dist: folder, + ProjectName: binary, + Checksum: config.Checksum{ + Algorithm: algo, + NameTemplate: "checksums.txt", + ExtraFiles: tt.extraFiles, + IDs: tt.ids, + }, }, - }, - ) + ) - ctx.Artifacts.Add(&artifact.Artifact{ - Name: binary, - Path: file, - Type: artifact.UploadableBinary, - Extra: map[string]interface{}{ - artifact.ExtraID: "id-1", - }, - }) + ctx.Artifacts.Add(&artifact.Artifact{ + Name: binary, + Path: file, + Type: artifact.UploadableBinary, + Extra: map[string]interface{}{ + artifact.ExtraID: "id-1", + }, + }) - require.NoError(t, Pipe{}.Run(ctx)) + require.NoError(t, Pipe{}.Run(ctx)) - bts, err := os.ReadFile(filepath.Join(folder, checksums)) + bts, err := os.ReadFile(filepath.Join(folder, checksums)) - require.NoError(t, err) - for _, want := range tt.want { - if tt.extraFiles == nil { - require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc "+want) - } else { - require.Contains(t, string(bts), "3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 "+want) + require.NoError(t, err) + if algo == "sha256" { + for _, want := range tt.want { + if want == binary { + require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc "+want) + } else if want == extraFileFoo { + require.Contains(t, string(bts), "3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 "+want) + } + } } - } - }) + + wantBinary := false + for _, want := range tt.want { + if want == binary { + wantBinary = true + break + } + } + if wantBinary { + _ = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableBinary)).Visit(func(a *artifact.Artifact) error { + if a.Path != file { + return nil + } + + algoTestCounter += 1 + checksum, ok := a.Extra[artifactChecksumExtra] + require.True(t, ok) + + expectedChecksum := fmt.Sprintf("%s:%s", algo, value) + require.Equal(t, expectedChecksum, checksum) + + return nil + }) + } + }) + } + const tests_that_include_binary = 2 + require.Equal(t, tests_that_include_binary, algoTestCounter) } } From 614b9352da6e908b512ec8870024035f596028d7 Mon Sep 17 00:00:00 2001 From: gal-legit Date: Sun, 13 Nov 2022 11:53:27 +0200 Subject: [PATCH 2/6] fix the docker digest field to UpperCamelCase --- internal/pipe/docker/docker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go index 7f868af3bfb..49330f44c59 100644 --- a/internal/pipe/docker/docker.go +++ b/internal/pipe/docker/docker.go @@ -20,7 +20,7 @@ import ( const ( dockerConfigExtra = "DockerConfig" - dockerDigestExtra = "digest" + dockerDigestExtra = "Digest" useBuildx = "buildx" useDocker = "docker" From 0c0d45c6b1314c460db0039b80bf85fedbbf7a78 Mon Sep 17 00:00:00 2001 From: gal-legit Date: Mon, 14 Nov 2022 16:09:04 +0200 Subject: [PATCH 3/6] pr fixes --- internal/pipe/checksums/checksums.go | 42 +++++++--------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/internal/pipe/checksums/checksums.go b/internal/pipe/checksums/checksums.go index 0769868c67f..4ec6f8a5745 100644 --- a/internal/pipe/checksums/checksums.go +++ b/internal/pipe/checksums/checksums.go @@ -125,34 +125,6 @@ func refresh(ctx *context.Context, filepath string) error { return err } - artifactsMapping := make(map[string]int) - for i, a := range artifactList { - if a.Type == artifact.UploadableFile { - continue // skip extra files - } - // Path is the only unique field for an artifact - artifactsMapping[a.Path] = i - } - - err = ctx.Artifacts.Visit(func(a *artifact.Artifact) error { - if index, ok := artifactsMapping[a.Path]; ok { - sumFields := strings.Fields(sumLines[index]) - if len(sumFields) == 0 { - return fmt.Errorf("missing checksum in sumline of %s: %s", a.Path, sumFields) - } - sum := sumFields[0] - if a.Extra == nil { - a.Extra = make(artifact.Extras) - } - a.Extra[artifactChecksumExtra] = fmt.Sprintf("%s:%s", ctx.Config.Checksum.Algorithm, sum) - } - - return nil - }) - if err != nil { - return err - } - file, err := os.OpenFile( filepath, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, @@ -169,11 +141,17 @@ func refresh(ctx *context.Context, filepath string) error { return err } -func checksums(algorithm string, artifact *artifact.Artifact) (string, error) { - log.WithField("file", artifact.Name).Debug("checksumming") - sha, err := artifact.Checksum(algorithm) +func checksums(algorithm string, a *artifact.Artifact) (string, error) { + log.WithField("file", a.Name).Debug("checksumming") + sha, err := a.Checksum(algorithm) if err != nil { return "", err } - return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil + + if a.Extra == nil { + a.Extra = make(artifact.Extras) + } + a.Extra[artifactChecksumExtra] = fmt.Sprintf("%s:%s", algorithm, sha) + + return fmt.Sprintf("%v %v\n", sha, a.Name), nil } From 864ff4aecb0764c42ded557355a4fb6deeac6948 Mon Sep 17 00:00:00 2001 From: gal-legit Date: Mon, 14 Nov 2022 16:10:31 +0200 Subject: [PATCH 4/6] lint fixes --- internal/pipe/checksums/checksums_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go index 3dfb74dfa56..034928d98ca 100644 --- a/internal/pipe/checksums/checksums_test.go +++ b/internal/pipe/checksums/checksums_test.go @@ -381,7 +381,7 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { return nil } - algoTestCounter += 1 + algoTestCounter++ checksum, ok := a.Extra[artifactChecksumExtra] require.True(t, ok) @@ -393,8 +393,8 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { } }) } - const tests_that_include_binary = 2 - require.Equal(t, tests_that_include_binary, algoTestCounter) + const testsThatIncludeBinary = 2 + require.Equal(t, testsThatIncludeBinary, algoTestCounter) } } From 3cd30071cb6b5a17fc2c8897170eee17b31f8971 Mon Sep 17 00:00:00 2001 From: gal-legit Date: Mon, 14 Nov 2022 19:34:46 +0200 Subject: [PATCH 5/6] remove all-algos test, only test for non-empty --- internal/pipe/checksums/checksums_test.go | 123 ++++++++-------------- 1 file changed, 43 insertions(+), 80 deletions(-) diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go index 034928d98ca..cffb7c36a8e 100644 --- a/internal/pipe/checksums/checksums_test.go +++ b/internal/pipe/checksums/checksums_test.go @@ -1,7 +1,6 @@ package checksums import ( - "fmt" "os" "path/filepath" "strings" @@ -277,15 +276,6 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { binary, }, }, - "default_plus_extra": { - extraFiles: []config.ExtraFile{ - {Glob: extraFileFooRelPath}, - }, - want: []string{ - binary, - extraFileFoo, - }, - }, "one extra file": { extraFiles: []config.ExtraFile{ {Glob: extraFileFooRelPath}, @@ -314,87 +304,60 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { }, }, } - checksumsMap := map[string]string{ - "crc32": "f94d3859", - "md5": "5ac749fbeec93607fc28d666be85e73a", - "sha1": "8b45e4bd1c6acb88bebf6407d16205f567e62a3e", - "sha224": "21bc225587d8768058837b68fe7e0341e87b972f02fd8fb0c236d1d3", - "sha256": "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc", - "sha384": "f6055a96a105d2fb5941a616964ffda8294fd415730cc4154a602062bc3d00e99d3c6f4a11af8c965a343de4afca3c2b", - "sha512": "14925e01a7a0cf0801aa95fe52d542b578af58ae7997ada66db3a6eae68a329d50600a5b7b442eabf4ea77ea8ef5fe40acf2ab31d47311b2a232c4f64009aac1", - } - for algo, value := range checksumsMap { - algoTestCounter := 0 - for name, tt := range tests { - t.Run(name, func(t *testing.T) { - folder := t.TempDir() - file := filepath.Join(folder, binary) - require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644)) - ctx := context.New( - config.Project{ - Dist: folder, - ProjectName: binary, - Checksum: config.Checksum{ - Algorithm: algo, - NameTemplate: "checksums.txt", - ExtraFiles: tt.extraFiles, - IDs: tt.ids, - }, + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + folder := t.TempDir() + file := filepath.Join(folder, binary) + require.NoError(t, os.WriteFile(file, []byte("some string"), 0o644)) + ctx := context.New( + config.Project{ + Dist: folder, + ProjectName: binary, + Checksum: config.Checksum{ + Algorithm: "sha256", + NameTemplate: "checksums.txt", + ExtraFiles: tt.extraFiles, + IDs: tt.ids, }, - ) + }, + ) - ctx.Artifacts.Add(&artifact.Artifact{ - Name: binary, - Path: file, - Type: artifact.UploadableBinary, - Extra: map[string]interface{}{ - artifact.ExtraID: "id-1", - }, - }) + ctx.Artifacts.Add(&artifact.Artifact{ + Name: binary, + Path: file, + Type: artifact.UploadableBinary, + Extra: map[string]interface{}{ + artifact.ExtraID: "id-1", + }, + }) - require.NoError(t, Pipe{}.Run(ctx)) + require.NoError(t, Pipe{}.Run(ctx)) - bts, err := os.ReadFile(filepath.Join(folder, checksums)) + bts, err := os.ReadFile(filepath.Join(folder, checksums)) - require.NoError(t, err) - if algo == "sha256" { - for _, want := range tt.want { - if want == binary { - require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc "+want) - } else if want == extraFileFoo { - require.Contains(t, string(bts), "3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 "+want) - } - } + require.NoError(t, err) + for _, want := range tt.want { + if tt.extraFiles == nil { + require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc "+want) + } else { + require.Contains(t, string(bts), "3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 "+want) } + } - wantBinary := false - for _, want := range tt.want { - if want == binary { - wantBinary = true - break - } + ctx.Artifacts.Visit(func(a *artifact.Artifact) error { + if a.Path != file { + return nil } - if wantBinary { - _ = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableBinary)).Visit(func(a *artifact.Artifact) error { - if a.Path != file { - return nil - } - - algoTestCounter++ - checksum, ok := a.Extra[artifactChecksumExtra] - require.True(t, ok) - - expectedChecksum := fmt.Sprintf("%s:%s", algo, value) - require.Equal(t, expectedChecksum, checksum) - - return nil - }) + if len(tt.ids) > 0 { + return nil } + checkSum, err := artifact.Extra[string](*a, artifactChecksumExtra) + require.Nil(t, err) + require.NotEmptyf(t, checkSum, "failed: %v", a.Path) + return nil }) - } - const testsThatIncludeBinary = 2 - require.Equal(t, testsThatIncludeBinary, algoTestCounter) + }) } } From 856f3705584a2c5efb4a6de7cddb98394ed658bd Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 14 Nov 2022 14:48:46 -0300 Subject: [PATCH 6/6] Update internal/pipe/checksums/checksums_test.go --- internal/pipe/checksums/checksums_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go index cffb7c36a8e..fde1baa0676 100644 --- a/internal/pipe/checksums/checksums_test.go +++ b/internal/pipe/checksums/checksums_test.go @@ -345,7 +345,7 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { } } - ctx.Artifacts.Visit(func(a *artifact.Artifact) error { + _ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error { if a.Path != file { return nil }