Skip to content

Commit 3afb1d0

Browse files
committed
Use Testify assertions in test
1 parent b0ae09e commit 3afb1d0

File tree

10 files changed

+187
-253
lines changed

10 files changed

+187
-253
lines changed

api/client_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@ import (
55
"errors"
66
"io/ioutil"
77
"net/http"
8-
"reflect"
98
"testing"
109

1110
"github.com/cli/cli/pkg/httpmock"
11+
"github.com/stretchr/testify/assert"
1212
)
1313

14-
func eq(t *testing.T, got interface{}, expected interface{}) {
15-
t.Helper()
16-
if !reflect.DeepEqual(got, expected) {
17-
t.Errorf("expected: %v, got: %v", expected, got)
18-
}
19-
}
20-
2114
func TestGraphQL(t *testing.T) {
2215
http := &httpmock.Registry{}
2316
client := NewClient(
@@ -38,13 +31,13 @@ func TestGraphQL(t *testing.T) {
3831
)
3932

4033
err := client.GraphQL("github.com", "QUERY", vars, &response)
41-
eq(t, err, nil)
42-
eq(t, response.Viewer.Login, "hubot")
34+
assert.Equal(t, err, nil)
35+
assert.Equal(t, response.Viewer.Login, "hubot")
4336

4437
req := http.Requests[0]
4538
reqBody, _ := ioutil.ReadAll(req.Body)
46-
eq(t, string(reqBody), `{"query":"QUERY","variables":{"name":"Mona"}}`)
47-
eq(t, req.Header.Get("Authorization"), "token OTOKEN")
39+
assert.Equal(t, string(reqBody), `{"query":"QUERY","variables":{"name":"Mona"}}`)
40+
assert.Equal(t, req.Header.Get("Authorization"), "token OTOKEN")
4841
}
4942

5043
func TestGraphQLError(t *testing.T) {
@@ -84,7 +77,7 @@ func TestRESTGetDelete(t *testing.T) {
8477

8578
r := bytes.NewReader([]byte(`{}`))
8679
err := client.REST("github.com", "DELETE", "applications/CLIENTID/grant", r, nil)
87-
eq(t, err, nil)
80+
assert.Equal(t, err, nil)
8881
}
8982

9083
func TestRESTError(t *testing.T) {

api/pull_request_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package api
33
import (
44
"encoding/json"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestPullRequest_ChecksStatus(t *testing.T) {
@@ -31,11 +33,11 @@ func TestPullRequest_ChecksStatus(t *testing.T) {
3133
} }] } }
3234
`
3335
err := json.Unmarshal([]byte(payload), &pr)
34-
eq(t, err, nil)
36+
assert.Equal(t, err, nil)
3537

3638
checks := pr.ChecksStatus()
37-
eq(t, checks.Total, 8)
38-
eq(t, checks.Pending, 3)
39-
eq(t, checks.Failing, 3)
40-
eq(t, checks.Passing, 2)
39+
assert.Equal(t, checks.Total, 8)
40+
assert.Equal(t, checks.Pending, 3)
41+
assert.Equal(t, checks.Failing, 3)
42+
assert.Equal(t, checks.Passing, 2)
4143
}

context/remote_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ package context
33
import (
44
"errors"
55
"net/url"
6-
"reflect"
76
"testing"
87

98
"github.com/cli/cli/git"
109
"github.com/cli/cli/internal/ghrepo"
10+
"github.com/stretchr/testify/assert"
1111
)
1212

13-
func eq(t *testing.T, got interface{}, expected interface{}) {
14-
t.Helper()
15-
if !reflect.DeepEqual(got, expected) {
16-
t.Errorf("expected: %v, got: %v", expected, got)
17-
}
18-
}
19-
2013
func Test_Remotes_FindByName(t *testing.T) {
2114
list := Remotes{
2215
&Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.New("monalisa", "myfork")},
@@ -25,15 +18,15 @@ func Test_Remotes_FindByName(t *testing.T) {
2518
}
2619

2720
r, err := list.FindByName("upstream", "origin")
28-
eq(t, err, nil)
29-
eq(t, r.Name, "upstream")
21+
assert.Equal(t, err, nil)
22+
assert.Equal(t, r.Name, "upstream")
3023

3124
r, err = list.FindByName("nonexistent", "*")
32-
eq(t, err, nil)
33-
eq(t, r.Name, "mona")
25+
assert.Equal(t, err, nil)
26+
assert.Equal(t, r.Name, "mona")
3427

3528
_, err = list.FindByName("nonexistent")
36-
eq(t, err, errors.New(`no GitHub remotes found`))
29+
assert.Equal(t, err, errors.New(`no GitHub remotes found`))
3730
}
3831

3932
func Test_translateRemotes(t *testing.T) {

git/remote_test.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package git
22

33
import (
4-
"reflect"
54
"testing"
6-
)
75

8-
// TODO: extract assertion helpers into a shared package
9-
func eq(t *testing.T, got interface{}, expected interface{}) {
10-
t.Helper()
11-
if !reflect.DeepEqual(got, expected) {
12-
t.Errorf("expected: %v, got: %v", expected, got)
13-
}
14-
}
6+
"github.com/stretchr/testify/assert"
7+
)
158

169
func Test_parseRemotes(t *testing.T) {
1710
remoteList := []string{
@@ -23,20 +16,20 @@ func Test_parseRemotes(t *testing.T) {
2316
"zardoz\thttps://example.com/zed.git (push)",
2417
}
2518
r := parseRemotes(remoteList)
26-
eq(t, len(r), 4)
19+
assert.Equal(t, len(r), 4)
2720

28-
eq(t, r[0].Name, "mona")
29-
eq(t, r[0].FetchURL.String(), "ssh://git@github.com/monalisa/myfork.git")
21+
assert.Equal(t, r[0].Name, "mona")
22+
assert.Equal(t, r[0].FetchURL.String(), "ssh://git@github.com/monalisa/myfork.git")
3023
if r[0].PushURL != nil {
3124
t.Errorf("expected no PushURL, got %q", r[0].PushURL)
3225
}
33-
eq(t, r[1].Name, "origin")
34-
eq(t, r[1].FetchURL.Path, "/monalisa/octo-cat.git")
35-
eq(t, r[1].PushURL.Path, "/monalisa/octo-cat-push.git")
26+
assert.Equal(t, r[1].Name, "origin")
27+
assert.Equal(t, r[1].FetchURL.Path, "/monalisa/octo-cat.git")
28+
assert.Equal(t, r[1].PushURL.Path, "/monalisa/octo-cat-push.git")
3629

37-
eq(t, r[2].Name, "upstream")
38-
eq(t, r[2].FetchURL.Host, "example.com")
39-
eq(t, r[2].PushURL.Host, "github.com")
30+
assert.Equal(t, r[2].Name, "upstream")
31+
assert.Equal(t, r[2].FetchURL.Host, "example.com")
32+
assert.Equal(t, r[2].PushURL.Host, "github.com")
4033

41-
eq(t, r[3].Name, "zardoz")
34+
assert.Equal(t, r[3].Name, "zardoz")
4235
}

internal/config/config_file_test.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@ package config
33
import (
44
"bytes"
55
"fmt"
6-
"reflect"
76
"testing"
87

98
"github.com/stretchr/testify/assert"
109
"gopkg.in/yaml.v3"
1110
)
1211

13-
func eq(t *testing.T, got interface{}, expected interface{}) {
14-
t.Helper()
15-
if !reflect.DeepEqual(got, expected) {
16-
t.Errorf("expected: %v, got: %v", expected, got)
17-
}
18-
}
19-
2012
func Test_parseConfig(t *testing.T) {
2113
defer StubConfig(`---
2214
hosts:
@@ -25,13 +17,13 @@ hosts:
2517
oauth_token: OTOKEN
2618
`, "")()
2719
config, err := ParseConfig("config.yml")
28-
eq(t, err, nil)
20+
assert.Equal(t, err, nil)
2921
user, err := config.Get("github.com", "user")
30-
eq(t, err, nil)
31-
eq(t, user, "monalisa")
22+
assert.Equal(t, err, nil)
23+
assert.Equal(t, user, "monalisa")
3224
token, err := config.Get("github.com", "oauth_token")
33-
eq(t, err, nil)
34-
eq(t, token, "OTOKEN")
25+
assert.Equal(t, err, nil)
26+
assert.Equal(t, token, "OTOKEN")
3527
}
3628

3729
func Test_parseConfig_multipleHosts(t *testing.T) {
@@ -45,13 +37,13 @@ hosts:
4537
oauth_token: OTOKEN
4638
`, "")()
4739
config, err := ParseConfig("config.yml")
48-
eq(t, err, nil)
40+
assert.Equal(t, err, nil)
4941
user, err := config.Get("github.com", "user")
50-
eq(t, err, nil)
51-
eq(t, user, "monalisa")
42+
assert.Equal(t, err, nil)
43+
assert.Equal(t, user, "monalisa")
5244
token, err := config.Get("github.com", "oauth_token")
53-
eq(t, err, nil)
54-
eq(t, token, "OTOKEN")
45+
assert.Equal(t, err, nil)
46+
assert.Equal(t, token, "OTOKEN")
5547
}
5648

5749
func Test_parseConfig_hostsFile(t *testing.T) {
@@ -61,13 +53,13 @@ github.com:
6153
oauth_token: OTOKEN
6254
`)()
6355
config, err := ParseConfig("config.yml")
64-
eq(t, err, nil)
56+
assert.Equal(t, err, nil)
6557
user, err := config.Get("github.com", "user")
66-
eq(t, err, nil)
67-
eq(t, user, "monalisa")
58+
assert.Equal(t, err, nil)
59+
assert.Equal(t, user, "monalisa")
6860
token, err := config.Get("github.com", "oauth_token")
69-
eq(t, err, nil)
70-
eq(t, token, "OTOKEN")
61+
assert.Equal(t, err, nil)
62+
assert.Equal(t, token, "OTOKEN")
7163
}
7264

7365
func Test_parseConfig_hostFallback(t *testing.T) {
@@ -83,16 +75,16 @@ example.com:
8375
git_protocol: https
8476
`)()
8577
config, err := ParseConfig("config.yml")
86-
eq(t, err, nil)
78+
assert.Equal(t, err, nil)
8779
val, err := config.Get("example.com", "git_protocol")
88-
eq(t, err, nil)
89-
eq(t, val, "https")
80+
assert.Equal(t, err, nil)
81+
assert.Equal(t, val, "https")
9082
val, err = config.Get("github.com", "git_protocol")
91-
eq(t, err, nil)
92-
eq(t, val, "ssh")
83+
assert.Equal(t, err, nil)
84+
assert.Equal(t, val, "ssh")
9385
val, err = config.Get("nonexistent.io", "git_protocol")
94-
eq(t, err, nil)
95-
eq(t, val, "ssh")
86+
assert.Equal(t, err, nil)
87+
assert.Equal(t, val, "ssh")
9688
}
9789

9890
func Test_ParseConfig_migrateConfig(t *testing.T) {

pkg/cmd/issue/create/create_test.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99
"os"
1010
"os/exec"
11-
"reflect"
1211
"strings"
1312
"testing"
1413

@@ -26,13 +25,6 @@ import (
2625
"github.com/stretchr/testify/assert"
2726
)
2827

29-
func eq(t *testing.T, got interface{}, expected interface{}) {
30-
t.Helper()
31-
if !reflect.DeepEqual(got, expected) {
32-
t.Errorf("expected: %v, got: %v", expected, got)
33-
}
34-
}
35-
3628
func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
3729
return runCommandWithRootDirOverridden(rt, isTTY, cli, "")
3830
}
@@ -120,7 +112,7 @@ func TestIssueCreate(t *testing.T) {
120112
t.Errorf("error running command `issue create`: %v", err)
121113
}
122114

123-
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
115+
assert.Equal(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
124116
}
125117

126118
func TestIssueCreate_recover(t *testing.T) {
@@ -152,9 +144,9 @@ func TestIssueCreate_recover(t *testing.T) {
152144
"URL": "https://github.com/OWNER/REPO/issues/12"
153145
} } } }
154146
`, func(inputs map[string]interface{}) {
155-
eq(t, inputs["title"], "recovered title")
156-
eq(t, inputs["body"], "recovered body")
157-
eq(t, inputs["labelIds"], []interface{}{"BUGID", "TODOID"})
147+
assert.Equal(t, inputs["title"], "recovered title")
148+
assert.Equal(t, inputs["body"], "recovered body")
149+
assert.Equal(t, inputs["labelIds"], []interface{}{"BUGID", "TODOID"})
158150
}))
159151

160152
as, teardown := prompt.InitAskStubber()
@@ -201,7 +193,7 @@ func TestIssueCreate_recover(t *testing.T) {
201193
t.Errorf("error running command `issue create`: %v", err)
202194
}
203195

204-
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
196+
assert.Equal(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
205197
}
206198

207199
func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
@@ -259,7 +251,7 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
259251
t.Errorf("error running command `issue create`: %v", err)
260252
}
261253

262-
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
254+
assert.Equal(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
263255
}
264256

265257
func TestIssueCreate_continueInBrowser(t *testing.T) {
@@ -376,12 +368,12 @@ func TestIssueCreate_metadata(t *testing.T) {
376368
"URL": "https://github.com/OWNER/REPO/issues/12"
377369
} } } }
378370
`, func(inputs map[string]interface{}) {
379-
eq(t, inputs["title"], "TITLE")
380-
eq(t, inputs["body"], "BODY")
381-
eq(t, inputs["assigneeIds"], []interface{}{"MONAID"})
382-
eq(t, inputs["labelIds"], []interface{}{"BUGID", "TODOID"})
383-
eq(t, inputs["projectIds"], []interface{}{"ROADMAPID"})
384-
eq(t, inputs["milestoneId"], "BIGONEID")
371+
assert.Equal(t, inputs["title"], "TITLE")
372+
assert.Equal(t, inputs["body"], "BODY")
373+
assert.Equal(t, inputs["assigneeIds"], []interface{}{"MONAID"})
374+
assert.Equal(t, inputs["labelIds"], []interface{}{"BUGID", "TODOID"})
375+
assert.Equal(t, inputs["projectIds"], []interface{}{"ROADMAPID"})
376+
assert.Equal(t, inputs["milestoneId"], "BIGONEID")
385377
if v, ok := inputs["userIds"]; ok {
386378
t.Errorf("did not expect userIds: %v", v)
387379
}
@@ -395,7 +387,7 @@ func TestIssueCreate_metadata(t *testing.T) {
395387
t.Errorf("error running command `issue create`: %v", err)
396388
}
397389

398-
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
390+
assert.Equal(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
399391
}
400392

401393
func TestIssueCreate_disabledIssues(t *testing.T) {
@@ -437,9 +429,9 @@ func TestIssueCreate_web(t *testing.T) {
437429
t.Fatal("expected a command to run")
438430
}
439431
url := seenCmd.Args[len(seenCmd.Args)-1]
440-
eq(t, url, "https://github.com/OWNER/REPO/issues/new")
441-
eq(t, output.String(), "")
442-
eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
432+
assert.Equal(t, url, "https://github.com/OWNER/REPO/issues/new")
433+
assert.Equal(t, output.String(), "")
434+
assert.Equal(t, output.Stderr(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
443435
}
444436

445437
func TestIssueCreate_webTitleBody(t *testing.T) {
@@ -462,7 +454,7 @@ func TestIssueCreate_webTitleBody(t *testing.T) {
462454
t.Fatal("expected a command to run")
463455
}
464456
url := strings.ReplaceAll(seenCmd.Args[len(seenCmd.Args)-1], "^", "")
465-
eq(t, url, "https://github.com/OWNER/REPO/issues/new?body=mybody&title=mytitle")
466-
eq(t, output.String(), "")
467-
eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
457+
assert.Equal(t, url, "https://github.com/OWNER/REPO/issues/new?body=mybody&title=mytitle")
458+
assert.Equal(t, output.String(), "")
459+
assert.Equal(t, output.Stderr(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
468460
}

0 commit comments

Comments
 (0)