Permalink
Browse files

Fix login issues with no netrc file

Fixes #110.
  • Loading branch information...
1 parent e46ed2b commit db8dba22942c3fca784107617b9e56640892aab7 @bgentry bgentry committed Feb 25, 2014
Showing with 39 additions and 1 deletion.
  1. +1 −0 util.go
  2. +38 −1 util_test.go
View
1 util.go
@@ -47,6 +47,7 @@ func loadNetrc() {
var err error
if nrc, err = netrc.ParseFile(netrcPath()); err != nil {
if os.IsNotExist(err) {
+ nrc = &netrc.Netrc{}
return
}
printFatal("loading netrc: " + err.Error())
View
39 util_test.go
@@ -7,7 +7,8 @@ import (
"testing"
)
-func init() {
+func setupFakeNetrc() {
+ nrc = nil
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
@@ -18,7 +19,14 @@ func init() {
}
}
+func cleanupNetrc() {
+ nrc = nil
+ os.Setenv("NETRC_PATH", "")
+}
+
func TestGetCreds(t *testing.T) {
+ setupFakeNetrc()
+
u, p := getCreds("https://omg:wtf@api.heroku.com")
if u != "omg" {
t.Errorf("expected user=omg, got %s", u)
@@ -39,6 +47,8 @@ func TestGetCreds(t *testing.T) {
if u != "" || p != "" {
t.Errorf("expected empty user and pass, got u=%q p=%q", u, p)
}
+
+ cleanupNetrc()
}
func TestNetrcPath(t *testing.T) {
@@ -49,3 +59,30 @@ func TestNetrcPath(t *testing.T) {
}
os.Setenv("NETRC_PATH", "")
}
+
+func TestLoadNetrc(t *testing.T) {
+ setupFakeNetrc()
+
+ loadNetrc()
+ m := nrc.FindMachine("api.heroku.com")
+ if m == nil {
+ t.Errorf("machine api.heroku.com not found")
+ } else if m.Login != "user@test.com" {
+ t.Errorf("expected user=user@test.com, got %s", m.Login)
+ }
+
+ nrc = nil
+ fakepath := "/fake/net/rc"
+ os.Setenv("NETRC_PATH", fakepath)
+
+ loadNetrc()
+ if nrc == nil {
+ t.Fatalf("expected non-nil netrc")
+ }
+ m = nrc.FindMachine("api.heroku.com")
+ if m != nil {
+ t.Errorf("unexpected machine api.heroku.com found")
+ }
+
+ cleanupNetrc()
+}

0 comments on commit db8dba2

Please sign in to comment.