Remove runtime package and OS checks in favor UserConfigDir and UserHomeDir which are platform agnostic and add more robust tests

pull/58/head
Jacob Biehler 4 years ago
parent b47b447748
commit b7ede5a57b

@ -3,22 +3,15 @@ package pathutil
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
) )
// UserPreferredHomeDir returns the preferred home directory for the user // UserPreferredHomeDir returns the preferred home directory for the user
func UserPreferredHomeDir() (string, bool) { func UserPreferredHomeDir() (string, bool) {
var home string
var isConfigDir bool var isConfigDir bool
if runtime.GOOS == "windows" { home, _ := os.UserConfigDir()
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") isConfigDir = true
isConfigDir = false
} else if runtime.GOOS == "linux" {
home = os.Getenv("XDG_CONFIG_HOME")
isConfigDir = true
}
if home == "" { if home == "" {
home, _ = os.UserHomeDir() home, _ = os.UserHomeDir()

@ -8,13 +8,22 @@ import (
// TestNormalizePath checks that NormalizePath returns the correct directory // TestNormalizePath checks that NormalizePath returns the correct directory
func TestNormalizePath(t *testing.T) { func TestNormalizePath(t *testing.T) {
home, _ := os.UserHomeDir()
configDir, _ := os.UserConfigDir()
cases := []struct { cases := []struct {
in, want string 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) got := NormalizePath(c.in)
if i > 1 {
home = ""
configDir = ""
}
if got != c.want { if got != c.want {
t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want) t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want)
} }

Loading…
Cancel
Save