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

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
angie pinilla
2021-09-01 23:07:43 -03:00
committed by GitHub
co-authored by Carlos Alexandro Becker
parent 17f71590e8
commit 9667216403
6 changed files with 111 additions and 4 deletions
+17
View File
@@ -11,6 +11,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/extrafiles"
"github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup" "github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl" "github.com/goreleaser/goreleaser/internal/tmpl"
@@ -55,6 +56,22 @@ func (Pipe) Run(ctx *context.Context) (err error) {
return nil 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) g := semerrgroup.New(ctx.Parallelism)
sumLines := make([]string, len(artifactList)) sumLines := make([]string, len(artifactList))
for i, artifact := range artifactList { for i, artifact := range artifactList {
+80
View File
@@ -238,4 +238,84 @@ func TestDefaultSet(t *testing.T) {
require.Equal(t, "checksums.txt", ctx.Config.Checksum.NameTemplate) 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 // TODO: add tests for LinuxPackage and UploadableSourceArchive
View File
View File
+5 -4
View File
@@ -534,10 +534,11 @@ type Snapshot struct {
// Checksum config. // Checksum config.
type Checksum struct { type Checksum struct {
NameTemplate string `yaml:"name_template,omitempty"` NameTemplate string `yaml:"name_template,omitempty"`
Algorithm string `yaml:"algorithm,omitempty"` Algorithm string `yaml:"algorithm,omitempty"`
IDs []string `yaml:"ids,omitempty"` IDs []string `yaml:"ids,omitempty"`
Disable bool `yaml:"disable,omitempty"` Disable bool `yaml:"disable,omitempty"`
ExtraFiles []ExtraFile `yaml:"extra_files,omitempty"`
} }
// Docker image config. // Docker image config.
+9
View File
@@ -30,6 +30,15 @@ checksum:
# Disable the generation/upload of the checksum file. # Disable the generation/upload of the checksum file.
# Default is false. # Default is false.
disable: true 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 !!! tip