diff --git a/go.sum b/go.sum index 64c1511b..1edcfcb6 100644 --- a/go.sum +++ b/go.sum @@ -310,6 +310,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tj/assert v0.0.0-20171129193455-018094318fb0 h1:Rw8kxzWo1mr6FSaYXjQELRe88y2KdfynXdnK72rdjtA= github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0= github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= diff --git a/pkg/config/config.go b/pkg/config/config.go index 8cd83e6c..466534c8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -186,7 +186,6 @@ func printCfg(cfg *Configuration) { } func findConfigFile(fileName string) (string, error) { - var cfgFile string if dir, ok := os.LookupEnv("CT_CONFIG_DIR"); ok { return filepath.Join(dir, fileName), nil } @@ -194,7 +193,7 @@ func findConfigFile(fileName string) (string, error) { for _, location := range configSearchLocations { filePath := filepath.Join(location, fileName) if util.FileExists(filePath) { - return cfgFile, nil + return filePath, nil } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index b5c47bba..b9b74de6 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -15,9 +15,12 @@ package config import ( + "os" + "path/filepath" "testing" "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -55,3 +58,56 @@ func loadAndAssertConfigFromFile(t *testing.T, configFile string) { require.Equal(t, "release", cfg.ReleaseLabel) require.Equal(t, true, cfg.ExcludeDeprecated) } + +func Test_findConfigFile(t *testing.T) { + tests := []struct { + name string + envVar string + defaultDir string + want string + wantErr bool + }{ + { + name: "without env var", + defaultDir: filepath.Join("testdata", "default"), + want: filepath.Join("testdata", "default", "test.yaml"), + }, + { + name: "with env var", + envVar: filepath.Join("testdata", "env"), + want: filepath.Join("testdata", "env", "test.yaml"), + }, + { + name: "with env var and default location", + envVar: filepath.Join("testdata", "env"), + defaultDir: filepath.Join("testdata", "default"), + want: filepath.Join("testdata", "env", "test.yaml"), + }, + { + name: "not found", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.envVar != "" { + err := os.Setenv("CT_CONFIG_DIR", tt.envVar) + require.NoError(t, err) + + t.Cleanup(func() { + err := os.Unsetenv("CT_CONFIG_DIR") + require.NoError(t, err) + }) + } + configSearchLocations = []string{tt.defaultDir} + + got, err := findConfigFile("test.yaml") + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, got) + } + }) + } +} diff --git a/pkg/config/testdata/default/test.yaml b/pkg/config/testdata/default/test.yaml new file mode 100644 index 00000000..e69de29b diff --git a/pkg/config/testdata/env/test.yaml b/pkg/config/testdata/env/test.yaml new file mode 100644 index 00000000..e69de29b