Skip to content

Commit

Permalink
fix: source archive not being added when no extra-files (#3938)
Browse files Browse the repository at this point in the history
closes #3937

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Apr 13, 2023
1 parent ac19f90 commit f6b5e9a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 72 deletions.
29 changes: 17 additions & 12 deletions internal/pipe/sourcearchive/source.go
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
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:]
}

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
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
Expand Up @@ -159,3 +159,5 @@ func TestTarInvalidLink(t *testing.T) {
Destination: "badlink.txt",
}))
}

// TODO: add copying test

0 comments on commit f6b5e9a

Please sign in to comment.