From 555019a6111fa34070d37974153a14850ff0799f Mon Sep 17 00:00:00 2001 From: NAGATA Hiroaki Date: Tue, 24 Dec 2024 17:28:11 +0900 Subject: [PATCH 1/2] test: cli.determineConfigPath --- cli/cli_test.go | 89 +++++++++++++++++++ .../cli_determine_config_path/home0/.gitkeep | 0 .../home1/.config/awsc/config.yaml | 0 .../home2/.awsc/config.yaml | 0 .../xdg_home/awsc/config.yaml | 0 5 files changed, 89 insertions(+) create mode 100644 cli/cli_test.go create mode 100644 cli/testdata/cli_determine_config_path/home0/.gitkeep create mode 100644 cli/testdata/cli_determine_config_path/home1/.config/awsc/config.yaml create mode 100644 cli/testdata/cli_determine_config_path/home2/.awsc/config.yaml create mode 100644 cli/testdata/cli_determine_config_path/xdg_home/awsc/config.yaml 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 From 200cd6c476dd7ebcb0715ef403663dfb1450be1b Mon Sep 17 00:00:00 2001 From: NAGATA Hiroaki Date: Tue, 24 Dec 2024 17:43:40 +0900 Subject: [PATCH 2/2] fix: XDG_CONFIG_HOME --- internal/env/env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" )