X Tutup
Skip to content

Commit ce15142

Browse files
committed
Migrate legacy tests
1 parent 5e97775 commit ce15142

File tree

19 files changed

+700
-600
lines changed

19 files changed

+700
-600
lines changed

api/client_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func TestGraphQL(t *testing.T) {
3232
}
3333
}{}
3434

35-
http.StubResponse(200, bytes.NewBufferString(`{"data":{"viewer":{"login":"hubot"}}}`))
35+
http.Register(
36+
httpmock.GraphQL("QUERY"),
37+
httpmock.StringResponse(`{"data":{"viewer":{"login":"hubot"}}}`),
38+
)
39+
3640
err := client.GraphQL("github.com", "QUERY", vars, &response)
3741
eq(t, err, nil)
3842
eq(t, response.Viewer.Login, "hubot")
@@ -48,12 +52,17 @@ func TestGraphQLError(t *testing.T) {
4852
client := NewClient(ReplaceTripper(http))
4953

5054
response := struct{}{}
51-
http.StubResponse(200, bytes.NewBufferString(`
52-
{ "errors": [
53-
{"message":"OH NO"},
54-
{"message":"this is fine"}
55-
]
56-
}`))
55+
56+
http.Register(
57+
httpmock.GraphQL(""),
58+
httpmock.StringResponse(`
59+
{ "errors": [
60+
{"message":"OH NO"},
61+
{"message":"this is fine"}
62+
]
63+
}
64+
`),
65+
)
5766

5867
err := client.GraphQL("github.com", "", nil, &response)
5968
if err == nil || err.Error() != "GraphQL error: OH NO\nthis is fine" {
@@ -68,7 +77,10 @@ func TestRESTGetDelete(t *testing.T) {
6877
ReplaceTripper(http),
6978
)
7079

71-
http.StubResponse(204, bytes.NewBuffer([]byte{}))
80+
http.Register(
81+
httpmock.REST("DELETE", "applications/CLIENTID/grant"),
82+
httpmock.StatusStringResponse(204, "{}"),
83+
)
7284

7385
r := bytes.NewReader([]byte(`{}`))
7486
err := client.REST("github.com", "DELETE", "applications/CLIENTID/grant", r, nil)

api/queries_issue_test.go

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"io/ioutil"
76
"testing"
@@ -16,30 +15,36 @@ func TestIssueList(t *testing.T) {
1615
http := &httpmock.Registry{}
1716
client := NewClient(ReplaceTripper(http))
1817

19-
http.StubResponse(200, bytes.NewBufferString(`
20-
{ "data": { "repository": {
21-
"hasIssuesEnabled": true,
22-
"issues": {
23-
"nodes": [],
24-
"pageInfo": {
25-
"hasNextPage": true,
26-
"endCursor": "ENDCURSOR"
27-
}
28-
}
29-
} } }
30-
`))
31-
http.StubResponse(200, bytes.NewBufferString(`
32-
{ "data": { "repository": {
33-
"hasIssuesEnabled": true,
34-
"issues": {
35-
"nodes": [],
36-
"pageInfo": {
37-
"hasNextPage": false,
38-
"endCursor": "ENDCURSOR"
39-
}
40-
}
41-
} } }
42-
`))
18+
http.Register(
19+
httpmock.GraphQL(`query IssueList\b`),
20+
httpmock.StringResponse(`
21+
{ "data": { "repository": {
22+
"hasIssuesEnabled": true,
23+
"issues": {
24+
"nodes": [],
25+
"pageInfo": {
26+
"hasNextPage": true,
27+
"endCursor": "ENDCURSOR"
28+
}
29+
}
30+
} } }
31+
`),
32+
)
33+
http.Register(
34+
httpmock.GraphQL(`query IssueList\b`),
35+
httpmock.StringResponse(`
36+
{ "data": { "repository": {
37+
"hasIssuesEnabled": true,
38+
"issues": {
39+
"nodes": [],
40+
"pageInfo": {
41+
"hasNextPage": false,
42+
"endCursor": "ENDCURSOR"
43+
}
44+
}
45+
} } }
46+
`),
47+
)
4348

4449
repo, _ := ghrepo.FromFullName("OWNER/REPO")
4550
_, err := IssueList(client, repo, "open", []string{}, "", 251, "", "", "")
@@ -75,44 +80,51 @@ func TestIssueList_pagination(t *testing.T) {
7580
http := &httpmock.Registry{}
7681
client := NewClient(ReplaceTripper(http))
7782

78-
http.StubResponse(200, bytes.NewBufferString(`
79-
{ "data": { "repository": {
80-
"hasIssuesEnabled": true,
81-
"issues": {
82-
"nodes": [
83-
{
84-
"title": "issue1",
85-
"labels": { "nodes": [ { "name": "bug" } ], "totalCount": 1 },
86-
"assignees": { "nodes": [ { "login": "user1" } ], "totalCount": 1 }
83+
http.Register(
84+
httpmock.GraphQL(`query IssueList\b`),
85+
httpmock.StringResponse(`
86+
{ "data": { "repository": {
87+
"hasIssuesEnabled": true,
88+
"issues": {
89+
"nodes": [
90+
{
91+
"title": "issue1",
92+
"labels": { "nodes": [ { "name": "bug" } ], "totalCount": 1 },
93+
"assignees": { "nodes": [ { "login": "user1" } ], "totalCount": 1 }
94+
}
95+
],
96+
"pageInfo": {
97+
"hasNextPage": true,
98+
"endCursor": "ENDCURSOR"
99+
},
100+
"totalCount": 2
87101
}
88-
],
89-
"pageInfo": {
90-
"hasNextPage": true,
91-
"endCursor": "ENDCURSOR"
92-
},
93-
"totalCount": 2
94-
}
95-
} } }
96-
`))
97-
http.StubResponse(200, bytes.NewBufferString(`
98-
{ "data": { "repository": {
99-
"hasIssuesEnabled": true,
100-
"issues": {
101-
"nodes": [
102-
{
103-
"title": "issue2",
104-
"labels": { "nodes": [ { "name": "enhancement" } ], "totalCount": 1 },
105-
"assignees": { "nodes": [ { "login": "user2" } ], "totalCount": 1 }
102+
} } }
103+
`),
104+
)
105+
106+
http.Register(
107+
httpmock.GraphQL(`query IssueList\b`),
108+
httpmock.StringResponse(`
109+
{ "data": { "repository": {
110+
"hasIssuesEnabled": true,
111+
"issues": {
112+
"nodes": [
113+
{
114+
"title": "issue2",
115+
"labels": { "nodes": [ { "name": "enhancement" } ], "totalCount": 1 },
116+
"assignees": { "nodes": [ { "login": "user2" } ], "totalCount": 1 }
117+
}
118+
],
119+
"pageInfo": {
120+
"hasNextPage": false,
121+
"endCursor": "ENDCURSOR"
122+
},
123+
"totalCount": 2
106124
}
107-
],
108-
"pageInfo": {
109-
"hasNextPage": false,
110-
"endCursor": "ENDCURSOR"
111-
},
112-
"totalCount": 2
113-
}
114-
} } }
115-
`))
125+
} } }
126+
`),
127+
)
116128

117129
repo := ghrepo.New("OWNER", "REPO")
118130
res, err := IssueList(client, repo, "", nil, "", 0, "", "", "")

internal/update/update_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package update
22

33
import (
4-
"bytes"
54
"fmt"
65
"io/ioutil"
76
"log"
@@ -54,10 +53,14 @@ func TestCheckForUpdate(t *testing.T) {
5453
t.Run(s.Name, func(t *testing.T) {
5554
http := &httpmock.Registry{}
5655
client := api.NewClient(api.ReplaceTripper(http))
57-
http.StubResponse(200, bytes.NewBufferString(fmt.Sprintf(`{
58-
"tag_name": "%s",
59-
"html_url": "%s"
60-
}`, s.LatestVersion, s.LatestURL)))
56+
57+
http.Register(
58+
httpmock.REST("GET", "repos/OWNER/REPO/releases/latest"),
59+
httpmock.StringResponse(fmt.Sprintf(`{
60+
"tag_name": "%s",
61+
"html_url": "%s"
62+
}`, s.LatestVersion, s.LatestURL)),
63+
)
6164

6265
rel, err := CheckForUpdate(client, tempFilePath(), "OWNER/REPO", s.CurrentVersion)
6366
if err != nil {

pkg/cmd/gist/delete/delete_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package delete
22

33
import (
44
"bytes"
5+
"net/http"
6+
"testing"
7+
58
"github.com/cli/cli/pkg/cmd/gist/shared"
69
"github.com/cli/cli/pkg/cmdutil"
710
"github.com/cli/cli/pkg/httpmock"
811
"github.com/cli/cli/pkg/iostreams"
912
"github.com/cli/cli/pkg/prompt"
1013
"github.com/google/shlex"
1114
"github.com/stretchr/testify/assert"
12-
"net/http"
13-
"testing"
1415
)
1516

1617
func TestNewCmdDelete(t *testing.T) {
@@ -98,7 +99,7 @@ func Test_deleteRun(t *testing.T) {
9899
},
99100
httpStubs: func(reg *httpmock.Registry) {
100101
reg.Register(httpmock.REST("DELETE", "gists/1234"),
101-
httpmock.StatusStringResponse(200, "{}"))
102+
httpmock.StringResponse("{}"))
102103
},
103104
wantErr: false,
104105
},

pkg/cmd/issue/close/close_test.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/cli/cli/pkg/iostreams"
1515
"github.com/cli/cli/test"
1616
"github.com/google/shlex"
17+
"github.com/stretchr/testify/assert"
1718
)
1819

1920
func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
@@ -58,14 +59,21 @@ func TestIssueClose(t *testing.T) {
5859
http := &httpmock.Registry{}
5960
defer http.Verify(t)
6061

61-
http.StubResponse(200, bytes.NewBufferString(`
62-
{ "data": { "repository": {
63-
"hasIssuesEnabled": true,
64-
"issue": { "number": 13, "title": "The title of the issue"}
65-
} } }
66-
`))
67-
68-
http.StubResponse(200, bytes.NewBufferString(`{"id": "THE-ID"}`))
62+
http.Register(
63+
httpmock.GraphQL(`query IssueByNumber\b`),
64+
httpmock.StringResponse(`
65+
{ "data": { "repository": {
66+
"hasIssuesEnabled": true,
67+
"issue": { "id": "THE-ID", "number": 13, "title": "The title of the issue"}
68+
} } }`),
69+
)
70+
http.Register(
71+
httpmock.GraphQL(`mutation IssueClose\b`),
72+
httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
73+
func(inputs map[string]interface{}) {
74+
assert.Equal(t, inputs["issueId"], "THE-ID")
75+
}),
76+
)
6977

7078
output, err := runCommand(http, true, "13")
7179
if err != nil {
@@ -83,12 +91,14 @@ func TestIssueClose_alreadyClosed(t *testing.T) {
8391
http := &httpmock.Registry{}
8492
defer http.Verify(t)
8593

86-
http.StubResponse(200, bytes.NewBufferString(`
87-
{ "data": { "repository": {
88-
"hasIssuesEnabled": true,
89-
"issue": { "number": 13, "title": "The title of the issue", "closed": true}
90-
} } }
91-
`))
94+
http.Register(
95+
httpmock.GraphQL(`query IssueByNumber\b`),
96+
httpmock.StringResponse(`
97+
{ "data": { "repository": {
98+
"hasIssuesEnabled": true,
99+
"issue": { "number": 13, "title": "The title of the issue", "closed": true}
100+
} } }`),
101+
)
92102

93103
output, err := runCommand(http, true, "13")
94104
if err != nil {
@@ -106,11 +116,13 @@ func TestIssueClose_issuesDisabled(t *testing.T) {
106116
http := &httpmock.Registry{}
107117
defer http.Verify(t)
108118

109-
http.StubResponse(200, bytes.NewBufferString(`
110-
{ "data": { "repository": {
111-
"hasIssuesEnabled": false
112-
} } }
113-
`))
119+
http.Register(
120+
httpmock.GraphQL(`query IssueByNumber\b`),
121+
httpmock.StringResponse(`
122+
{ "data": { "repository": {
123+
"hasIssuesEnabled": false
124+
} } }`),
125+
)
114126

115127
_, err := runCommand(http, true, "13")
116128
if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" {

0 commit comments

Comments
 (0)
X Tutup