1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-07 13:31:37 +02:00

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

closes #3937

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2023-04-13 11:44:02 -03:00 committed by GitHub
parent ac19f902b4
commit f6b5e9abb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 71 deletions

View File

@ -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.

View File

@ -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(),

View File

@ -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)

View File

@ -159,3 +159,5 @@ func TestTarInvalidLink(t *testing.T) {
Destination: "badlink.txt",
}))
}
// TODO: add copying test