Repository: goreleaser/goreleaser
Pull request #2187 (2021-04-25 02:58:13)

fix: several fd leaks (on tests)
fix a bunch of file descriptor leaks on tests.

found most of them while trying to make tests pass on windows, which complains about them...

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/DJkbuvfcYrgkGSrkPmtvEQbASnV2](https://vercel.com/goreleaser/goreleaser/DJkbuvfcYrgkGSrkPmtvEQbASnV2)  
✅ Preview: [https://goreleaser-git-fd-goreleaser.vercel.app](https://goreleaser-git-fd-goreleaser.vercel.app)



Patch:
diff --git a/internal/pipe/gomod/gomod.go b/internal/pipe/gomod/gomod.go
index 36997622..c2b33b99 100644
--- a/internal/pipe/gomod/gomod.go
+++ b/internal/pipe/gomod/gomod.go
@@ -183,6 +183,7 @@ func copyGoSum(src, dst string) error {
 		}
 		return err
 	}
+	defer r.Close()
 
 	w, err := os.Create(dst)
 	if err != nil {


Test patch:
diff --git a/internal/artifact/artifact_test.go b/internal/artifact/artifact_test.go
index 4ea37b0a..856e6f91 100644
--- a/internal/artifact/artifact_test.go
+++ b/internal/artifact/artifact_test.go
@@ -15,7 +15,7 @@ var _ fmt.Stringer = Type(0)
 
 func TestAdd(t *testing.T) {
 	var g errgroup.Group
-	var artifacts = New()
+	artifacts := New()
 	for _, a := range []*Artifact{
 		{
 			Name: "foo",
@@ -45,7 +45,7 @@ func TestAdd(t *testing.T) {
 }
 
 func TestFilter(t *testing.T) {
-	var data = []*Artifact{
+	data := []*Artifact{
 		{
 			Name:   "foo",
 			Goos:   "linux",
@@ -68,7 +68,7 @@ func TestFilter(t *testing.T) {
 			Type: Checksum,
 		},
 	}
-	var artifacts = New()
+	artifacts := New()
 	for _, a := range data {
 		artifacts.Add(a)
 	}
@@ -108,7 +108,7 @@ func TestFilter(t *testing.T) {
 }
 
 func TestGroupByPlatform(t *testing.T) {
-	var data = []*Artifact{
+	data := []*Artifact{
 		{
 			Name:   "foo",
 			Goos:   "linux",
@@ -142,12 +142,12 @@ func TestGroupByPlatform(t *testing.T) {
 			Type: Checksum,
 		},
 	}
-	var artifacts = New()
+	artifacts := New()
 	for _, a := range data {
 		artifacts.Add(a)
 	}
 
-	var groups = artifacts.GroupByPlatform()
+	groups := artifacts.GroupByPlatform()
 	require.Len(t, groups["linuxamd64"], 2)
 	require.Len(t, groups["linuxarm6"], 1)
 	require.Len(t, groups["linuxmipssoftfloat"], 1)
@@ -155,11 +155,11 @@ func TestGroupByPlatform(t *testing.T) {
 }
 
 func TestChecksum(t *testing.T) {
-	var folder = t.TempDir()
-	var file = filepath.Join(folder, "subject")
-	require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644))
+	folder := t.TempDir()
+	file := filepath.Join(folder, "subject")
+	require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0o644))
 
-	var artifact = Artifact{
+	artifact := Artifact{
 		Path: file,
 	}
 
@@ -181,19 +181,20 @@ func TestChecksum(t *testing.T) {
 }
 
 func TestChecksumFileDoesntExist(t *testing.T) {
-	var artifact = Artifact{
-		Path: "/tmp/adasdasdas/asdasd/asdas",
+	file := filepath.Join(t.TempDir(), "nope")
+	artifact := Artifact{
+		Path: file,
 	}
 	sum, err := artifact.Checksum("sha1")
-	require.EqualError(t, err, `failed to checksum: open /tmp/adasdasdas/asdasd/asdas: no such file or directory`)
+	require.EqualError(t, err, fmt.Sprintf(`failed to checksum: open %s: no such file or directory`, file))
 	require.Empty(t, sum)
 }
 
 func TestInvalidAlgorithm(t *testing.T) {
 	f, err := ioutil.TempFile(t.TempDir(), "")
 	require.NoError(t, err)
-	t.Cleanup(func() { f.Close() })
-	var artifact = Artifact{
+	require.NoError(t, f.Close())
+	artifact := Artifact{
 		Path: f.Name(),
 	}
 	sum, err := artifact.Checksum("sha1ssss")
@@ -202,7 +203,7 @@ func TestInvalidAlgorithm(t *testing.T) {
 }
 
 func TestExtraOr(t *testing.T) {
-	var a = &Artifact{
+	a := &Artifact{
 		Extra: map[string]interface{}{
 			"Foo": "foo",
 		},
@@ -212,7 +213,7 @@ func TestExtraOr(t *testing.T) {
 }
 
 func TestByIDs(t *testing.T) {
-	var data = []*Artifact{
+	data := []*Artifact{
 		{
 			Name: "foo",
 			Extra: map[string]interface{}{
@@ -242,7 +243,7 @@ func TestByIDs(t *testing.T) {
 			Type: Checksum,
 		},
 	}
-	var artifacts = New()
+	artifacts := New()
 	for _, a := range data {
 		artifacts.Add(a)
 	}
@@ -253,7 +254,7 @@ func TestByIDs(t *testing.T) {
 }
 
 func TestByFormats(t *testing.T) {
-	var data = []*Artifact{
+	data := []*Artifact{
 		{
 			Name: "foo",
 			Extra: map[string]interface{}{
@@ -279,7 +280,7 @@ func TestByFormats(t *testing.T) {
 			},
 		},
 	}
-	var artifacts = New()
+	artifacts := New()
 	for _, a := range data {
 		artifacts.Add(a)
 	}
@@ -315,8 +316,8 @@ func TestTypeToString(t *testing.T) {
 }
 
 func TestPaths(t *testing.T) {
-	var paths = []string{"a/b", "b/c", "d/e", "f/g"}
-	var artifacts = New()
+	paths := []string{"a/b", "b/c", "d/e", "f/g"}
+	artifacts := New()
 	for _, a := range paths {
 		artifacts.Add(&Artifact{
 			Path: a,
diff --git a/internal/client/gitea_test.go b/internal/client/gitea_test.go
index 606d36c9..cd34c9bc 100644
--- a/internal/client/gitea_test.go
+++ b/internal/client/gitea_test.go
@@ -366,17 +366,17 @@ func (s *GiteaUploadSuite) SetupTest() {
 	s.artifact = &artifact.Artifact{Name: "ArtifactName"}
 	file, err := ioutil.TempFile(t.TempDir(), "gitea_test_tempfile")
 	require.NoError(t, err)
-	t.Cleanup(func() { file.Close() })
 	require.NotNil(t, file)
+	t.Cleanup(func() {
+		_ = file.Close()
+	})
 	s.file = file
 	s.releaseAttachmentsURL = fmt.Sprintf("%v/assets", s.releaseURL)
 }
 
 func (s *GiteaUploadSuite) TearDownTest() {
-	t := s.T()
 	s.GiteaReleasesTestSuite.TearDownTest()
-	err := s.file.Close()
-	require.NoError(t, err)
+	require.NoError(s.T(), s.file.Close())
 }
 
 func (s *GiteaUploadSuite) TestErrorParsingReleaseID() {
diff --git a/internal/http/http_test.go b/internal/http/http_test.go
index b2d938ff..b5d8440d 100644
--- a/internal/http/http_test.go
+++ b/internal/http/http_test.go
@@ -30,6 +30,9 @@ func TestAssetOpenDefault(t *testing.T) {
 	if err != nil {
 		t.Fatalf("can not open asset: %v", err)
 	}
+	t.Cleanup(func() {
+		require.NoError(t, a.ReadCloser.Close())
+	})
 	bs, err := ioutil.ReadAll(a.ReadCloser)
 	if err != nil {
 		t.Fatalf("can not read asset: %v", err)
diff --git a/internal/pipe/archive/archive_test.go b/internal/pipe/archive/archive_test.go
index 2c5640e8..dd730462 100644
--- a/internal/pipe/archive/archive_test.go
+++ b/internal/pipe/archive/archive_test.go
@@ -27,8 +27,9 @@ func createFakeBinary(t *testing.T, dist, arch, bin string) {
 	t.Helper()
 	var path = filepath.Join(dist, arch, bin)
 	require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755))
-	_, err := os.Create(path)
+	f, err := os.Create(path)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 }
 
 func TestRunPipe(t *testing.T) {
@@ -42,12 +43,14 @@ func TestRunPipe(t *testing.T) {
 			}
 			createFakeBinary(t, dist, "windowsamd64", "bin/mybin.exe")
 			for _, tt := range []string{"darwin", "linux", "windows"} {
-				_, err := os.Create(filepath.Join(folder, fmt.Sprintf("README.%s.md", tt)))
+				f, err := os.Create(filepath.Join(folder, fmt.Sprintf("README.%s.md", tt)))
 				require.NoError(t, err)
+				require.NoError(t, f.Close())
 			}
 			require.NoError(t, os.MkdirAll(filepath.Join(folder, "foo", "bar", "foobar"), 0755))
-			_, err := os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
+			f, err := os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
 			require.NoError(t, err)
+			require.NoError(t, f.Close())
 			var ctx = context.New(
 				config.Project{
 					Dist:        dist,
@@ -306,12 +309,15 @@ func TestRunPipeBinary(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
-	_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
+	require.NoError(t, f.Close())
+	f, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
 	require.NoError(t, err)
-	_, err = os.Create(filepath.Join(folder, "README.md"))
+	require.NoError(t, f.Close())
+	f, err = os.Create(filepath.Join(folder, "README.md"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist: dist,
@@ -393,8 +399,9 @@ func TestRunPipeInvalidGlob(t *testing.T) {
 	var dist = filepath.Join(folder, "dist")
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist: dist,
@@ -430,8 +437,9 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
 	var dist = filepath.Join(folder, "dist")
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist: dist,
@@ -464,8 +472,9 @@ func TestRunPipeInvalidFilesNameTemplate(t *testing.T) {
 	var dist = filepath.Join(folder, "dist")
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist: dist,
@@ -501,8 +510,9 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
 	var dist = filepath.Join(folder, "dist")
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist: dist,
@@ -536,10 +546,12 @@ func TestRunPipeWrap(t *testing.T) {
 	var dist = filepath.Join(folder, "dist")
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
-	_, err = os.Create(filepath.Join(folder, "README.md"))
+	require.NoError(t, f.Close())
+	f, err = os.Create(filepath.Join(folder, "README.md"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist: dist,
@@ -578,7 +590,7 @@ func TestRunPipeWrap(t *testing.T) {
 	require.Equal(t, "foo_macOS", archives[0].ExtraOr("WrappedIn", ""))
 
 	// Check archive contents
-	f, err := os.Open(filepath.Join(dist, "foo.tar.gz"))
+	f, err = os.Open(filepath.Join(dist, "foo.tar.gz"))
 	require.NoError(t, err)
 	defer func() { require.NoError(t, f.Close()) }()
 	gr, err := gzip.NewReader(f)
@@ -669,12 +681,15 @@ func TestBinaryOverride(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
-	_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
+	require.NoError(t, f.Close())
+	f, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
 	require.NoError(t, err)
-	_, err = os.Create(filepath.Join(folder, "README.md"))
+	require.NoError(t, f.Close())
+	f, err = os.Create(filepath.Join(folder, "README.md"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	for _, format := range []string{"tar.gz", "zip"} {
 		t.Run("Archive format "+format, func(t *testing.T) {
 			var ctx = context.New(
@@ -746,10 +761,12 @@ func TestRunPipeSameArchiveFilename(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
-	_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
+	f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
 	require.NoError(t, err)
-	_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
+	require.NoError(t, f.Close())
+	f, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = context.New(
 		config.Project{
 			Dist:        dist,
@@ -802,14 +819,18 @@ func TestDuplicateFilesInsideArchive(t *testing.T) {
 
 	f, err := ioutil.TempFile(folder, "")
 	require.NoError(t, err)
-	defer f.Close()
+	t.Cleanup(func() {
+		require.NoError(t, f.Close())
+	})
 
 	ff, err := ioutil.TempFile(folder, "")
 	require.NoError(t, err)
-	defer ff.Close()
+	require.NoError(t, ff.Close())
 
 	a := NewEnhancedArchive(archive.New(f), "")
-	defer a.Close()
+	t.Cleanup(func() {
+		require.NoError(t, a.Close())
+	})
 
 	require.NoError(t, a.Add("foo", ff.Name()))
 	require.EqualError(t, a.Add("foo", ff.Name()), "file foo already exists in the archive")
diff --git a/internal/pipe/artifactory/artifactory_test.go b/internal/pipe/artifactory/artifactory_test.go
index 8941122a..4762c029 100644
--- a/internal/pipe/artifactory/artifactory_test.go
+++ b/internal/pipe/artifactory/artifactory_test.go
@@ -217,8 +217,10 @@ func TestRunPipe_ModeArchive(t *testing.T) {
 	folder := t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 	debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
 	require.NoError(t, err)
+	require.NoError(t, debfile.Close())
 
 	ctx := context.New(config.Project{
 		ProjectName: "goreleaser",
@@ -318,6 +320,7 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
 	folder := t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 
 	ctx := context.New(config.Project{
 		ProjectName: "goreleaser",
diff --git a/internal/pipe/brew/brew_test.go b/internal/pipe/brew/brew_test.go
index 1c31b7df..7d570a2b 100644
--- a/internal/pipe/brew/brew_test.go
+++ b/internal/pipe/brew/brew_test.go
@@ -237,8 +237,9 @@ func TestRunPipe(t *testing.T) {
 				},
 			})
 
-			_, err := os.Create(path)
+			f, err := os.Create(path)
 			require.NoError(t, err)
+			require.NoError(t, f.Close())
 			client := &DummyClient{}
 			var distFile = filepath.Join(folder, name+".rb")
 
@@ -301,8 +302,9 @@ func TestRunPipeNameTemplate(t *testing.T) {
 		},
 	})
 
-	_, err := os.Create(path)
+	f, err := os.Create(path)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	client := &DummyClient{}
 	var distFile = filepath.Join(folder, "foo_is_bar.rb")
 
@@ -385,8 +387,9 @@ func TestRunPipeMultipleBrewsWithSkip(t *testing.T) {
 		},
 	})
 
-	_, err := os.Create(path)
+	f, err := os.Create(path)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 
 	var cli = &DummyClient{}
 	require.EqualError(t, publishAll(ctx, cli), `brew.skip_upload is set`)
@@ -503,8 +506,9 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
 					"Format": "tar.gz",
 				},
 			})
-			_, err := os.Create(path)
+			f, err := os.Create(path)
 			require.NoError(t, err)
+			require.NoError(t, f.Close())
 		}
 
 		client := &DummyClient{}
@@ -562,7 +566,9 @@ func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
 	ctx.TokenType = context.TokenTypeGitHub
 	f, err := ioutil.TempFile(t.TempDir(), "")
 	require.NoError(t, err)
-	t.Cleanup(func() { f.Close() })
+	t.Cleanup(func() {
+		require.NoError(t, f.Close())
+	})
 
 	tests := []struct {
 		expectedError error
@@ -742,8 +748,9 @@ func TestRunPipeNoUpload(t *testing.T) {
 	ctx.TokenType = context.TokenTypeGitHub
 	ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
 	var path = filepath.Join(folder, "whatever.tar.gz")
-	_, err := os.Create(path)
+	f, err := os.Create(path)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name:   "bin",
 		Path:   path,
@@ -793,8 +800,9 @@ func TestRunEmptyTokenType(t *testing.T) {
 	})
 	ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
 	var path = filepath.Join(folder, "whatever.tar.gz")
-	_, err := os.Create(path)
+	f, err := os.Create(path)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name:   "bin",
 		Path:   path,
@@ -828,8 +836,9 @@ func TestRunTokenTypeNotImplementedForBrew(t *testing.T) {
 	ctx.TokenType = context.TokenTypeGitea
 	ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"}
 	var path = filepath.Join(folder, "whatever.tar.gz")
-	_, err := os.Create(path)
+	f, err := os.Create(path)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name:   "bin",
 		Path:   path,
diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go
index 82dd184a..ca8a6421 100644
--- a/internal/pipe/checksums/checksums_test.go
+++ b/internal/pipe/checksums/checksums_test.go
@@ -142,7 +142,7 @@ func TestPipeFileNotExist(t *testing.T) {
 func TestPipeInvalidNameTemplate(t *testing.T) {
 	binFile, err := ioutil.TempFile(t.TempDir(), "goreleasertest-bin")
 	require.NoError(t, err)
-	t.Cleanup(func() { binFile.Close() })
+	t.Cleanup(func() { require.NoError(t, binFile.Close()) })
 	_, err = binFile.WriteString("fake artifact")
 	require.NoError(t, err)
 
@@ -179,7 +179,7 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) {
 	var folder = t.TempDir()
 	binFile, err := ioutil.TempFile(folder, "goreleasertest-bin")
 	require.NoError(t, err)
-	t.Cleanup(func() { binFile.Close() })
+	t.Cleanup(func() { require.NoError(t, binFile.Close()) })
 	_, err = binFile.WriteString("fake artifact")
 	require.NoError(t, err)
 
diff --git a/internal/pipe/dist/dist_test.go b/internal/pipe/dist/dist_test.go
index bde4a29f..5da6f7ab 100644
--- a/internal/pipe/dist/dist_test.go
+++ b/internal/pipe/dist/dist_test.go
@@ -29,8 +29,9 @@ func TestPopulatedDistExists(t *testing.T) {
 	var folder = t.TempDir()
 	var dist = filepath.Join(folder, "dist")
 	require.NoError(t, os.Mkdir(dist, 0755))
-	_, err := os.Create(filepath.Join(dist, "mybin"))
+	f, err := os.Create(filepath.Join(dist, "mybin"))
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	var ctx = &context.Context{
 		Config: config.Project{
 			Dist: dist,
diff --git a/internal/pipe/docker/docker_test.go b/internal/pipe/docker/docker_test.go
index 4b5eb199..6fd9a696 100644
--- a/internal/pipe/docker/docker_test.go
+++ b/internal/pipe/docker/docker_test.go
@@ -852,15 +852,19 @@ func TestRunPipe(t *testing.T) {
 			dist := filepath.Join(folder, "dist")
 			require.NoError(t, os.Mkdir(dist, 0o755))
 			require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
-			_, err := os.Create(filepath.Join(dist, "mybin", "mybin"))
+			f, err := os.Create(filepath.Join(dist, "mybin", "mybin"))
 			require.NoError(t, err)
-			_, err = os.Create(filepath.Join(dist, "mybin", "anotherbin"))
+			require.NoError(t, f.Close())
+			f, err = os.Create(filepath.Join(dist, "mybin", "anotherbin"))
 			require.NoError(t, err)
-			_, err = os.Create(filepath.Join(dist, "mynfpm.apk"))
+			require.NoError(t, f.Close())
+			f, err = os.Create(filepath.Join(dist, "mynfpm.apk"))
 			require.NoError(t, err)
+			require.NoError(t, f.Close())
 			for _, arch := range []string{"amd64", "386", "arm64"} {
-				_, err = os.Create(filepath.Join(dist, fmt.Sprintf("mybin_%s.apk", arch)))
+				f, err = os.Create(filepath.Join(dist, fmt.Sprintf("mybin_%s.apk", arch)))
 				require.NoError(t, err)
+				require.NoError(t, f.Close())
 			}
 
 			ctx := context.New(config.Project{
@@ -1177,14 +1181,11 @@ func Test_processImageTemplates(t *testing.T) {
 }
 
 func TestLinkFile(t *testing.T) {
-	src, err := ioutil.TempFile(t.TempDir(), "src")
+	dir := t.TempDir()
+	src, err := ioutil.TempFile(dir, "src")
 	require.NoError(t, err)
 	require.NoError(t, src.Close())
-	dst := filepath.Join(filepath.Dir(src.Name()), "dst")
-	t.Cleanup(func() {
-		os.Remove(src.Name())
-		os.Remove(dst)
-	})
+	dst := filepath.Join(dir, "dst")
 	fmt.Println("src:", src.Name())
 	fmt.Println("dst:", dst)
 	require.NoError(t, ioutil.WriteFile(src.Name(), []byte("foo"), 0o644))
diff --git a/internal/pipe/env/env_test.go b/internal/pipe/env/env_test.go
index 1b88c6d9..01d362e3 100644
--- a/internal/pipe/env/env_test.go
+++ b/internal/pipe/env/env_test.go
@@ -124,6 +124,7 @@ func TestEmptyGithubEnvFile(t *testing.T) {
 	require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
 	f, err := ioutil.TempFile(t.TempDir(), "token")
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	require.NoError(t, os.Chmod(f.Name(), 0377))
 	var ctx = &context.Context{
 		Config: config.Project{
@@ -139,6 +140,7 @@ func TestEmptyGitlabEnvFile(t *testing.T) {
 	require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))
 	f, err := ioutil.TempFile(t.TempDir(), "token")
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	require.NoError(t, os.Chmod(f.Name(), 0377))
 	var ctx = &context.Context{
 		Config: config.Project{
@@ -154,6 +156,7 @@ func TestEmptyGiteaEnvFile(t *testing.T) {
 	require.NoError(t, os.Unsetenv("GITEA_TOKEN"))
 	f, err := ioutil.TempFile(t.TempDir(), "token")
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	require.NoError(t, os.Chmod(f.Name(), 0377))
 	var ctx = &context.Context{
 		Config: config.Project{
@@ -200,6 +203,7 @@ func TestLoadEnv(t *testing.T) {
 		f, err := ioutil.TempFile(t.TempDir(), "token")
 		require.NoError(t, err)
 		fmt.Fprintf(f, "123")
+		require.NoError(t, f.Close())
 		v, err := loadEnv(env, f.Name())
 		require.NoError(t, err)
 		require.Equal(t, "123", v)
@@ -210,6 +214,7 @@ func TestLoadEnv(t *testing.T) {
 		f, err := ioutil.TempFile(t.TempDir(), "token")
 		require.NoError(t, err)
 		fmt.Fprintf(f, "123\n")
+		require.NoError(t, f.Close())
 		v, err := loadEnv(env, f.Name())
 		require.NoError(t, err)
 		require.Equal(t, "123", v)
@@ -220,6 +225,7 @@ func TestLoadEnv(t *testing.T) {
 		f, err := ioutil.TempFile(t.TempDir(), "token")
 		require.NoError(t, err)
 		fmt.Fprintf(f, "123")
+		require.NoError(t, f.Close())
 		err = os.Chmod(f.Name(), 0377)
 		require.NoError(t, err)
 		v, err := loadEnv(env, f.Name())
diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go
index 268c9877..5de01f92 100644
--- a/internal/pipe/git/git_test.go
+++ b/internal/pipe/git/git_test.go
@@ -94,6 +94,7 @@ func TestDirty(t *testing.T) {
 	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
 	dummy, err := os.Create(filepath.Join(folder, "dummy"))
 	require.NoError(t, err)
+	require.NoError(t, dummy.Close())
 	testlib.GitAdd(t)
 	testlib.GitCommit(t, "commit2")
 	testlib.GitTag(t, "v0.0.1")
diff --git a/internal/pipe/nfpm/nfpm_test.go b/internal/pipe/nfpm/nfpm_test.go
index 593e9e67..93f5bacd 100644
--- a/internal/pipe/nfpm/nfpm_test.go
+++ b/internal/pipe/nfpm/nfpm_test.go
@@ -78,8 +78,9 @@ func TestRunPipe(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0o755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
 	binPath := filepath.Join(dist, "mybin", "mybin")
-	_, err := os.Create(binPath)
+	f, err := os.Create(binPath)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx := context.New(config.Project{
 		ProjectName: "mybin",
 		Dist:        dist,
@@ -421,8 +422,9 @@ func TestDebSpecificConfig(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0o755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
 	binPath := filepath.Join(dist, "mybin", "mybin")
-	_, err := os.Create(binPath)
+	f, err := os.Create(binPath)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx := context.New(config.Project{
 		ProjectName: "mybin",
 		Dist:        dist,
@@ -494,8 +496,9 @@ func TestRPMSpecificConfig(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0o755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
 	binPath := filepath.Join(dist, "mybin", "mybin")
-	_, err := os.Create(binPath)
+	f, err := os.Create(binPath)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx := context.New(config.Project{
 		ProjectName: "mybin",
 		Dist:        dist,
@@ -567,8 +570,9 @@ func TestAPKSpecificConfig(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0o755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
 	binPath := filepath.Join(dist, "mybin", "mybin")
-	_, err := os.Create(binPath)
+	f, err := os.Create(binPath)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx := context.New(config.Project{
 		ProjectName: "mybin",
 		Dist:        dist,
@@ -657,8 +661,9 @@ func TestMeta(t *testing.T) {
 	require.NoError(t, os.Mkdir(dist, 0o755))
 	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
 	binPath := filepath.Join(dist, "mybin", "mybin")
-	_, err := os.Create(binPath)
+	f, err := os.Create(binPath)
 	require.NoError(t, err)
+	require.NoError(t, f.Close())
 	ctx := context.New(config.Project{
 		ProjectName: "mybin",
 		Dist:        dist,
diff --git a/internal/pipe/release/release_test.go b/internal/pipe/release/release_test.go
index d95ff284..8cedb09a 100644
--- a/internal/pipe/release/release_test.go
+++ b/internal/pipe/release/release_test.go
@@ -26,14 +26,19 @@ func TestRunPipeWithoutIDsThenDoesNotFilter(t *testing.T) {
 	folder := t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 	srcfile, err := os.Create(filepath.Join(folder, "source.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, srcfile.Close())
 	debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
 	require.NoError(t, err)
+	require.NoError(t, debfile.Close())
 	filteredtarfile, err := os.Create(filepath.Join(folder, "filtered.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, filteredtarfile.Close())
 	filtereddebfile, err := os.Create(filepath.Join(folder, "filtered.deb"))
 	require.NoError(t, err)
+	require.NoError(t, filtereddebfile.Close())
 
 	config := config.Project{
 		Dist: folder,
@@ -101,12 +106,16 @@ func TestRunPipeWithIDsThenFilters(t *testing.T) {
 	folder := t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 	debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
 	require.NoError(t, err)
+	require.NoError(t, debfile.Close())
 	filteredtarfile, err := os.Create(filepath.Join(folder, "filtered.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, filteredtarfile.Close())
 	filtereddebfile, err := os.Create(filepath.Join(folder, "filtered.deb"))
 	require.NoError(t, err)
+	require.NoError(t, filtereddebfile.Close())
 
 	config := config.Project{
 		Dist: folder,
diff --git a/internal/pipe/snapcraft/snapcraft_test.go b/internal/pipe/snapcraft/snapcraft_test.go
index 5cb96868..970290d6 100644
--- a/internal/pipe/snapcraft/snapcraft_test.go
+++ b/internal/pipe/snapcraft/snapcraft_test.go
@@ -423,8 +423,9 @@ func addBinaries(t *testing.T, ctx *context.Context, name, dist string) {
 			var folder = goos + goarch
 			require.NoError(t, os.MkdirAll(filepath.Join(dist, folder), 0755))
 			var binPath = filepath.Join(dist, folder, name)
-			_, err := os.Create(binPath)
+			f, err := os.Create(binPath)
 			require.NoError(t, err)
+			require.NoError(t, f.Close())
 			ctx.Artifacts.Add(&artifact.Artifact{
 				Name:   name,
 				Path:   binPath,
diff --git a/internal/pipe/upload/upload_test.go b/internal/pipe/upload/upload_test.go
index 86295107..9d2dde4d 100644
--- a/internal/pipe/upload/upload_test.go
+++ b/internal/pipe/upload/upload_test.go
@@ -151,8 +151,10 @@ func TestRunPipe_ModeArchive(t *testing.T) {
 	var folder = t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 	debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
 	require.NoError(t, err)
+	require.NoError(t, debfile.Close())
 
 	var ctx = context.New(config.Project{
 		ProjectName: "goreleaser",
@@ -286,8 +288,10 @@ func TestRunPipe_ModeArchive_CustomArtifactName(t *testing.T) {
 	var folder = t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 	debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
 	require.NoError(t, err)
+	require.NoError(t, debfile.Close())
 
 	var ctx = context.New(config.Project{
 		ProjectName: "goreleaser",
@@ -354,6 +358,7 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
 	var folder = t.TempDir()
 	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
 	require.NoError(t, err)
+	require.NoError(t, tarfile.Close())
 
 	var ctx = context.New(config.Project{
 		ProjectName: "goreleaser",
diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go
index b1274273..c6fd5eaf 100644
--- a/pkg/archive/archive_test.go
+++ b/pkg/archive/archive_test.go
@@ -11,6 +11,7 @@ func TestArchive(t *testing.T) {
 	var folder = t.TempDir()
 	empty, err := os.Create(folder + "/empty.txt")
 	require.NoError(t, err)
+	require.NoError(t, empty.Close())
 	require.NoError(t, os.Mkdir(folder+"/folder-inside", 0755))
 
 	for _, format := range []string{"tar.gz", "zip", "gz", "tar.xz", "willbeatargzanyway"} {
@@ -19,9 +20,12 @@ func TestArchive(t *testing.T) {
 			file, err := os.Create(folder + "/folder." + format)
 			require.NoError(t, err)
 			var archive = New(file)
+			t.Cleanup(func() {
+				require.NoError(t, archive.Close())
+				require.NoError(t, file.Close())
+			})
 			require.NoError(t, archive.Add("empty.txt", empty.Name()))
 			require.Error(t, archive.Add("dont.txt", empty.Name()+"_nope"))
-			require.NoError(t, archive.Close())
 		})
 	}
 }
diff --git a/pkg/archive/gzip/gzip_test.go b/pkg/archive/gzip/gzip_test.go
index 01c4d0b8..6e0729a0 100644
--- a/pkg/archive/gzip/gzip_test.go
+++ b/pkg/archive/gzip/gzip_test.go
@@ -16,14 +16,13 @@ func TestGzFile(t *testing.T) {
 	require.NoError(t, err)
 	defer f.Close() // nolint: errcheck
 	archive := New(f)
+	defer archive.Close() // nolint: errcheck
 
 	require.NoError(t, archive.Add("sub1/sub2/subfoo.txt", "../testdata/sub1/sub2/subfoo.txt"))
 	require.EqualError(t, archive.Add("foo.txt", "../testdata/foo.txt"), "gzip: failed to add foo.txt, only one file can be archived in gz format")
 	require.NoError(t, archive.Close())
-
 	require.NoError(t, f.Close())
 
-	t.Log(f.Name())
 	f, err = os.Open(f.Name())
 	require.NoError(t, err)
 	defer f.Close() // nolint: errcheck
diff --git a/pkg/archive/targz/targz_test.go b/pkg/archive/targz/targz_test.go
index a580ae0b..aa413443 100644
--- a/pkg/archive/targz/targz_test.go
+++ b/pkg/archive/targz/targz_test.go
@@ -17,6 +17,7 @@ func TestTarGzFile(t *testing.T) {
 	require.NoError(t, err)
 	defer f.Close() // nolint: errcheck
 	archive := New(f)
+	defer archive.Close() // nolint: errcheck
 
 	require.Error(t, archive.Add("nope.txt", "../testdata/nope.txt"))
 	require.NoError(t, archive.Add("foo.txt", "../testdata/foo.txt"))
diff --git a/pkg/archive/tarxz/tarxz_test.go b/pkg/archive/tarxz/tarxz_test.go
index 2b0bfa55..46da4b87 100644
--- a/pkg/archive/tarxz/tarxz_test.go
+++ b/pkg/archive/tarxz/tarxz_test.go
@@ -17,6 +17,7 @@ func TestTarXzFile(t *testing.T) {
 	require.NoError(t, err)
 	defer f.Close() // nolint: errcheck
 	archive := New(f)
+	defer archive.Close() // nolint: errcheck
 
 	require.Error(t, archive.Add("nope.txt", "../testdata/nope.txt"))
 	require.NoError(t, archive.Add("foo.txt", "../testdata/foo.txt"))
@@ -30,7 +31,6 @@ func TestTarXzFile(t *testing.T) {
 	require.Error(t, archive.Add("tar.go", "tar.go"))
 	require.NoError(t, f.Close())
 
-	t.Log(f.Name())
 	f, err = os.Open(f.Name())
 	require.NoError(t, err)
 	defer f.Close() // nolint: errcheck
@@ -41,7 +41,6 @@ func TestTarXzFile(t *testing.T) {
 
 	xzf, err := xz.NewReader(f)
 	require.NoError(t, err)
-	//defer xzf.Close() // nolint: errcheck
 
 	var paths []string
 	r := tar.NewReader(xzf)
diff --git a/pkg/archive/zip/zip_test.go b/pkg/archive/zip/zip_test.go
index 81f443ec..a857a103 100644
--- a/pkg/archive/zip/zip_test.go
+++ b/pkg/archive/zip/zip_test.go
@@ -2,7 +2,6 @@ package zip
 
 import (
 	"archive/zip"
-	"fmt"
 	"os"
 	"path/filepath"
 	"testing"
@@ -14,9 +13,9 @@ func TestZipFile(t *testing.T) {
 	var tmp = t.TempDir()
 	f, err := os.Create(filepath.Join(tmp, "test.zip"))
 	require.NoError(t, err)
-	fmt.Println(f.Name())
 	defer f.Close() // nolint: errcheck
 	archive := New(f)
+	defer archive.Close() // nolint: errcheck
 
 	require.Error(t, archive.Add("nope.txt", "../testdata/nope.txt"))
 	require.NoError(t, archive.Add("foo.txt", "../testdata/foo.txt"))
@@ -30,7 +29,6 @@ func TestZipFile(t *testing.T) {
 	require.Error(t, archive.Add("tar.go", "tar.go"))
 	require.NoError(t, f.Close())
 
-	t.Log(f.Name())
 	f, err = os.Open(f.Name())
 	require.NoError(t, err)
 	defer f.Close() // nolint: errcheck
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index 8f92dd02..fb741a9c 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -47,7 +47,7 @@ func TestLoadBadReader(t *testing.T) {
 func TestFile(t *testing.T) {
 	f, err := ioutil.TempFile(t.TempDir(), "config")
 	require.NoError(t, err)
-	t.Cleanup(func() { f.Close() })
+	require.NoError(t, f.Close())
 	_, err = Load(filepath.Join(f.Name()))
 	require.NoError(t, err)
 }