Skip to content

Commit

Permalink
Remove runtime package and OS checks in favor UserConfigDir and UserH…
Browse files Browse the repository at this point in the history
…omeDir which are platform agnostic and add more robust tests
  • Loading branch information
Jacob Biehler committed Aug 10, 2020
1 parent b47b447 commit b7ede5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 2 additions & 9 deletions cointop/common/pathutil/pathutil.go
Expand Up @@ -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()
Expand Down
13 changes: 11 additions & 2 deletions cointop/common/pathutil/pathutil_test.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit b7ede5a

Please sign in to comment.