@@ -16,6 +16,16 @@ import (
1616 "github.com/stackitcloud/stackit-sdk-go/core/config"
1717)
1818
19+ func setTemporaryHome (t * testing.T ) {
20+ old := userHomeDir
21+ t .Cleanup (func () {
22+ userHomeDir = old
23+ })
24+ userHomeDir = func () (string , error ) {
25+ return t .TempDir (), nil
26+ }
27+ }
28+
1929func fixtureServiceAccountKey (mods ... func (* clients.ServiceAccountKeyResponse )) * clients.ServiceAccountKeyResponse {
2030 validUntil := time .Now ().Add (time .Hour )
2131 serviceAccountKeyResponse := & clients.ServiceAccountKeyResponse {
@@ -150,6 +160,7 @@ func TestSetupAuth(t *testing.T) {
150160 },
151161 } {
152162 t .Run (test .desc , func (t * testing.T ) {
163+ setTemporaryHome (t )
153164 if test .setKeys {
154165 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , saKeyFile .Name ())
155166 t .Setenv ("STACKIT_PRIVATE_KEY_PATH" , privateKeyFile .Name ())
@@ -232,6 +243,7 @@ func TestReadCredentials(t *testing.T) {
232243 },
233244 } {
234245 t .Run (test .desc , func (t * testing.T ) {
246+ setTemporaryHome (t )
235247 t .Setenv ("STACKIT_CREDENTIALS_PATH" , test .pathEnv )
236248
237249 var credential string
@@ -342,6 +354,7 @@ func TestDefaultAuth(t *testing.T) {
342354 },
343355 } {
344356 t .Run (test .desc , func (t * testing.T ) {
357+ setTemporaryHome (t )
345358 if test .setKeys {
346359 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , saKeyFile .Name ())
347360 t .Setenv ("STACKIT_PRIVATE_KEY_PATH" , privateKeyFile .Name ())
@@ -406,6 +419,7 @@ func TestTokenAuth(t *testing.T) {
406419 },
407420 } {
408421 t .Run (test .desc , func (t * testing.T ) {
422+ setTemporaryHome (t )
409423 t .Setenv ("STACKIT_SERVICE_ACCOUNT_TOKEN" , "" )
410424 t .Setenv ("STACKIT_CREDENTIALS_PATH" , "test-path" )
411425
@@ -499,6 +513,7 @@ func TestKeyAuth(t *testing.T) {
499513 },
500514 } {
501515 t .Run (test .desc , func (t * testing.T ) {
516+ setTemporaryHome (t )
502517 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY" , "" )
503518 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , "" )
504519 t .Setenv ("STACKIT_PRIVATE_KEY" , "" )
@@ -557,7 +572,7 @@ func TestNoAuth(t *testing.T) {
557572 },
558573 } {
559574 t .Run (test .desc , func (t * testing.T ) {
560- // Get the default authentication client and ensure that it's not nil
575+ setTemporaryHome ( t ) // Get the default authentication client and ensure that it's not nil
561576 authClient , err := NoAuth ()
562577 if err != nil {
563578 t .Fatalf ("Test returned error on valid test case: %v" , err )
@@ -624,6 +639,7 @@ func TestGetServiceAccountEmail(t *testing.T) {
624639 },
625640 } {
626641 t .Run (test .description , func (t * testing.T ) {
642+ setTemporaryHome (t )
627643 if test .envEmailSet {
628644 t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "env_email" )
629645 } else {
@@ -742,6 +758,7 @@ func TestGetPrivateKey(t *testing.T) {
742758 },
743759 } {
744760 t .Run (test .name , func (t * testing.T ) {
761+ setTemporaryHome (t )
745762 t .Setenv ("STACKIT_CREDENTIALS_PATH" , test .credentialsFilePath )
746763
747764 if test .envPrivateKeyPathSet {
@@ -843,6 +860,7 @@ func TestGetServiceAccountKey(t *testing.T) {
843860 },
844861 } {
845862 t .Run (test .name , func (t * testing.T ) {
863+ setTemporaryHome (t )
846864 t .Setenv ("STACKIT_CREDENTIALS_PATH" , test .credentialsFilePath )
847865
848866 if test .envServiceAccountKeyPathSet {
0 commit comments