Skip to content

Commit

Permalink
feat: add support for checksum generation for extra files
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Aug 17, 2021
1 parent 72b7f3b commit 8516691
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
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
}

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 {
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

0 comments on commit 8516691

Please sign in to comment.