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

fix: source archive not being added when no extra-files #3938

Merged
merged 3 commits into from
Apr 13, 2023
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
29 changes: 17 additions & 12 deletions internal/pipe/sourcearchive/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
150 changes: 93 additions & 57 deletions internal/pipe/sourcearchive/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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:]
}
Comment on lines -95 to -98
Copy link
Member Author

Choose a reason for hiding this comment

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

this was actually another bug, fixed it too


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(),
Expand Down
7 changes: 4 additions & 3 deletions pkg/archive/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/archive/zip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,5 @@ func TestTarInvalidLink(t *testing.T) {
Destination: "badlink.txt",
}))
}

// TODO: add copying test