@@ -448,6 +448,7 @@ func TestKeyAuth(t *testing.T) {
448448 envVarPrivateKey string
449449 expectedPrivateKey string
450450 isValid bool
451+ homeDir func (* testing.T ) string
451452 }{
452453 {
453454 desc : "configured_private_key" ,
@@ -484,6 +485,7 @@ func TestKeyAuth(t *testing.T) {
484485 serviceAccountKey : nil ,
485486 configuredPrivateKey : configuredPrivateKey ,
486487 isValid : false ,
488+ homeDir : func (t * testing.T ) string { return t .TempDir () },
487489 },
488490 {
489491 desc : "missing_private_key" ,
@@ -496,14 +498,23 @@ func TestKeyAuth(t *testing.T) {
496498 serviceAccountKey : nil ,
497499 configuredPrivateKey : "" ,
498500 isValid : false ,
501+ homeDir : func (t * testing.T ) string { return t .TempDir () },
499502 },
500503 } {
501504 t .Run (test .desc , func (t * testing.T ) {
502505 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY" , "" )
503506 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , "" )
504507 t .Setenv ("STACKIT_PRIVATE_KEY" , "" )
505508 t .Setenv ("STACKIT_PRIVATE_KEY_PATH" , "" )
506- t .Setenv ("HOME" , t .TempDir ())
509+ if homeDir := test .homeDir ; homeDir != nil {
510+ old := userHomeDir
511+ t .Cleanup (func () {
512+ userHomeDir = old
513+ })
514+ userHomeDir = func () (string , error ) {
515+ return test .homeDir (t ), nil
516+ }
517+ }
507518
508519 var saKey string
509520 if test .serviceAccountKey != nil {
@@ -770,6 +781,7 @@ func TestGetServiceAccountKey(t *testing.T) {
770781 credentialsFilePath string
771782 wantErr bool
772783 expectedKey string
784+ userHomeDir func (* testing.T ) string
773785 }{
774786 {
775787 name : "cfg_sa_key" ,
@@ -841,11 +853,20 @@ func TestGetServiceAccountKey(t *testing.T) {
841853 cfg : & config.Configuration {},
842854 wantErr : true ,
843855 expectedKey : "" ,
856+ userHomeDir : func (t * testing.T ) string { return t .TempDir () },
844857 },
845858 } {
846859 t .Run (test .name , func (t * testing.T ) {
847- t .Setenv ("HOME" , t .TempDir ())
848860 t .Setenv ("STACKIT_CREDENTIALS_PATH" , test .credentialsFilePath )
861+ if homeDir := test .userHomeDir ; homeDir != nil {
862+ old := userHomeDir
863+ t .Cleanup (func () {
864+ userHomeDir = old
865+ })
866+ userHomeDir = func () (string , error ) {
867+ return homeDir (t ), nil
868+ }
869+ }
849870
850871 if test .envServiceAccountKeyPathSet {
851872 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , "test_resources/test_string_key.txt" )
0 commit comments