diff --git a/cli/cli_test.go b/cli/cli_test.go new file mode 100644 index 0000000..2a965d0 --- /dev/null +++ b/cli/cli_test.go @@ -0,0 +1,89 @@ +package cli + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/handlename/awsc/internal/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_determineConfigPath(t *testing.T) { + homedir := os.Getenv("HOME") + defer os.Setenv("HOME", homedir) + + resetEnvs := func() error { + envs := []string{ + "HOME", + env.EnvConfigPath, + env.EnvDefaultConfigDir, + } + + for _, e := range envs { + if err := os.Unsetenv(e); err != nil { + return err + } + } + + return nil + } + + _, filename, _, ok := runtime.Caller(0) + require.True(t, ok) + testdataRoot := filepath.Join(filepath.Dir(filename), "testdata", "cli_determine_config_path") + + tests := []struct { + name string + env map[string]string + want string + wantErr bool + errBody string + }{ + { + name: "no envs", + env: map[string]string{ + "HOME": filepath.Join(testdataRoot, "home0"), + }, + want: "", + }, + { + name: fmt.Sprintf("set %s", env.EnvConfigPath), + env: map[string]string{ + env.EnvConfigPath: filepath.Join(testdataRoot, "specific", "config.toml"), + }, + want: filepath.Join(testdataRoot, "specific", "config.toml"), + }, + { + name: "exists ~/.config/awsc/config.yaml", + env: map[string]string{ + "HOME": filepath.Join(testdataRoot, "home1"), + }, + want: filepath.Join(testdataRoot, "home1", ".config", "awsc", "config.yaml"), + }, + { + name: "exists ~/.awsc/config.yaml", + env: map[string]string{ + "HOME": filepath.Join(testdataRoot, "home2"), + }, + want: filepath.Join(testdataRoot, "home2", ".awsc", "config.yaml"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.NoError(t, resetEnvs()) + + for k, v := range tt.env { + os.Setenv(k, v) + } + + path, err := determineConigPath() + require.NoError(t, err) + assert.Equal(t, tt.want, path) + }) + } +} diff --git a/cli/testdata/cli_determine_config_path/home0/.gitkeep b/cli/testdata/cli_determine_config_path/home0/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/cli/testdata/cli_determine_config_path/home1/.config/awsc/config.yaml b/cli/testdata/cli_determine_config_path/home1/.config/awsc/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/cli/testdata/cli_determine_config_path/home2/.awsc/config.yaml b/cli/testdata/cli_determine_config_path/home2/.awsc/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/cli/testdata/cli_determine_config_path/xdg_home/awsc/config.yaml b/cli/testdata/cli_determine_config_path/xdg_home/awsc/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/internal/env/env.go b/internal/env/env.go index be56cb0..8db0e8d 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -5,5 +5,5 @@ const ( EnvLogLevel = EnvPrefix + "_LOG_LEVEL" EnvConfigPath = EnvPrefix + "_CONFIG_PATH" EnvShowVersion = EnvPrefix + "_SHOW_VERSION" - EnvDefaultConfigDir = "XDG_CONFIG_PATH" + EnvDefaultConfigDir = "XDG_CONFIG_HOME" )