This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
util_test.go
88 lines (75 loc) · 1.72 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"log"
"os"
"path/filepath"
"testing"
)
func setupFakeNetrc() {
nrc = nil
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
err = os.Setenv("NETRC_PATH", filepath.Join(wd, "fakenetrc"))
if err != nil {
log.Fatal(err)
}
}
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)
}
if p != "wtf" {
t.Errorf("expected password=wtf, got %s", p)
}
u, p = getCreds("https://api.heroku.com")
if u != "user@test.com" {
t.Errorf("expected user=user@test.com, got %s", u)
}
if p != "faketestpassword" {
t.Errorf("expected password=faketestpassword, got %s", p)
}
// test with a nil machine
u, p = getCreds("https://someotherapi.heroku.com")
if u != "" || p != "" {
t.Errorf("expected empty user and pass, got u=%q p=%q", u, p)
}
cleanupNetrc()
}
func TestNetrcPath(t *testing.T) {
fakepath := "/fake/net/rc"
os.Setenv("NETRC_PATH", fakepath)
if p := netrcPath(); p != fakepath {
t.Errorf("NETRC_PATH override expected %q, got %q", fakepath, p)
}
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()
}