Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for checksum generation for extra files #2406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/pipe/checksums/checksums.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a test trying to add a path that don't exist so we cover this branch too?

}

for name, path := range extraFiles {
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("failed to checksum %s: %w", name, err)
}
Comment on lines +65 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check is not needed I think, the extrafiles.Find would guarantee the file exists...

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 {
Expand Down
80 changes: 80 additions & 0 deletions internal/pipe/checksums/checksums_test.go
Expand Up @@ -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
Empty file.
Empty file.
9 changes: 5 additions & 4 deletions pkg/config/config.go
Expand Up @@ -533,10 +533,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.
Expand Down
9 changes: 9 additions & 0 deletions www/docs/customization/checksum.md
Expand Up @@ -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
Expand Down