generated from handlename/my-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from handlename/fix/default-config-dir
Use XDG_CONFIG_HOME
- Loading branch information
Showing
6 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters