Skip to content

Commit

Permalink
cmd: fix JoinWithUserDir on Windows
Browse files Browse the repository at this point in the history
Using the proper environment variable, and also filepath instead of
path.

Tested on Windows 8.

Fix #1169.
  • Loading branch information
Francisco Souza committed Apr 24, 2015
1 parent ca9d7cb commit 2f41e49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ import (
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"launchpad.net/gnuflag"
)

func getHome() string {
envs := []string{"HOME", "HOMEPATH"}
var home string
for i := 0; i < len(envs) && home == ""; i++ {
home = os.Getenv(envs[i])
}
return home
}

func JoinWithUserDir(p ...string) string {
paths := []string{os.ExpandEnv("$HOME")}
paths := []string{getHome()}
paths = append(paths, p...)
return path.Join(paths...)
return filepath.Join(paths...)
}

func writeToken(token string) error {
Expand Down
16 changes: 16 additions & 0 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ package cmd

import (
"io/ioutil"
"os"
"path"

"github.com/tsuru/tsuru/fs/fstest"
"gopkg.in/check.v1"
"launchpad.net/gnuflag"
)

func (s *S) TestJoinWithUserDir(c *check.C) {
expected := path.Join(os.Getenv("HOME"), "a", "b")
path := JoinWithUserDir("a", "b")
c.Assert(path, check.Equals, expected)
}

func (s *S) TestJoinWithUserDirHomePath(c *check.C) {
defer os.Setenv("HOME", os.Getenv("HOME"))
os.Setenv("HOME", "")
os.Setenv("HOMEPATH", "/wat")
path := JoinWithUserDir("a", "b")
c.Assert(path, check.Equals, "/wat/a/b")
}

func (s *S) TestWriteToken(c *check.C) {
rfs := &fstest.RecordingFs{}
fsystem = rfs
Expand Down

1 comment on commit 2f41e49

@marceloboeira
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏, I was trying to test here, but didn't had much free time to study Go.

Please sign in to comment.