Skip to content

Commit

Permalink
Check HOMEDRIVE and HOMEPATH for windows, expand vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Piña committed Aug 26, 2019
1 parent 289984c commit 20f7133
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
26 changes: 20 additions & 6 deletions nats.go
Expand Up @@ -4018,7 +4018,7 @@ func wipeSlice(buf []byte) {
}

func userFromFile(userFile string) (string, error) {
path, err := expandTildePath(userFile)
path, err := expandPath(userFile)
if err != nil {
return _EMPTY_, fmt.Errorf("nats: %v", err)
}
Expand All @@ -4032,19 +4032,33 @@ func userFromFile(userFile string) (string, error) {
}

func homeDir() (string, error) {
envVar := "HOME"
if runtime.GOOS == "windows" {
envVar = "USERPROFILE"
homeDrive, homePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH")
userProfile := os.Getenv("USERPROFILE")

var home string
if homeDrive == "" || homePath == "" {
if userProfile == "" {
return _EMPTY_, errors.New("nats: failed to get home dir, require %HOMEDRIVE% and %HOMEPATH% or %USERPROFILE%")
}
home = userProfile
} else {
home = filepath.Join(homeDrive, homePath)
}

return home, nil
}

home := os.Getenv(envVar)
home := os.Getenv("HOME")
if home == "" {
return _EMPTY_, fmt.Errorf("nats: failed to get home dir, require %s environment variable", envVar)
return _EMPTY_, errors.New("nats: failed to get home dir, require $HOME")
}
return home, nil
}

func expandTildePath(p string) (string, error) {
func expandPath(p string) (string, error) {
p = os.ExpandEnv(p)

if !strings.HasPrefix(p, "~") {
return p, nil
}
Expand Down
57 changes: 39 additions & 18 deletions nats_test.go
Expand Up @@ -98,7 +98,7 @@ func TestVersionMatchesTag(t *testing.T) {
}
}

func TestExpandTildePath(t *testing.T) {
func TestExpandPath(t *testing.T) {
t.Run("unix", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
Expand All @@ -109,23 +109,25 @@ func TestExpandTildePath(t *testing.T) {
defer os.Setenv("HOME", origHome)

cases := []struct {
path string
home string
path string
home string
testEnv string

wantPath string
wantErr bool
}{
{path: "/foo/bar", home: "/fizz/buzz", wantPath: "/foo/bar", wantErr: false},
{path: "foo/bar", home: "/fizz/buzz", wantPath: "foo/bar", wantErr: false},
{path: "~/fizz", home: "/foo/bar", wantPath: "/foo/bar/fizz", wantErr: false},
{path: "/foo/bar", home: "/fizz/buzz", wantPath: "/foo/bar"},
{path: "foo/bar", home: "/fizz/buzz", wantPath: "foo/bar"},
{path: "~/fizz", home: "/foo/bar", wantPath: "/foo/bar/fizz"},
{path: "$HOME/fizz", home: "/foo/bar", wantPath: "/foo/bar/fizz"},

// missing HOME env var
{path: "~/fizz", wantErr: true},
}
for i, c := range cases {
os.Setenv("HOME", c.home)

gotPath, err := expandTildePath(c.path)
gotPath, err := expandPath(c.path)
if !c.wantErr && err != nil {
t.Error("unexpected error")
t.Errorf("case=%d; got=%v; want=%v", i, err, nil)
Expand All @@ -149,26 +151,45 @@ func TestExpandTildePath(t *testing.T) {
}

origUserProfile := os.Getenv("USERPROFILE")
defer os.Setenv("USERPROFILE", origUserProfile)
origHomeDrive, origHomePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH")
defer func() {
os.Setenv("USERPROFILE", origUserProfile)
os.Setenv("HOMEDRIVE", origHomeDrive)
os.Setenv("HOMEPATH", origHomePath)
}()

cases := []struct {
path string
home string
path string
userProfile string
homeDrive string
homePath string

wantPath string
wantErr bool
}{
{path: "/Foo/Bar", home: `C:\Foo\Bar`, wantPath: "/Foo/Bar", wantErr: false},
{path: "Foo/Bar", home: `C:\Foo\Bar`, wantPath: "Foo/Bar", wantErr: false},
{path: "~/Fizz", home: `C:\Foo\Bar`, wantPath: `C:\Foo\Bar\Fizz`, wantErr: false},

// missing USERPROFILE env var
{path: "~/fizz", wantErr: true},
// Missing HOMEDRIVE and HOMEPATH.
{path: "/Foo/Bar", userProfile: `C:\Foo\Bar`, wantPath: "/Foo/Bar"},
{path: "Foo/Bar", userProfile: `C:\Foo\Bar`, wantPath: "Foo/Bar"},
{path: "~/Fizz", userProfile: `C:\Foo\Bar`, wantPath: `C:\Foo\Bar\Fizz`},
{path: `%HOMEDRIVE%%HOMEPATH%\Fizz`, userProfile: `C:\Foo\Bar`, wantPath: `C:\Foo\Bar\Fizz`},

// Missing USERPROFILE.
{path: "~/Fizz", homeDrive: "X:", homePath: `\Foo\Bar`, wantPath: `X:\Foo\Bar\Fizz`},

// Set all environment variables. HOMEDRIVE and HOMEPATH take
// precedence.
{path: "~/Fizz", userProfile: `C:\Foo\Bar`,
homeDrive: "X:", homePath: `\Foo\Bar`, wantPath: `X:\Foo\Bar\Fizz`},

// Missing all environment variables.
{path: "~/Fizz", wantErr: true},
}
for i, c := range cases {
os.Setenv("USERPROFILE", c.home)
os.Setenv("USERPROFILE", c.userProfile)
os.Setenv("HOMEDRIVE", c.homeDrive)
os.Setenv("HOMEPATH", c.homePath)

gotPath, err := expandTildePath(c.path)
gotPath, err := expandPath(c.path)
if !c.wantErr && err != nil {
t.Error("unexpected error")
t.Errorf("case=%d; got=%v; want=%v", i, err, nil)
Expand Down

0 comments on commit 20f7133

Please sign in to comment.