Skip to content

Commit bab0180

Browse files
committed
Fix temp file creation
Signed-off-by: Søren Juul <[email protected]>
1 parent 4d12839 commit bab0180

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

cmd/cosign/cli/options/registry_test.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func writePrivateKey(t *testing.T, privateKey *rsa.PrivateKey, fileLocation stri
5151
}
5252
}
5353

54-
func generateCertificate(t *testing.T, isCa bool) (certficateLocation, privateKeyLocation string) {
55-
certficateLocation = createTempFile(t)
56-
privateKeyLocation = createTempFile(t)
54+
func generateCertificate(t *testing.T, dir string, isCa bool) (certficateLocation, privateKeyLocation string) {
55+
certficateLocation = createTempFile(t, dir)
56+
privateKeyLocation = createTempFile(t, dir)
5757

5858
// Generate a private key for the CA
5959
privateKey := generatePrivateKey(t)
@@ -94,15 +94,12 @@ func generateCertificate(t *testing.T, isCa bool) (certficateLocation, privateKe
9494
}
9595

9696
func TestGetTLSConfig(t *testing.T) {
97-
validCaCertificate, validCaKey := generateCertificate(t, true)
98-
validClientCertificate, validClientKey := generateCertificate(t, false)
99-
97+
tempDir := t.TempDir() // Create a temporary directory for testing
10098
t.Cleanup(func() {
101-
removeTempFile(t, validCaCertificate)
102-
removeTempFile(t, validCaKey)
103-
removeTempFile(t, validClientCertificate)
104-
removeTempFile(t, validClientKey)
99+
os.RemoveAll(tempDir)
105100
})
101+
validCaCertificate, validCaKey := generateCertificate(t, tempDir, true)
102+
validClientCertificate, validClientKey := generateCertificate(t, tempDir, false)
106103

107104
tests := []struct {
108105
name string
@@ -203,18 +200,12 @@ func TestGetTLSConfig(t *testing.T) {
203200
}
204201

205202
// Helper function to create temporary files for testing
206-
func createTempFile(t *testing.T) string {
207-
tmpfile, err := os.CreateTemp("", "registry-test-")
203+
func createTempFile(t *testing.T, dir string) string {
204+
tmpfile, err := os.CreateTemp(dir, "registry-test-")
208205
if err != nil {
209206
t.Fatal(err)
210207
}
208+
defer tmpfile.Close()
211209

212210
return tmpfile.Name()
213211
}
214-
215-
// Helper function to remove temporary files after testing
216-
func removeTempFile(t *testing.T, filename string) {
217-
if err := os.Remove(filename); err != nil {
218-
t.Fatal(err)
219-
}
220-
}

0 commit comments

Comments
 (0)