Merge pull request #6 from Matt3o12/master
Fixed a bug which would expand string incorrectly under *NIX.
diff --git a/homedir.go b/homedir.go
index b6d3502..051f111 100644
--- a/homedir.go
+++ b/homedir.go
@@ -5,6 +5,7 @@
"errors"
"os"
"os/exec"
+ "path/filepath"
"runtime"
"strings"
)
@@ -43,7 +44,7 @@
return "", err
}
- return dir + path[1:], nil
+ return filepath.Join(dir, path[1:]), nil
}
func dirUnix() (string, error) {
diff --git a/homedir_test.go b/homedir_test.go
index 89e74c3..ddc24ee 100644
--- a/homedir_test.go
+++ b/homedir_test.go
@@ -2,10 +2,21 @@
import (
"fmt"
+ "os"
"os/user"
"testing"
)
+func patchEnv(key, value string) func() {
+ bck := os.Getenv(key)
+ deferFunc := func() {
+ os.Setenv(key, bck)
+ }
+
+ os.Setenv(key, value)
+ return deferFunc
+}
+
func TestDir(t *testing.T) {
u, err := user.Current()
if err != nil {
@@ -44,7 +55,7 @@
fmt.Sprintf("%s/foo", u.HomeDir),
false,
},
-
+
{
"",
"",
@@ -74,4 +85,14 @@
t.Fatalf("Input: %#v\n\nOutput: %#v", tc.Input, actual)
}
}
+
+ defer patchEnv("HOME", "/custom/path/")()
+ expected := "/custom/path/foo/bar"
+ actual, err := Expand("~/foo/bar")
+
+ if err != nil {
+ t.Errorf("No error is expected, got: %v", err)
+ } else if actual != "/custom/path/foo/bar" {
+ t.Errorf("Expected: %v; actual: %v", expected, actual)
+ }
}