Repository: goreleaser/goreleaser
Pull request #3100 (2022-05-12 00:30:10)

refactor: improve archive pipe
Defaulting to tar.gz is probably not a good idea, better error instead.

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/3100?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#3100](https://codecov.io/gh/goreleaser/goreleaser/pull/3100?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (1bd28aa) into [main](https://codecov.io/gh/goreleaser/goreleaser/commit/17e4d627912d5df8d5ced171c05b6dd9867b477e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (17e4d62) will **decrease** coverage by `0.03%`.
> The diff coverage is `83.33%`.

```diff
@@            Coverage Diff             @@
##             main    #3100      +/-   ##
==========================================
- Coverage   84.41%   84.37%   -0.04%     
==========================================
  Files         112      112              
  Lines        9295     9295              
==========================================
- Hits         7846     7843       -3     
- Misses       1176     1178       +2     
- Partials      273      274       +1     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/3100?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/pipe/archive/archive.go](https://codecov.io/gh/goreleaser/goreleaser/pull/3100/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9hcmNoaXZlL2FyY2hpdmUuZ28=) | `93.53% <40.00%> (-1.06%)` | :arrow_down: |
| [pkg/archive/archive.go](https://codecov.io/gh/goreleaser/goreleaser/pull/3100/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cGtnL2FyY2hpdmUvYXJjaGl2ZS5nbw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/3100?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/3100?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [17e4d62...1bd28aa](https://codecov.io/gh/goreleaser/goreleaser/pull/3100?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).


Patch:
diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go
index 096eb05b..d2297d58 100644
--- a/internal/pipe/archive/archive.go
+++ b/internal/pipe/archive/archive.go
@@ -164,8 +164,11 @@ func doCreate(ctx *context.Context, arch config.Archive, binaries []*artifact.Ar
 	if err != nil {
 		return err
 	}
-
-	a := NewEnhancedArchive(archive.New(archiveFile), wrap)
+	a, err := archive.New(archiveFile, format)
+	if err != nil {
+		return err
+	}
+	a = NewEnhancedArchive(a, wrap)
 	defer a.Close()
 
 	files, err := findFiles(template, arch.Files)
diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go
index 9da00b1d..1ff3ef06 100644
--- a/pkg/archive/archive.go
+++ b/pkg/archive/archive.go
@@ -2,8 +2,8 @@
 package archive
 
 import (
-	"os"
-	"strings"
+	"fmt"
+	"io"
 
 	"github.com/goreleaser/goreleaser/pkg/archive/gzip"
 	"github.com/goreleaser/goreleaser/pkg/archive/tar"
@@ -20,21 +20,18 @@ type Archive interface {
 }
 
 // New archive.
-func New(file *os.File) Archive {
-	if strings.HasSuffix(file.Name(), ".tar.gz") {
-		return targz.New(file)
+func New(w io.Writer, format string) (Archive, error) {
+	switch format {
+	case "tar.gz":
+		return targz.New(w), nil
+	case "tar":
+		return tar.New(w), nil
+	case "gz":
+		return gzip.New(w), nil
+	case "tar.xz":
+		return tarxz.New(w), nil
+	case "zip":
+		return zip.New(w), nil
 	}
-	if strings.HasSuffix(file.Name(), ".gz") {
-		return gzip.New(file)
-	}
-	if strings.HasSuffix(file.Name(), ".tar.xz") {
-		return tarxz.New(file)
-	}
-	if strings.HasSuffix(file.Name(), ".zip") {
-		return zip.New(file)
-	}
-	if strings.HasSuffix(file.Name(), ".tar") {
-		return tar.New(file)
-	}
-	return targz.New(file)
+	return nil, fmt.Errorf("invalid archive format: %s", format)
 }


Test patch:
diff --git a/internal/pipe/archive/archive_test.go b/internal/pipe/archive/archive_test.go
index d99992d4..dab315b7 100644
--- a/internal/pipe/archive/archive_test.go
+++ b/internal/pipe/archive/archive_test.go
@@ -901,8 +901,9 @@ func TestDuplicateFilesInsideArchive(t *testing.T) {
 	ff, err := os.CreateTemp(folder, "")
 	require.NoError(t, err)
 	require.NoError(t, ff.Close())
-
-	a := NewEnhancedArchive(archive.New(f), "")
+	a, err := archive.New(f, "tar.gz")
+	require.NoError(t, err)
+	a = NewEnhancedArchive(a, "")
 	t.Cleanup(func() {
 		require.NoError(t, a.Close())
 	})
@@ -1156,3 +1157,18 @@ func TestArchive_globbing(t *testing.T) {
 		})
 	})
 }
+
+func TestInvalidFormat(t *testing.T) {
+	ctx := context.New(config.Project{
+		Dist: t.TempDir(),
+		Archives: []config.Archive{
+			{
+				ID:           "foo",
+				NameTemplate: "foo",
+				Meta:         true,
+				Format:       "7z",
+			},
+		},
+	})
+	require.EqualError(t, Pipe{}.Run(ctx), "invalid archive format: 7z")
+}
diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go
index 6de1a02a..651819d2 100644
--- a/pkg/archive/archive_test.go
+++ b/pkg/archive/archive_test.go
@@ -1,6 +1,7 @@
 package archive
 
 import (
+	"io"
 	"os"
 	"testing"
 
@@ -15,15 +16,13 @@ func TestArchive(t *testing.T) {
 	require.NoError(t, empty.Close())
 	require.NoError(t, os.Mkdir(folder+"/folder-inside", 0o755))
 
-	for _, format := range []string{"tar.gz", "zip", "gz", "tar.xz", "tar", "willbeatargzanyway"} {
+	for _, format := range []string{"tar.gz", "zip", "gz", "tar.xz", "tar"} {
 		format := format
 		t.Run(format, func(t *testing.T) {
-			file, err := os.Create(folder + "/folder." + format)
+			archive, err := New(io.Discard, format)
 			require.NoError(t, err)
-			archive := New(file)
 			t.Cleanup(func() {
 				require.NoError(t, archive.Close())
-				require.NoError(t, file.Close())
 			})
 			require.NoError(t, archive.Add(config.File{
 				Source:      empty.Name(),
@@ -35,4 +34,9 @@ func TestArchive(t *testing.T) {
 			}))
 		})
 	}
+
+	t.Run("7z", func(t *testing.T) {
+		_, err := New(io.Discard, "7z")
+		require.EqualError(t, err, "invalid archive format: 7z")
+	})
 }