@@ -11,6 +11,7 @@ import (
1111 "time"
1212
1313 "github.com/creativeprojects/resticprofile/util/maybe"
14+ "github.com/spf13/afero"
1415 "github.com/stretchr/testify/assert"
1516 "github.com/stretchr/testify/require"
1617)
@@ -366,67 +367,57 @@ x=0
366367}
367368
368369func TestIncludes (t * testing.T ) {
369- files := []string {}
370- cleanFiles := func () {
371- for _ , file := range files {
372- os .Remove (file )
373- }
374- files = files [:0 ]
375- }
376- defer cleanFiles ()
377-
378- createFile := func (t * testing.T , suffix , content string ) string {
370+ createFile := func (t * testing.T , fs afero.Fs , suffix , content string ) string {
379371 t .Helper ()
380372 name := ""
381- file , err := os . CreateTemp ( "" , "*-" + suffix )
373+ file , err := afero . TempFile ( fs , "" , "*-" + suffix )
382374 if err == nil {
383375 defer file .Close ()
384376 _ , err = file .WriteString (content )
385377 name = file .Name ()
386- files = append (files , name )
387378 }
388379 require .NoError (t , err )
389380 return name
390381 }
391382
392- mustLoadConfig := func (t * testing.T , configFile string ) * Config {
383+ mustLoadConfig := func (t * testing.T , fs afero. Fs , configFile string ) * Config {
393384 t .Helper ()
394- config , err := LoadFile (configFile , "" )
385+ config , err := LoadFile (fs , configFile , "" )
395386 require .NoError (t , err )
396387 return config
397388 }
398389
399390 testID := fmt .Sprintf ("%d" , time .Now ().Unix ())
400391
401392 t .Run ("multiple-includes" , func (t * testing.T ) {
402- defer cleanFiles ()
393+ fs := afero . NewMemMapFs ()
403394 content := fmt .Sprintf (`includes=['*%[1]s.inc.toml','*%[1]s.inc.yaml','*%[1]s.inc.json']` , testID )
404395
405- configFile := createFile (t , "profiles.conf" , content )
406- createFile (t , "d-" + testID + ".inc.toml" , "[one]\n k='v'" )
407- createFile (t , "o-" + testID + ".inc.yaml" , `two: { k: v }` )
408- createFile (t , "j-" + testID + ".inc.json" , `{"three":{ "k": "v" }}` )
396+ configFile := createFile (t , fs , "profiles.conf" , content )
397+ createFile (t , fs , "d-" + testID + ".inc.toml" , "[one]\n k='v'" )
398+ createFile (t , fs , "o-" + testID + ".inc.yaml" , `two: { k: v }` )
399+ createFile (t , fs , "j-" + testID + ".inc.json" , `{"three":{ "k": "v" }}` )
409400
410- config := mustLoadConfig (t , configFile )
401+ config := mustLoadConfig (t , fs , configFile )
411402 assert .True (t , config .IsSet ("includes" ))
412403 assert .True (t , config .HasProfile ("one" ))
413404 assert .True (t , config .HasProfile ("two" ))
414405 assert .True (t , config .HasProfile ("three" ))
415406 })
416407
417408 t .Run ("overrides" , func (t * testing.T ) {
418- defer cleanFiles ()
409+ fs := afero . NewMemMapFs ()
419410
420- configFile := createFile (t , "profiles.conf" , `
411+ configFile := createFile (t , fs , "profiles.conf" , `
421412includes = "*` + testID + `.inc.toml"
422413[default]
423414repository = "default-repo"` )
424415
425- createFile (t , "override-" + testID + ".inc.toml" , `
416+ createFile (t , fs , "override-" + testID + ".inc.toml" , `
426417[default]
427418repository = "overridden-repo"` )
428419
429- config := mustLoadConfig (t , configFile )
420+ config := mustLoadConfig (t , fs , configFile )
430421 assert .True (t , config .HasProfile ("default" ))
431422
432423 profile , err := config .GetProfile ("default" )
@@ -435,26 +426,26 @@ repository = "overridden-repo"`)
435426 })
436427
437428 t .Run ("mixins" , func (t * testing.T ) {
438- defer cleanFiles ()
429+ fs := afero . NewMemMapFs ()
439430
440- configFile := createFile (t , "profiles.conf" , `
431+ configFile := createFile (t , fs , "profiles.conf" , `
441432version = 2
442433includes = "*` + testID + `.inc.toml"
443434[profiles.default]
444435use = "another-run-before"
445436run-before = "default-before"` )
446437
447- createFile (t , "mixin-" + testID + ".inc.toml" , `
438+ createFile (t , fs , "mixin-" + testID + ".inc.toml" , `
448439[mixins.another-run-before]
449440"run-before..." = "another-run-before"
450441[mixins.another-run-before2]
451442"run-before..." = "another-run-before2"` )
452443
453- createFile (t , "mixin-use-" + testID + ".inc.toml" , `
444+ createFile (t , fs , "mixin-use-" + testID + ".inc.toml" , `
454445[profiles.default]
455446use = "another-run-before2"` )
456447
457- config := mustLoadConfig (t , configFile )
448+ config := mustLoadConfig (t , fs , configFile )
458449 assert .True (t , config .HasProfile ("default" ))
459450
460451 profile , err := config .GetProfile ("default" )
@@ -463,56 +454,56 @@ use = "another-run-before2"`)
463454 })
464455
465456 t .Run ("hcl-includes-only-hcl" , func (t * testing.T ) {
466- defer cleanFiles ()
457+ fs := afero . NewMemMapFs ()
467458
468- configFile := createFile (t , "profiles.hcl" , `includes = "*` + testID + `.inc.*"` )
469- createFile (t , "pass-" + testID + ".inc.hcl" , `one { }` )
459+ configFile := createFile (t , fs , "profiles.hcl" , `includes = "*` + testID + `.inc.*"` )
460+ createFile (t , fs , "pass-" + testID + ".inc.hcl" , `one { }` )
470461
471- config := mustLoadConfig (t , configFile )
462+ config := mustLoadConfig (t , fs , configFile )
472463 assert .True (t , config .HasProfile ("one" ))
473464
474- createFile (t , "fail-" + testID + ".inc.toml" , `[two]` )
475- _ , err := LoadFile (configFile , "" )
465+ createFile (t , fs , "fail-" + testID + ".inc.toml" , `[two]` )
466+ _ , err := LoadFile (fs , configFile , "" )
476467 assert .Error (t , err )
477468 assert .Regexp (t , ".+ is in hcl format, includes must use the same format" , err .Error ())
478469 })
479470
480471 t .Run ("non-hcl-include-no-hcl" , func (t * testing.T ) {
481- defer cleanFiles ()
472+ fs := afero . NewMemMapFs ()
482473
483- configFile := createFile (t , "profiles.toml" , `includes = "*` + testID + `.inc.*"` )
484- createFile (t , "pass-" + testID + ".inc.toml" , "[one]\n k='v'" )
474+ configFile := createFile (t , fs , "profiles.toml" , `includes = "*` + testID + `.inc.*"` )
475+ createFile (t , fs , "pass-" + testID + ".inc.toml" , "[one]\n k='v'" )
485476
486- config := mustLoadConfig (t , configFile )
477+ config := mustLoadConfig (t , fs , configFile )
487478 assert .True (t , config .HasProfile ("one" ))
488479
489- createFile (t , "fail-" + testID + ".inc.hcl" , `one { }` )
490- _ , err := LoadFile (configFile , "" )
480+ createFile (t , fs , "fail-" + testID + ".inc.hcl" , `one { }` )
481+ _ , err := LoadFile (fs , configFile , "" )
491482 assert .Error (t , err )
492483 assert .Regexp (t , "hcl format .+ cannot be used in includes from toml" , err .Error ())
493484 })
494485
495486 t .Run ("cannot-load-different-versions" , func (t * testing.T ) {
496- defer cleanFiles ()
487+ fs := afero . NewMemMapFs ()
497488 content := fmt .Sprintf (`includes=['*%s.inc.json']` , testID )
498489
499- configFile := createFile (t , "profiles.conf" , content )
500- createFile (t , "a-" + testID + ".inc.json" , `{"version": 2, "profiles": {"one":{}}}` )
501- createFile (t , "b-" + testID + ".inc.json" , `{"two":{}}` )
490+ configFile := createFile (t , fs , "profiles.conf" , content )
491+ createFile (t , fs , "a-" + testID + ".inc.json" , `{"version": 2, "profiles": {"one":{}}}` )
492+ createFile (t , fs , "b-" + testID + ".inc.json" , `{"two":{}}` )
502493
503- _ , err := LoadFile (configFile , "" )
494+ _ , err := LoadFile (fs , configFile , "" )
504495 assert .ErrorContains (t , err , "cannot include different versions of the configuration file" )
505496 })
506497
507498 t .Run ("cannot-load-different-versions" , func (t * testing.T ) {
508- defer cleanFiles ()
499+ fs := afero . NewMemMapFs ()
509500 content := fmt .Sprintf (`{"version": 2, "includes":["*%s.inc.json"]}` , testID )
510501
511- configFile := createFile (t , "profiles.json" , content )
512- createFile (t , "c-" + testID + ".inc.json" , `{"version": 1, "two":{}}` )
513- createFile (t , "d-" + testID + ".inc.json" , `{"profiles": {"one":{}}}` )
502+ configFile := createFile (t , fs , "profiles.json" , content )
503+ createFile (t , fs , "c-" + testID + ".inc.json" , `{"version": 1, "two":{}}` )
504+ createFile (t , fs , "d-" + testID + ".inc.json" , `{"profiles": {"one":{}}}` )
514505
515- _ , err := LoadFile (configFile , "" )
506+ _ , err := LoadFile (fs , configFile , "" )
516507 assert .ErrorContains (t , err , "cannot include different versions of the configuration file" )
517508 })
518509}
0 commit comments