diff --git a/internal/pipe/sourcearchive/source.go b/internal/pipe/sourcearchive/source.go index bb839c3c047..a9ba0510465 100644 --- a/internal/pipe/sourcearchive/source.go +++ b/internal/pipe/sourcearchive/source.go @@ -61,10 +61,24 @@ func (Pipe) Run(ctx *context.Context) error { return err } - if len(ctx.Config.Source.Files) == 0 { - return nil + if len(ctx.Config.Source.Files) > 0 { + if err := appendExtraFilesToArchive(ctx, prefix, path, format); err != nil { + return err + } } + ctx.Artifacts.Add(&artifact.Artifact{ + Type: artifact.UploadableSourceArchive, + Name: filename, + Path: path, + Extra: map[string]interface{}{ + artifact.ExtraFormat: format, + }, + }) + return err +} + +func appendExtraFilesToArchive(ctx *context.Context, prefix, path, format string) error { oldPath := path + ".bkp" if err := gio.Copy(path, oldPath); err != nil { return fmt.Errorf("failed make a backup of %q: %w", path, err) @@ -110,16 +124,7 @@ func (Pipe) Run(ctx *context.Context) error { if err := af.Close(); err != nil { return fmt.Errorf("could not close archive file: %w", err) } - - ctx.Artifacts.Add(&artifact.Artifact{ - Type: artifact.UploadableSourceArchive, - Name: filename, - Path: path, - Extra: map[string]interface{}{ - artifact.ExtraFormat: format, - }, - }) - return err + return nil } // Default sets the pipe defaults. diff --git a/internal/pipe/sourcearchive/source_test.go b/internal/pipe/sourcearchive/source_test.go index 6cceba83a27..048623c96a7 100644 --- a/internal/pipe/sourcearchive/source_test.go +++ b/internal/pipe/sourcearchive/source_test.go @@ -9,6 +9,7 @@ import ( "github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testlib" "github.com/goreleaser/goreleaser/pkg/config" + "github.com/goreleaser/goreleaser/pkg/context" "github.com/stretchr/testify/require" ) @@ -42,69 +43,104 @@ subfolder/ require.NoError(t, os.MkdirAll("subfolder", 0o755)) require.NoError(t, os.WriteFile("subfolder/file.md", []byte("a file within a folder, added later"), 0o655)) - ctx := testctx.NewWithCfg( - config.Project{ - ProjectName: "foo", - Dist: "dist", - Source: config.Source{ - Format: format, - Enabled: true, - PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/", - Files: []config.File{ - {Source: "*.txt"}, - {Source: "subfolder/*"}, + t.Run("with extra files", func(t *testing.T) { + doVerifyTestArchive( + t, + testctx.NewWithCfg( + config.Project{ + ProjectName: "foo", + Dist: "dist", + Source: config.Source{ + Format: format, + Enabled: true, + PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/", + Files: []config.File{ + {Source: "*.txt"}, + {Source: "subfolder/*"}, + }, + }, }, + testctx.WithCommit("HEAD"), + testctx.WithVersion("1.0.0"), + testctx.WithCurrentTag("v1.0.0"), + ), + tmp, + format, + []string{ + "foo-1.0.0/", + "foo-1.0.0/.gitignore", + "foo-1.0.0/.gitattributes", + "foo-1.0.0/.VERSION", + "foo-1.0.0/README.md", + "foo-1.0.0/code.py", + "foo-1.0.0/code.rb", + "foo-1.0.0/code.txt", + "foo-1.0.0/added-later.txt", + "foo-1.0.0/subfolder/file.md", }, - }, - testctx.WithCommit("HEAD"), - testctx.WithVersion("1.0.0"), - testctx.WithCurrentTag("v1.0.0"), - ) - - require.NoError(t, Pipe{}.Default(ctx)) - require.NoError(t, Pipe{}.Run(ctx)) - - artifacts := ctx.Artifacts.List() - require.Len(t, artifacts, 1) - require.Equal(t, artifact.Artifact{ - Type: artifact.UploadableSourceArchive, - Name: "foo-1.0.0." + format, - Path: "dist/foo-1.0.0." + format, - Extra: map[string]interface{}{ - artifact.ExtraFormat: format, - }, - }, *artifacts[0]) - path := filepath.Join(tmp, "dist", "foo-1.0.0."+format) - stat, err := os.Stat(path) - require.NoError(t, err) - require.Greater(t, stat.Size(), int64(100)) - - expected := []string{ - "foo-1.0.0/", - "foo-1.0.0/.gitignore", - "foo-1.0.0/.gitattributes", - "foo-1.0.0/.VERSION", - "foo-1.0.0/README.md", - "foo-1.0.0/code.py", - "foo-1.0.0/code.rb", - "foo-1.0.0/code.txt", - "foo-1.0.0/added-later.txt", - "foo-1.0.0/subfolder/file.md", - } - - // zips wont have the parent dir - if format == "zip" { - expected = expected[1:] - } - - require.ElementsMatch(t, expected, testlib.LsArchive(t, path, format)) - - version := testlib.GetFileFromArchive(t, path, format, "foo-1.0.0/.VERSION") - require.Equal(t, " (HEAD -> main, tag: v1.0.0)", string(version)) + ) + }) + + t.Run("simple", func(t *testing.T) { + doVerifyTestArchive( + t, + testctx.NewWithCfg( + config.Project{ + ProjectName: "foo", + Dist: "dist", + Source: config.Source{ + Format: format, + Enabled: true, + PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/", + }, + }, + testctx.WithCommit("HEAD"), + testctx.WithVersion("1.0.0"), + testctx.WithCurrentTag("v1.0.0"), + ), + tmp, + format, + []string{ + "foo-1.0.0/", + "foo-1.0.0/.gitignore", + "foo-1.0.0/.gitattributes", + "foo-1.0.0/.VERSION", + "foo-1.0.0/README.md", + "foo-1.0.0/code.py", + "foo-1.0.0/code.rb", + }, + ) + }) }) } } +func doVerifyTestArchive(tb testing.TB, ctx *context.Context, tmp, format string, expected []string) { + tb.Helper() + require.NoError(tb, Pipe{}.Default(ctx)) + require.NoError(tb, Pipe{}.Run(ctx)) + + artifacts := ctx.Artifacts.List() + require.Len(tb, artifacts, 1) + require.Equal(tb, artifact.Artifact{ + Type: artifact.UploadableSourceArchive, + Name: "foo-1.0.0." + format, + Path: "dist/foo-1.0.0." + format, + Extra: map[string]interface{}{ + artifact.ExtraFormat: format, + }, + }, *artifacts[0]) + path := filepath.Join(tmp, "dist", "foo-1.0.0."+format) + stat, err := os.Stat(path) + require.NoError(tb, err) + require.Greater(tb, stat.Size(), int64(100)) + + require.ElementsMatch(tb, expected, testlib.LsArchive(tb, path, format)) + + version := testlib.GetFileFromArchive(tb, path, format, "foo-1.0.0/.VERSION") + require.Equal(tb, " (HEAD -> main, tag: v1.0.0)", string(version)) +} + func TestInvalidFormat(t *testing.T) { ctx := testctx.NewWithCfg(config.Project{ Dist: t.TempDir(), diff --git a/pkg/archive/zip/zip.go b/pkg/archive/zip/zip.go index 2d9fbfc7893..13547ac27da 100644 --- a/pkg/archive/zip/zip.go +++ b/pkg/archive/zip/zip.go @@ -44,9 +44,7 @@ func Copying(source *os.File, target io.Writer) (Archive, error) { } w := New(target) for _, zf := range r.File { - if zf.Mode().IsDir() { - continue - } + w.files[zf.Name] = true hdr := zip.FileHeader{ Name: zf.Name, @@ -59,6 +57,9 @@ func Copying(source *os.File, target io.Writer) (Archive, error) { if err != nil { return Archive{}, fmt.Errorf("creating %q header in target: %w", zf.Name, err) } + if zf.Mode().IsDir() { + continue + } rr, err := zf.Open() if err != nil { return Archive{}, fmt.Errorf("opening %q from source: %w", zf.Name, err) diff --git a/pkg/archive/zip/zip_test.go b/pkg/archive/zip/zip_test.go index 37366008f74..3da30a92c10 100644 --- a/pkg/archive/zip/zip_test.go +++ b/pkg/archive/zip/zip_test.go @@ -159,3 +159,5 @@ func TestTarInvalidLink(t *testing.T) { Destination: "badlink.txt", })) } + +// TODO: add copying test