@@ -51,9 +51,9 @@ func writePrivateKey(t *testing.T, privateKey *rsa.PrivateKey, fileLocation stri
51
51
}
52
52
}
53
53
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 )
57
57
58
58
// Generate a private key for the CA
59
59
privateKey := generatePrivateKey (t )
@@ -94,15 +94,12 @@ func generateCertificate(t *testing.T, isCa bool) (certficateLocation, privateKe
94
94
}
95
95
96
96
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
100
98
t .Cleanup (func () {
101
- removeTempFile (t , validCaCertificate )
102
- removeTempFile (t , validCaKey )
103
- removeTempFile (t , validClientCertificate )
104
- removeTempFile (t , validClientKey )
99
+ os .RemoveAll (tempDir )
105
100
})
101
+ validCaCertificate , validCaKey := generateCertificate (t , tempDir , true )
102
+ validClientCertificate , validClientKey := generateCertificate (t , tempDir , false )
106
103
107
104
tests := []struct {
108
105
name string
@@ -203,18 +200,12 @@ func TestGetTLSConfig(t *testing.T) {
203
200
}
204
201
205
202
// 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-" )
208
205
if err != nil {
209
206
t .Fatal (err )
210
207
}
208
+ defer tmpfile .Close ()
211
209
212
210
return tmpfile .Name ()
213
211
}
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