From 9667216403d8149f475a8d6e557cfb2721a68861 Mon Sep 17 00:00:00 2001 From: angie pinilla Date: Wed, 1 Sep 2021 22:07:43 -0400 Subject: [PATCH] feat: add support for checksum generation for extra files (#2406) Co-authored-by: Carlos Alexandro Becker --- internal/pipe/checksums/checksums.go | 17 +++++ internal/pipe/checksums/checksums_test.go | 80 ++++++++++++++++++++ internal/pipe/checksums/testdata/foo.txt | 0 internal/pipe/checksums/testdata/foo/bar.txt | 0 pkg/config/config.go | 9 ++- www/docs/customization/checksum.md | 9 +++ 6 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 internal/pipe/checksums/testdata/foo.txt create mode 100644 internal/pipe/checksums/testdata/foo/bar.txt diff --git a/internal/pipe/checksums/checksums.go b/internal/pipe/checksums/checksums.go index 03d1281c147..820a5f9830b 100644 --- a/internal/pipe/checksums/checksums.go +++ b/internal/pipe/checksums/checksums.go @@ -11,6 +11,7 @@ import ( "github.com/apex/log" "github.com/goreleaser/goreleaser/internal/artifact" + "github.com/goreleaser/goreleaser/internal/extrafiles" "github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/internal/semerrgroup" "github.com/goreleaser/goreleaser/internal/tmpl" @@ -55,6 +56,22 @@ func (Pipe) Run(ctx *context.Context) (err error) { return nil } + extraFiles, err := extrafiles.Find(ctx.Config.Checksum.ExtraFiles) + if err != nil { + return err + } + + for name, path := range extraFiles { + if _, err := os.Stat(path); os.IsNotExist(err) { + return fmt.Errorf("failed to checksum %s: %w", name, err) + } + artifactList = append(artifactList, &artifact.Artifact{ + Name: name, + Path: path, + Type: artifact.UploadableFile, + }) + } + g := semerrgroup.New(ctx.Parallelism) sumLines := make([]string, len(artifactList)) for i, artifact := range artifactList { diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go index e6f8db629d0..62b6e91acc6 100644 --- a/internal/pipe/checksums/checksums_test.go +++ b/internal/pipe/checksums/checksums_test.go @@ -238,4 +238,84 @@ func TestDefaultSet(t *testing.T) { require.Equal(t, "checksums.txt", ctx.Config.Checksum.NameTemplate) } +func TestPipeCheckSumsWithExtraFiles(t *testing.T) { + const binary = "binary" + const checksums = "checksums.txt" + const extraFileFooRelPath = "./testdata/foo.txt" + const extraFileBarRelPath = "./testdata/**/bar.txt" + const extraFileFoo = "foo.txt" + const extraFileBar = "bar.txt" + + tests := map[string]struct { + extraFiles []config.ExtraFile + want []string + }{ + "default": { + extraFiles: nil, + want: []string{ + binary, + }, + }, + "one extra file": { + extraFiles: []config.ExtraFile{ + {Glob: extraFileFooRelPath}, + }, + want: []string{ + extraFileFoo, + }, + }, + "multiple extra files": { + extraFiles: []config.ExtraFile{ + {Glob: extraFileFooRelPath}, + {Glob: extraFileBarRelPath}, + }, + want: []string{ + extraFileFoo, + extraFileBar, + }, + }, + } + + 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, + }, + }, + ) + + ctx.Artifacts.Add(&artifact.Artifact{ + Name: binary, + Path: file, + Type: artifact.UploadableBinary, + Extra: map[string]interface{}{ + "ID": "id-1", + }, + }) + + require.NoError(t, Pipe{}.Run(ctx)) + + 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) + } + } + }) + } +} + // TODO: add tests for LinuxPackage and UploadableSourceArchive diff --git a/internal/pipe/checksums/testdata/foo.txt b/internal/pipe/checksums/testdata/foo.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/pipe/checksums/testdata/foo/bar.txt b/internal/pipe/checksums/testdata/foo/bar.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/pkg/config/config.go b/pkg/config/config.go index ae0a641c107..06413757afd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -534,10 +534,11 @@ type Snapshot struct { // Checksum config. type Checksum struct { - NameTemplate string `yaml:"name_template,omitempty"` - Algorithm string `yaml:"algorithm,omitempty"` - IDs []string `yaml:"ids,omitempty"` - Disable bool `yaml:"disable,omitempty"` + NameTemplate string `yaml:"name_template,omitempty"` + Algorithm string `yaml:"algorithm,omitempty"` + IDs []string `yaml:"ids,omitempty"` + Disable bool `yaml:"disable,omitempty"` + ExtraFiles []ExtraFile `yaml:"extra_files,omitempty"` } // Docker image config. diff --git a/www/docs/customization/checksum.md b/www/docs/customization/checksum.md index 0dfe84f00f8..59def905f7b 100644 --- a/www/docs/customization/checksum.md +++ b/www/docs/customization/checksum.md @@ -30,6 +30,15 @@ checksum: # Disable the generation/upload of the checksum file. # Default is false. disable: true + + # You can add extra pre-existing files to the checksums file. + # The filename on the checksums file will be the last part of the path (base). If + # another file with the same name exists, the latest one found will be used. + # Defaults to empty. + extra_files: + - glob: ./path/to/file.txt + - glob: ./glob/**/to/**/file/**/* + - glob: ./glob/foo/to/bar/file/foobar/override_from_previous ``` !!! tip