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 (
"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()

@ -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)
}

Loading…
Cancel
Save