-
Notifications
You must be signed in to change notification settings - Fork 46
/
gh_test.go
195 lines (173 loc) · 5.12 KB
/
gh_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package gh
import (
"fmt"
"os"
"strings"
"testing"
"github.com/cli/go-gh/internal/config"
"github.com/cli/go-gh/pkg/api"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestHelperProcess(t *testing.T) {
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
return
}
if err := func(args []string) error {
if args[len(args)-1] == "error" {
return fmt.Errorf("process exited with error")
}
fmt.Fprintf(os.Stdout, "%v", args)
return nil
}(os.Args[3:]); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
func TestRun(t *testing.T) {
stdOut, stdErr, err := run(os.Args[0],
[]string{"GH_WANT_HELPER_PROCESS=1"},
"-test.run=TestHelperProcess", "--", "gh", "issue", "list")
assert.NoError(t, err)
assert.Equal(t, "[gh issue list]", stdOut.String())
assert.Equal(t, "", stdErr.String())
}
func TestRunError(t *testing.T) {
stdOut, stdErr, err := run(os.Args[0],
[]string{"GH_WANT_HELPER_PROCESS=1"},
"-test.run=TestHelperProcess", "--", "gh", "issue", "list", "error")
assert.EqualError(t, err, "failed to run gh: process exited with error. error: exit status 1")
assert.Equal(t, "", stdOut.String())
assert.Equal(t, "process exited with error", stdErr.String())
}
func TestRESTClient(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")
gock.New("https://api.github.com").
Get("/some/test/path").
MatchHeader("Authorization", "token GH_TOKEN").
Reply(200).
JSON(`{"message": "success"}`)
client, err := RESTClient(nil)
assert.NoError(t, err)
res := struct{ Message string }{}
err = client.Do("GET", "some/test/path", nil, &res)
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, "success", res.Message)
}
func TestGQLClient(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")
gock.New("https://api.github.com").
Post("/graphql").
MatchHeader("Authorization", "token GH_TOKEN").
BodyString(`{"query":"QUERY","variables":{"var":"test"}}`).
Reply(200).
JSON(`{"data":{"viewer":{"login":"hubot"}}}`)
client, err := GQLClient(nil)
assert.NoError(t, err)
vars := map[string]interface{}{"var": "test"}
res := struct{ Viewer struct{ Login string } }{}
err = client.Do("QUERY", vars, &res)
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, "hubot", res.Viewer.Login)
}
func TestHTTPClient(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")
gock.New("https://api.github.com").
Get("/some/test/path").
MatchHeader("Authorization", "token GH_TOKEN").
Reply(200).
JSON(`{"message": "success"}`)
client, err := HTTPClient(nil)
assert.NoError(t, err)
res, err := client.Get("https://api.github.com/some/test/path")
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, 200, res.StatusCode)
}
func TestResolveOptions(t *testing.T) {
cfg := testConfig()
tests := []struct {
name string
opts *api.ClientOptions
wantAuthToken string
wantHost string
wantSocket string
}{
{
name: "honors consumer provided ClientOptions",
opts: &api.ClientOptions{
Host: "test.com",
AuthToken: "token_from_opts",
UnixDomainSocket: "socket_from_opts",
},
wantAuthToken: "token_from_opts",
wantHost: "test.com",
wantSocket: "socket_from_opts",
},
{
name: "uses config values if there are no consumer provided ClientOptions",
opts: &api.ClientOptions{},
wantAuthToken: "token",
wantHost: "github.com",
wantSocket: "socket",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := resolveOptions(tt.opts, cfg)
assert.NoError(t, err)
assert.Equal(t, tt.wantHost, tt.opts.Host)
assert.Equal(t, tt.wantAuthToken, tt.opts.AuthToken)
assert.Equal(t, tt.wantSocket, tt.opts.UnixDomainSocket)
})
}
}
func testConfig() config.Config {
var data = `
hosts:
github.com:
user: user1
oauth_token: token
git_protocol: ssh
http_unix_socket: socket
`
cfg, _ := config.FromString(data)
return cfg
}
func printPendingMocks(mocks []gock.Mock) string {
paths := []string{}
for _, mock := range mocks {
paths = append(paths, mock.Request().URLStruct.String())
}
return fmt.Sprintf("%d unmatched mocks: %s", len(paths), strings.Join(paths, ", "))
}