From b47b4477483de2ce1bbd43a83a7b28d1551ee030 Mon Sep 17 00:00:00 2001 From: Jacob Biehler Date: Mon, 10 Aug 2020 16:25:49 -0700 Subject: [PATCH 1/3] Add boolean to UserPrefferedHomeDir to help correctly build configuration file placement and account for users with XDG_CONFIG_HOME defined --- cointop/common/pathutil/pathutil.go | 14 +++++++++++--- cointop/common/pathutil/pathutil_test.go | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 cointop/common/pathutil/pathutil_test.go 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) + } + } +} From b7ede5a57bae3e832eba9c9c2b7726a5b4ab26b6 Mon Sep 17 00:00:00 2001 From: Jacob Biehler Date: Mon, 10 Aug 2020 16:52:49 -0700 Subject: [PATCH 2/3] Remove runtime package and OS checks in favor UserConfigDir and UserHomeDir which are platform agnostic and add more robust tests --- cointop/common/pathutil/pathutil.go | 11 ++--------- cointop/common/pathutil/pathutil_test.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cointop/common/pathutil/pathutil.go b/cointop/common/pathutil/pathutil.go index 58bb4e3..6761b28 100644 --- a/cointop/common/pathutil/pathutil.go +++ b/cointop/common/pathutil/pathutil.go @@ -3,22 +3,15 @@ package pathutil import ( "os" "path/filepath" - "runtime" "strings" ) // UserPreferredHomeDir returns the preferred home directory for the user 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 - } + home, _ := os.UserConfigDir() + isConfigDir = true if home == "" { home, _ = os.UserHomeDir() diff --git a/cointop/common/pathutil/pathutil_test.go b/cointop/common/pathutil/pathutil_test.go index 8f13463..954ac0b 100644 --- a/cointop/common/pathutil/pathutil_test.go +++ b/cointop/common/pathutil/pathutil_test.go @@ -8,13 +8,22 @@ import ( // TestNormalizePath checks that NormalizePath returns the correct directory func TestNormalizePath(t *testing.T) { + home, _ := os.UserHomeDir() + configDir, _ := os.UserConfigDir() cases := []struct { in, want string }{ - {"~/.config/cointop/config.toml", filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(configDir, "/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(configDir, "/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")}, } - for _, c := range cases { + for i, c := range cases { got := NormalizePath(c.in) + if i > 1 { + home = "" + configDir = "" + } if got != c.want { t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want) } From f56d0a61b1dd395244ef7faacea5234230257579 Mon Sep 17 00:00:00 2001 From: Jacob Biehler Date: Tue, 11 Aug 2020 09:48:49 -0700 Subject: [PATCH 3/3] Removing artifacts from tests to check what would happen if HOME and XDG_CONFIG_HOME are undefined --- cointop/common/pathutil/pathutil_test.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cointop/common/pathutil/pathutil_test.go b/cointop/common/pathutil/pathutil_test.go index 954ac0b..3c5146e 100644 --- a/cointop/common/pathutil/pathutil_test.go +++ b/cointop/common/pathutil/pathutil_test.go @@ -15,15 +15,9 @@ func TestNormalizePath(t *testing.T) { }{ {"~/.config/cointop/config.toml", filepath.Join(configDir, "/cointop/config.toml")}, {"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")}, - {"~/.config/cointop/config.toml", filepath.Join(configDir, "/cointop/config.toml")}, - {"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")}, } - for i, c := range cases { + for _, c := range cases { got := NormalizePath(c.in) - if i > 1 { - home = "" - configDir = "" - } if got != c.want { t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want) }