■ ■ ■ ■ ■ ■
internal/config/application_test.go
| 1 | + | package config |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "os" |
| 5 | + | "path" |
| 6 | + | "testing" |
| 7 | + | |
| 8 | + | "github.com/adrg/xdg" |
| 9 | + | "github.com/spf13/viper" |
| 10 | + | "github.com/stretchr/testify/assert" |
| 11 | + | ) |
| 12 | + | |
| 13 | + | // TODO: set negative case when config.yaml is no longer a valid option |
| 14 | + | func TestApplicationConfig(t *testing.T) { |
| 15 | + | // config is picked up at desired configuration paths |
| 16 | + | // VALID: .syft.yaml, .syft/config.yaml, ~/.syft.yaml, <XDG_CONFIG_HOME>/syft/config.yaml |
| 17 | + | // DEPRECATED: config.yaml is currently supported by |
| 18 | + | tests := []struct { |
| 19 | + | name string |
| 20 | + | setup func(t *testing.T) string |
| 21 | + | assertions func(t *testing.T, app *Application) |
| 22 | + | Cleanup func(t *testing.T) |
| 23 | + | }{ |
| 24 | + | { |
| 25 | + | name: "explicit config", |
| 26 | + | setup: func(t *testing.T) string { |
| 27 | + | return "./test-fixtures/.syft.yaml" |
| 28 | + | }, // no-op for explicit config |
| 29 | + | assertions: func(t *testing.T, app *Application) { |
| 30 | + | assert.Equal(t, "test-explicit-config", app.File) |
| 31 | + | }, |
| 32 | + | Cleanup: func(t *testing.T) {}, |
| 33 | + | }, |
| 34 | + | { |
| 35 | + | name: "current working directory named config", |
| 36 | + | setup: func(t *testing.T) string { |
| 37 | + | err := os.Chdir("./test-fixtures/config-wd-file") // change application cwd to test-fixtures |
| 38 | + | if err != nil { |
| 39 | + | t.Fatalf("%s failed to change cwd: %+v", t.Name(), err) |
| 40 | + | } |
| 41 | + | return "" |
| 42 | + | }, |
| 43 | + | assertions: func(t *testing.T, app *Application) { |
| 44 | + | assert.Equal(t, "test-wd-named-config", app.File) |
| 45 | + | }, |
| 46 | + | Cleanup: func(t *testing.T) {}, |
| 47 | + | }, |
| 48 | + | { |
| 49 | + | name: "current working directory syft dir config", |
| 50 | + | setup: func(t *testing.T) string { |
| 51 | + | err := os.Chdir("./test-fixtures/config-dir-test") // change application cwd to test-fixtures |
| 52 | + | if err != nil { |
| 53 | + | t.Fatalf("%s failed to change cwd: %+v", t.Name(), err) |
| 54 | + | } |
| 55 | + | return "" |
| 56 | + | }, |
| 57 | + | assertions: func(t *testing.T, app *Application) { |
| 58 | + | assert.Equal(t, "test-dir-config", app.File) |
| 59 | + | }, |
| 60 | + | Cleanup: func(t *testing.T) {}, |
| 61 | + | }, |
| 62 | + | { |
| 63 | + | name: "home directory file config", |
| 64 | + | setup: func(t *testing.T) string { |
| 65 | + | // Because Setenv affects the whole process, it cannot be used in parallel tests or |
| 66 | + | // tests with parallel ancestors: see separate XDG test for consequence of this |
| 67 | + | t.Setenv("HOME", "./test-fixtures/config-home-test") |
| 68 | + | err := os.Link("./test-fixtures/config-home-test/config-file/.syft.yaml", "./test-fixtures/config-home-test/.syft.yaml") |
| 69 | + | if err != nil { |
| 70 | + | t.Fatalf("%s failed to link home config: %+v", t.Name(), err) |
| 71 | + | } |
| 72 | + | return "" |
| 73 | + | }, |
| 74 | + | assertions: func(t *testing.T, app *Application) { |
| 75 | + | assert.Equal(t, "test-home-config", app.File) |
| 76 | + | }, |
| 77 | + | Cleanup: func(t *testing.T) { |
| 78 | + | err := os.Remove("./test-fixtures/config-home-test/.syft.yaml") // |
| 79 | + | if err != nil { |
| 80 | + | t.Fatalf("%s failed to remove home config link: %+v", t.Name(), err) |
| 81 | + | } |
| 82 | + | }, |
| 83 | + | }, |
| 84 | + | { |
| 85 | + | name: "XDG file config", |
| 86 | + | setup: func(t *testing.T) string { |
| 87 | + | wd, err := os.Getwd() |
| 88 | + | if err != nil { |
| 89 | + | t.Fatalf("%s: failed to get working directory: %+v", t.Name(), err) |
| 90 | + | } |
| 91 | + | configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata |
| 92 | + | t.Setenv("XDG_CONFIG_DIRS", configDir) |
| 93 | + | xdg.Reload() |
| 94 | + | return "" |
| 95 | + | }, |
| 96 | + | assertions: func(t *testing.T, app *Application) { |
| 97 | + | assert.Equal(t, "test-home-XDG-config", app.File) |
| 98 | + | }, |
| 99 | + | Cleanup: func(t *testing.T) {}, |
| 100 | + | }, |
| 101 | + | } |
| 102 | + | for _, test := range tests { |
| 103 | + | t.Run(test.name, func(t *testing.T) { |
| 104 | + | defer test.Cleanup(t) |
| 105 | + | wd, err := os.Getwd() |
| 106 | + | if err != nil { |
| 107 | + | t.Fatalf("failed to get working directory: %+v", err) |
| 108 | + | } |
| 109 | + | defer os.Chdir(wd) // reset working directory after test |
| 110 | + | application := &Application{} |
| 111 | + | viperInstance := viper.New() |
| 112 | + | |
| 113 | + | configPath := test.setup(t) |
| 114 | + | err = application.LoadAllValues(viperInstance, configPath) |
| 115 | + | if err != nil { |
| 116 | + | t.Fatalf("failed to load application config: %+v", err) |
| 117 | + | } |
| 118 | + | test.assertions(t, application) |
| 119 | + | }) |
| 120 | + | } |
| 121 | + | } |
| 122 | + | |