Преглед изворни кода

fixed VCS temp file remove

jguer пре 2 година
родитељ
комит
4d5131d6c7
2 измењених фајлова са 23 додато и 16 уклоњено
  1. 10 8
      pkg/vcs/vcs.go
  2. 13 8
      pkg/vcs/vcs_test.go

+ 10 - 8
pkg/vcs/vcs.go

@@ -30,13 +30,14 @@ type OriginInfoByURL map[string]OriginInfo
 
 // OriginInfo contains the last commit sha of a repo
 // Example:
-// "github.com/Jguer/yay.git": {
-// 	"protocols": [
-// 		"https"
-// 	],
-// 	"branch": "next",
-// 	"sha": "c1171d41467c68ffd3c46748182a16366aaaf87b"
-// }.
+//
+//	"github.com/Jguer/yay.git": {
+//		"protocols": [
+//			"https"
+//		],
+//		"branch": "next",
+//		"sha": "c1171d41467c68ffd3c46748182a16366aaaf87b"
+//	}.
 type OriginInfo struct {
 	Protocols []string `json:"protocols"`
 	Branch    string   `json:"branch"`
@@ -90,7 +91,8 @@ func (v *InfoStore) getCommit(ctx context.Context, url, branch string, protocols
 }
 
 func (v *InfoStore) Update(ctx context.Context, pkgName string,
-	sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup) {
+	sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup,
+) {
 	defer wg.Done()
 
 	info := make(OriginInfoByURL)

+ 13 - 8
pkg/vcs/vcs_test.go

@@ -13,6 +13,7 @@ import (
 	gosrc "github.com/Morganamilo/go-srcinfo"
 	"github.com/bradleyjkemp/cupaloy"
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 
 	"github.com/Jguer/yay/v11/pkg/settings/exe"
 )
@@ -261,9 +262,9 @@ func TestInfoStore_Update(t *testing.T) {
 		},
 	}
 
-	file, err := os.CreateTemp("/tmp", "yay-vcs-test")
-	assert.NoError(t, err)
-	defer os.Remove(file.Name())
+	file, err := os.CreateTemp("/tmp", "yay-infostore-*-test")
+	filePath := file.Name()
+	require.NoError(t, err)
 
 	for _, tt := range tests {
 		tt := tt
@@ -271,7 +272,7 @@ func TestInfoStore_Update(t *testing.T) {
 			t.Parallel()
 			v := &InfoStore{
 				OriginsByPackage: tt.fields.OriginsByPackage,
-				FilePath:         file.Name(),
+				FilePath:         filePath,
 				CmdBuilder:       tt.fields.CmdBuilder,
 			}
 			var mux sync.Mutex
@@ -296,6 +297,8 @@ func TestInfoStore_Update(t *testing.T) {
 			cupaloy.SnapshotT(t, marshalledinfo)
 		})
 	}
+
+	require.NoError(t, os.Remove(filePath))
 }
 
 func TestInfoStore_Remove(t *testing.T) {
@@ -325,9 +328,9 @@ func TestInfoStore_Remove(t *testing.T) {
 		},
 	}
 
-	file, err := os.CreateTemp("/tmp", "yay-vcs-test")
-	assert.NoError(t, err)
-	defer os.Remove(file.Name())
+	file, err := os.CreateTemp("/tmp", "yay-vcs-*-test")
+	filePath := file.Name()
+	require.NoError(t, err)
 
 	for _, tt := range tests {
 		tt := tt
@@ -335,10 +338,12 @@ func TestInfoStore_Remove(t *testing.T) {
 			t.Parallel()
 			v := &InfoStore{
 				OriginsByPackage: tt.fields.OriginsByPackage,
-				FilePath:         file.Name(),
+				FilePath:         filePath,
 			}
 			v.RemovePackage(tt.args.pkgs)
 			assert.Len(t, tt.fields.OriginsByPackage, 2)
 		})
 	}
+
+	require.NoError(t, os.Remove(filePath))
 }