@@ -11,7 +11,15 @@ import (
1111 "github.com/stackitcloud/stackit-cli/internal/pkg/auth"
1212)
1313
14+ func overwriteCacheDir (t * testing.T ) func () {
15+ cacheDirOverwrite = t .TempDir ()
16+ return func () {
17+ cacheDirOverwrite = ""
18+ }
19+ }
20+
1421func TestGetObjectErrors (t * testing.T ) {
22+ defer overwriteCacheDir (t )()
1523 if err := Init (); err != nil {
1624 t .Fatalf ("cache init failed: %s" , err )
1725 }
@@ -42,10 +50,6 @@ func TestGetObjectErrors(t *testing.T) {
4250
4351 // setup
4452 if tt .expectFile {
45- err := os .MkdirAll (cacheFolderPath , 0o750 )
46- if err != nil {
47- t .Fatalf ("create cache folder: %s" , err .Error ())
48- }
4953 path := filepath .Join (cacheFolderPath , id )
5054 if err := os .WriteFile (path , []byte ("dummy" ), 0o600 ); err != nil {
5155 t .Fatalf ("setup: WriteFile (%s) failed" , path )
@@ -71,6 +75,7 @@ func TestGetObjectErrors(t *testing.T) {
7175 }
7276}
7377func TestPutObject (t * testing.T ) {
78+ defer overwriteCacheDir (t )()
7479 if err := Init (); err != nil {
7580 t .Fatalf ("cache init failed: %s" , err )
7681 }
@@ -145,6 +150,7 @@ func TestPutObject(t *testing.T) {
145150}
146151
147152func TestDeleteObject (t * testing.T ) {
153+ defer overwriteCacheDir (t )()
148154 if err := Init (); err != nil {
149155 t .Fatalf ("cache init failed: %s" , err )
150156 }
@@ -182,8 +188,11 @@ func TestDeleteObject(t *testing.T) {
182188
183189 // setup
184190 if tt .existingFile {
191+ if err := os .MkdirAll (cacheFolderPath , 0o700 ); err != nil {
192+ t .Fatalf ("setup: MkdirAll (%s) failed: %v" , path , err )
193+ }
185194 if err := os .WriteFile (path , []byte ("dummy" ), 0o600 ); err != nil {
186- t .Fatalf ("setup: WriteFile (%s) failed" , path )
195+ t .Fatalf ("setup: WriteFile (%s) failed: %v " , path , err )
187196 }
188197 }
189198 // test
@@ -228,6 +237,7 @@ func TestWriteAndRead(t *testing.T) {
228237 },
229238 } {
230239 t .Run (tt .name , func (t * testing.T ) {
240+ defer overwriteCacheDir (t )()
231241 if tt .clearKeys {
232242 clearKeys (t )
233243 }
@@ -256,6 +266,7 @@ func TestWriteAndRead(t *testing.T) {
256266}
257267
258268func TestCacheCleanup (t * testing.T ) {
269+ defer overwriteCacheDir (t )()
259270 if err := Init (); err != nil {
260271 t .Fatalf ("cache init failed: %s" , err )
261272 }
@@ -279,3 +290,10 @@ func TestCacheCleanup(t *testing.T) {
279290 t .Fatalf ("getobject failed with unexpected error: %v" , err )
280291 }
281292}
293+
294+ func TestInit (t * testing.T ) {
295+ // test that init without cache directory overwrite works
296+ if err := Init (); err != nil {
297+ t .Fatalf ("cache init failed: %s" , err )
298+ }
299+ }
0 commit comments