Skip to content

Commit

Permalink
Merge pull request #21 from handlename/fix/default-config-dir
Browse files Browse the repository at this point in the history
Use XDG_CONFIG_HOME
  • Loading branch information
handlename authored Dec 24, 2024
2 parents 25cbbf0 + 200cd6c commit dac3031
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
89 changes: 89 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit dac3031

Please sign in to comment.