diff options
| author | Daniel Theophanes <kardianos@gmail.com> | 2016-03-29 07:14:43 -0700 |
|---|---|---|
| committer | Andrew Gerrand <adg@golang.org> | 2016-05-31 03:30:11 +0000 |
| commit | 55c7d78c71a67acd97b52778826c5fe108bfc529 (patch) | |
| tree | a7e4165413e69e5f58b929310b1e6684e61593f3 | |
| parent | 510dcfe65574591e23e3e3e8488deee0642b2f2e (diff) | |
| download | go-x-review-55c7d78c71a67acd97b52778826c5fe108bfc529.tar.xz | |
review: look in correct location for netrc on Windows.
Change-Id: I38a58a2fb1bf6f4840cca8fe24aaa85ad3d6289e
Reviewed-on: https://go-review.googlesource.com/21252
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
| -rw-r--r-- | git-codereview/api.go | 28 | ||||
| -rw-r--r-- | git-codereview/api_test.go | 12 |
2 files changed, 35 insertions, 5 deletions
diff --git a/git-codereview/api.go b/git-codereview/api.go index 786ac60..910900f 100644 --- a/git-codereview/api.go +++ b/git-codereview/api.go @@ -12,7 +12,9 @@ import ( "io/ioutil" "net/http" "net/url" - "os" + "os/user" + "path/filepath" + "runtime" "sort" "strings" ) @@ -120,6 +122,19 @@ func loadGerritOriginInternal(origin, remoteOrigin string) error { return nil } +// testHomeDir is empty for normal use. During tests it may be set and used +// in place of the actual home directory. Tests may still need to +// set the HOME var for sub-processes such as git. +var testHomeDir = "" + +func netrcName() string { + // Git on Windows will look in $HOME\_netrc. + if runtime.GOOS == "windows" { + return "_netrc" + } + return ".netrc" +} + // loadAuth loads the authentication tokens for making API calls to // the Gerrit origin host. func loadAuth() { @@ -152,7 +167,16 @@ func loadAuth() { // If not there, then look in $HOME/.netrc, which is where Gerrit // used to tell users to store the information, until the passwords // got so long that old versions of curl couldn't handle them. - data, _ := ioutil.ReadFile(os.Getenv("HOME") + "/.netrc") + netrc := netrcName() + homeDir := testHomeDir + if homeDir == "" { + usr, err := user.Current() + if err != nil { + dief("failed to get current user home directory to look for %q: %v", netrc, err) + } + homeDir = usr.HomeDir + } + data, _ := ioutil.ReadFile(filepath.Join(homeDir, netrc)) for _, line := range lines(string(data)) { if i := strings.Index(line, "#"); i >= 0 { line = line[:i] diff --git a/git-codereview/api_test.go b/git-codereview/api_test.go index 279a76f..41c599f 100644 --- a/git-codereview/api_test.go +++ b/git-codereview/api_test.go @@ -6,6 +6,7 @@ package main import ( "os" + "path/filepath" "testing" ) @@ -75,6 +76,12 @@ func TestLoadAuth(t *testing.T) { defer os.Setenv("HOME", os.Getenv("HOME")) os.Setenv("HOME", gt.client) + + testHomeDir = gt.client + netrc := filepath.Join(gt.client, netrcName()) + defer func() { + testHomeDir = "" + }() trun(t, gt.client, "git", "config", "remote.origin.url", "https://go.googlesource.com/go") for i, tt := range authTests { @@ -86,10 +93,10 @@ func TestLoadAuth(t *testing.T) { trun(t, gt.client, "git", "config", "http.cookiefile", "XXX") trun(t, gt.client, "git", "config", "--unset", "http.cookiefile") - remove(t, gt.client+"/.netrc") + remove(t, netrc) remove(t, gt.client+"/.cookies") if tt.netrc != "" { - write(t, gt.client+"/.netrc", tt.netrc) + write(t, netrc, tt.netrc) } if tt.cookiefile != "" { if tt.cookiefile != "MISSING" { @@ -99,7 +106,6 @@ func TestLoadAuth(t *testing.T) { } // Run command via testMain to trap stdout, stderr, death. - // mail -n will load auth info for us. if tt.died { testMainDied(t, "test-loadAuth") } else { |
