@@ -48,6 +48,32 @@ func TestRunSubprocess(t *testing.T) {
48
48
49
49
assert .Equal (t , expectedValue , string (content ))
50
50
})
51
+
52
+ t .Run ("Finds secrets file in a directory above the working directory" , func (t * testing.T ) {
53
+ var err error
54
+ topDir := t .TempDir ()
55
+
56
+ fileAbovePath := filepath .Join (topDir , "secrets.yml" )
57
+ _ , err = os .Create (fileAbovePath )
58
+ assert .NoError (t , err )
59
+
60
+ // Create a downwards directory hierarchy, starting from topDir, and
61
+ // chdir to it.
62
+ downDir := filepath .Join (topDir , "dir1" , "dir2" , "dir3" )
63
+ err = os .MkdirAll (downDir , 0o700 )
64
+ assert .NoError (t , err )
65
+ restore := chdir (t , downDir )
66
+ t .Cleanup (restore )
67
+
68
+ code , err := RunSubprocess (& SubprocessConfig {
69
+ Args : []string {"true" },
70
+ RecurseUp : true ,
71
+ Filepath : "secrets.yml" ,
72
+ })
73
+
74
+ assert .NoError (t , err )
75
+ assert .Equal (t , 0 , code )
76
+ })
51
77
}
52
78
53
79
func TestHandleResultsFromProvider (t * testing.T ) {
@@ -244,6 +270,7 @@ func TestDefaultVariableResolutionWithValue(t *testing.T) {
244
270
assert .Equal (t , expectedValue , string (content ))
245
271
})
246
272
}
273
+
247
274
func TestConvertSubsToMap (t * testing.T ) {
248
275
t .Run ("Substitutions are returned as a map used later for interpolation" , func (t * testing.T ) {
249
276
input := []string {
@@ -318,12 +345,10 @@ func TestLocateFileRecurseUp(t *testing.T) {
318
345
filename := "test.txt"
319
346
320
347
t .Run ("Finds file in current working directory" , func (t * testing.T ) {
321
- topDir , err := os .MkdirTemp ("" , "summon" )
322
- assert .NoError (t , err )
323
- defer os .RemoveAll (topDir )
348
+ topDir := t .TempDir ()
324
349
325
350
localFilePath := filepath .Join (topDir , filename )
326
- _ , err = os .Create (localFilePath )
351
+ _ , err : = os .Create (localFilePath )
327
352
assert .NoError (t , err )
328
353
329
354
gotPath , err := findInParentTree (filename , topDir )
@@ -332,62 +357,54 @@ func TestLocateFileRecurseUp(t *testing.T) {
332
357
assert .Equal (t , localFilePath , gotPath )
333
358
})
334
359
335
- t .Run ("Finds file in a higher working directory" , func (t * testing.T ) {
336
- topDir , err := os .MkdirTemp ("" , "summon" )
337
- assert .NoError (t , err )
338
- defer os .RemoveAll (topDir )
360
+ t .Run ("Finds file in a directory above the working directory" , func (t * testing.T ) {
361
+ topDir := t .TempDir ()
339
362
340
- higherFilePath := filepath .Join (topDir , filename )
341
- _ , err = os .Create (higherFilePath )
363
+ fileAbovePath := filepath .Join (topDir , filename )
364
+ _ , err : = os .Create (fileAbovePath )
342
365
assert .NoError (t , err )
343
366
344
367
// Create a downwards directory hierarchy, starting from topDir
345
368
downDir := filepath .Join (topDir , "dir1" , "dir2" , "dir3" )
346
- err = os .MkdirAll (downDir , 0700 )
369
+ err = os .MkdirAll (downDir , 0o700 )
347
370
assert .NoError (t , err )
348
371
349
372
gotPath , err := findInParentTree (filename , downDir )
350
373
assert .NoError (t , err )
351
374
352
- assert .Equal (t , higherFilePath , gotPath )
375
+ assert .Equal (t , fileAbovePath , gotPath )
353
376
})
354
377
355
378
t .Run ("returns a friendly error if file not found" , func (t * testing.T ) {
356
- topDir , err := os .MkdirTemp ("" , "summon" )
357
- assert .NoError (t , err )
358
- defer os .RemoveAll (topDir )
379
+ topDir := t .TempDir ()
359
380
360
381
// A unlikely to exist file name
361
382
nonExistingFileName := strconv .FormatInt (time .Now ().Unix (), 10 )
362
383
wantErrMsg := fmt .Sprintf (
363
384
"unable to locate file specified (%s): reached root of file system" ,
364
385
nonExistingFileName )
365
386
366
- _ , err = findInParentTree (nonExistingFileName , topDir )
387
+ _ , err : = findInParentTree (nonExistingFileName , topDir )
367
388
assert .EqualError (t , err , wantErrMsg )
368
389
})
369
390
370
391
t .Run ("returns a friendly error if file is an absolute path" , func (t * testing.T ) {
371
- topDir , err := os .MkdirTemp ("" , "summon" )
372
- assert .NoError (t , err )
373
- defer os .RemoveAll (topDir )
392
+ topDir := t .TempDir ()
374
393
375
394
absFileName := "/foo/bar/baz"
376
395
wantErrMsg := "file specified (/foo/bar/baz) is an absolute path: will not recurse up"
377
396
378
- _ , err = findInParentTree (absFileName , topDir )
397
+ _ , err : = findInParentTree (absFileName , topDir )
379
398
assert .EqualError (t , err , wantErrMsg )
380
399
})
381
400
382
401
t .Run ("returns a friendly error in unexpected circumstances (100% coverage)" , func (t * testing.T ) {
383
- topDir , err := os .MkdirTemp ("" , "summon" )
384
- assert .NoError (t , err )
385
- defer os .RemoveAll (topDir )
402
+ topDir := t .TempDir ()
386
403
387
404
fileNameWithNulByte := "pizza\x00 margherita"
388
405
wantErrMsg := "unable to locate file specified (pizza\x00 margherita): stat"
389
406
390
- _ , err = findInParentTree (fileNameWithNulByte , topDir )
407
+ _ , err : = findInParentTree (fileNameWithNulByte , topDir )
391
408
assert .Contains (t , err .Error (), wantErrMsg )
392
409
})
393
410
}
@@ -412,6 +429,7 @@ func TestReturnStatusOfError(t *testing.T) {
412
429
assert .Equal (t , expected , err )
413
430
})
414
431
}
432
+
415
433
func TestNonInteractiveProviderFallback (t * testing.T ) {
416
434
secrets := secretsyml.SecretsMap {
417
435
"key1" : secretsyml.SecretSpec {Path : "path1" },
@@ -433,3 +451,26 @@ func TestNonInteractiveProviderFallback(t *testing.T) {
433
451
assert .Nil (t , result .Error )
434
452
}
435
453
}
454
+
455
+ // chdir changes the current working directory to the named directory and
456
+ // returns a function that, when called, restores the original working
457
+ // directory.
458
+ //
459
+ // Courtesy of https://github.com/golang/go/issues/45182
460
+ // Can be replaced by https://pkg.go.dev/testing@master#T.Chdir
461
+ // when Go 1.24 is out (2025-02).
462
+ func chdir (t * testing.T , dir string ) func () {
463
+ wd , err := os .Getwd ()
464
+ if err != nil {
465
+ t .Fatalf ("chdir %s: %v" , dir , err )
466
+ }
467
+ if err := os .Chdir (dir ); err != nil {
468
+ t .Fatal (err )
469
+ }
470
+
471
+ return func () {
472
+ if err := os .Chdir (wd ); err != nil {
473
+ t .Fatalf ("restoring working directory: %v" , err )
474
+ }
475
+ }
476
+ }
0 commit comments