Merge pull request #2 from bredov/pull-request
Fix Expand for path of len <= 1.
diff --git a/homedir.go b/homedir.go
index 2f0c5d8..3621a70 100644
--- a/homedir.go
+++ b/homedir.go
@@ -26,11 +26,11 @@
// is prefixed with `~`. If it isn't prefixed with `~`, the path is
// returned as-is.
func Expand(path string) (string, error) {
- if path[0] != '~' {
+ if len(path) == 0 || path[0] != '~' {
return path, nil
}
- if path[1] != '/' && path[1] != '\\' {
+ if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
return "", errors.New("cannot expand user-specific home dir")
}
diff --git a/homedir_test.go b/homedir_test.go
index 1313370..89e74c3 100644
--- a/homedir_test.go
+++ b/homedir_test.go
@@ -44,6 +44,18 @@
fmt.Sprintf("%s/foo", u.HomeDir),
false,
},
+
+ {
+ "",
+ "",
+ false,
+ },
+
+ {
+ "~",
+ u.HomeDir,
+ false,
+ },
{
"~foo/foo",