diff --git a/cointop/common/pathutil/pathutil.go b/cointop/common/pathutil/pathutil.go index def97a9..58bb4e3 100644 --- a/cointop/common/pathutil/pathutil.go +++ b/cointop/common/pathutil/pathutil.go @@ -8,27 +8,35 @@ import ( ) // UserPreferredHomeDir returns the preferred home directory for the user -func UserPreferredHomeDir() string { +func UserPreferredHomeDir() (string, bool) { var home string + var isConfigDir bool if runtime.GOOS == "windows" { home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + isConfigDir = false } else if runtime.GOOS == "linux" { home = os.Getenv("XDG_CONFIG_HOME") + isConfigDir = true } if home == "" { home, _ = os.UserHomeDir() + isConfigDir = false } - return home + return home, isConfigDir } // NormalizePath normalizes and extends the path string func NormalizePath(path string) string { // expand tilde if strings.HasPrefix(path, "~/") { - path = filepath.Join(UserPreferredHomeDir(), path[2:]) + home, isConfigDir := UserPreferredHomeDir() + if !isConfigDir { + path = filepath.Join(home, path[2:]) + } + path = filepath.Join(home, path[10:]) } path = strings.Replace(path, "/", string(filepath.Separator), -1) diff --git a/cointop/common/pathutil/pathutil_test.go b/cointop/common/pathutil/pathutil_test.go new file mode 100644 index 0000000..8f13463 --- /dev/null +++ b/cointop/common/pathutil/pathutil_test.go @@ -0,0 +1,22 @@ +package pathutil + +import ( + "os" + "path/filepath" + "testing" +) + +// TestNormalizePath checks that NormalizePath returns the correct directory +func TestNormalizePath(t *testing.T) { + cases := []struct { + in, want string + }{ + {"~/.config/cointop/config.toml", filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "/cointop/config.toml")}, + } + for _, c := range cases { + got := NormalizePath(c.in) + if got != c.want { + t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want) + } + } +}