diff --git a/internal/pipe/sourcearchive/source_test.go b/internal/pipe/sourcearchive/source_test.go index 7111810b7..6cceba83a 100644 --- a/internal/pipe/sourcearchive/source_test.go +++ b/internal/pipe/sourcearchive/source_test.go @@ -9,7 +9,6 @@ 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" ) @@ -23,8 +22,20 @@ func TestArchive(t *testing.T) { require.NoError(t, os.WriteFile("code.rb", []byte("not really code"), 0o655)) require.NoError(t, os.WriteFile("code.py", []byte("print 1"), 0o655)) require.NoError(t, os.WriteFile("README.md", []byte("# my dope fake project"), 0o655)) + require.NoError(t, os.WriteFile(".gitignore", []byte(` +added-later.txt +ignored.txt +code.txt +subfolder/ + `), 0o655)) + + require.NoError(t, os.WriteFile(".gitattributes", []byte(` +.VERSION export-subst + `), 0o655)) + require.NoError(t, os.WriteFile(".VERSION", []byte("$Format:%d$"), 0o655)) testlib.GitAdd(t) testlib.GitCommit(t, "feat: first") + testlib.GitTag(t, "v1.0.0") require.NoError(t, os.WriteFile("added-later.txt", []byte("this file was added later"), 0o655)) require.NoError(t, os.WriteFile("ignored.md", []byte("never added"), 0o655)) require.NoError(t, os.WriteFile("code.txt", []byte("not really code"), 0o655)) @@ -45,8 +56,9 @@ func TestArchive(t *testing.T) { }, }, }, - testctx.WithGitInfo(context.GitInfo{FullCommit: "HEAD"}), + testctx.WithCommit("HEAD"), testctx.WithVersion("1.0.0"), + testctx.WithCurrentTag("v1.0.0"), ) require.NoError(t, Pipe{}.Default(ctx)) @@ -69,6 +81,9 @@ func TestArchive(t *testing.T) { 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", @@ -83,6 +98,9 @@ func TestArchive(t *testing.T) { } 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)) }) } } diff --git a/internal/testctx/testctx.go b/internal/testctx/testctx.go index 15c878e90..0e695b5ff 100644 --- a/internal/testctx/testctx.go +++ b/internal/testctx/testctx.go @@ -70,6 +70,7 @@ func WithCurrentTag(tag string) Opt { func WithCommit(commig string) Opt { return func(ctx *context.Context) { ctx.Git.Commit = commig + ctx.Git.FullCommit = commig } } diff --git a/internal/testlib/archive.go b/internal/testlib/archive.go index b7665f951..8df178822 100644 --- a/internal/testlib/archive.go +++ b/internal/testlib/archive.go @@ -12,6 +12,30 @@ import ( "github.com/ulikunitz/xz" ) +// GetFileFromArchive returns the contents of filename inside the given archive +// path. +func GetFileFromArchive(tb testing.TB, path, format, filename string) []byte { + tb.Helper() + f := openFile(tb, path) + switch format { + case "tar.gz", "tgz": + return catTarFile(tb, openGzip(tb, f), filename) + case "tar.xz", "txz": + return catTarFile(tb, openXz(tb, f), filename) + case "tar": + return catTarFile(tb, f, filename) + case "zip": + return catZipFile(tb, f, filename) + case "gz": + out, err := io.ReadAll(openGzip(tb, f)) + require.NoError(tb, err) + return out + default: + tb.Errorf("invalid format: %s", format) + return nil + } +} + // LsArchive return the file list of a given archive in a given formatkj func LsArchive(tb testing.TB, path, format string) []string { tb.Helper() @@ -47,6 +71,27 @@ func openXz(tb testing.TB, r io.Reader) *xz.Reader { return xz } +func catZipFile(tb testing.TB, f *os.File, path string) []byte { + tb.Helper() + + stat, err := f.Stat() + require.NoError(tb, err) + z, err := zip.NewReader(f, stat.Size()) + require.NoError(tb, err) + + for _, zf := range z.File { + if path == zf.Name { + zz, err := zf.Open() + require.NoError(tb, err) + tb.Cleanup(func() { require.NoError(tb, zz.Close()) }) + bts, err := io.ReadAll(zz) + require.NoError(tb, err) + return bts + } + } + return nil +} + func lsZip(tb testing.TB, f *os.File) []string { tb.Helper() @@ -62,6 +107,27 @@ func lsZip(tb testing.TB, f *os.File) []string { return paths } +func catTarFile(tb testing.TB, f io.Reader, path string) []byte { + tb.Helper() + + z := tar.NewReader(f) + for { + h, err := z.Next() + if h == nil || err == io.EOF { + break + } + if h.Format == tar.FormatPAX { + continue + } + if h.Name == path { + out, err := io.ReadAll(z) + require.NoError(tb, err) + return out + } + } + return nil +} + func doLsTar(f io.Reader) []string { z := tar.NewReader(f) var paths []string