Skip to content

Commit b5366c6

Browse files
authored
Merge pull request cli#2794 from cristiand391/use-testify-assertion
Use Testify assertions in tests
2 parents e566c61 + 75ebb86 commit b5366c6

File tree

23 files changed

+253
-363
lines changed

23 files changed

+253
-363
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.NoError(t, err)
35+
assert.Equal(t, "hubot", response.Viewer.Login)
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, `{"query":"QUERY","variables":{"name":"Mona"}}`, string(reqBody))
40+
assert.Equal(t, "token OTOKEN", req.Header.Get("Authorization"))
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.NoError(t, err)
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.NoError(t, err)
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, 8, checks.Total)
40+
assert.Equal(t, 3, checks.Pending)
41+
assert.Equal(t, 3, checks.Failing)
42+
assert.Equal(t, 2, checks.Passing)
4143
}

context/remote_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
package context
22

33
import (
4-
"errors"
54
"net/url"
6-
"reflect"
75
"testing"
86

97
"github.com/cli/cli/git"
108
"github.com/cli/cli/internal/ghrepo"
9+
"github.com/stretchr/testify/assert"
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_Remotes_FindByName(t *testing.T) {
2113
list := Remotes{
2214
&Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.New("monalisa", "myfork")},
@@ -25,15 +17,15 @@ func Test_Remotes_FindByName(t *testing.T) {
2517
}
2618

2719
r, err := list.FindByName("upstream", "origin")
28-
eq(t, err, nil)
29-
eq(t, r.Name, "upstream")
20+
assert.NoError(t, err)
21+
assert.Equal(t, "upstream", r.Name)
3022

3123
r, err = list.FindByName("nonexistent", "*")
32-
eq(t, err, nil)
33-
eq(t, r.Name, "mona")
24+
assert.NoError(t, err)
25+
assert.Equal(t, "mona", r.Name)
3426

3527
_, err = list.FindByName("nonexistent")
36-
eq(t, err, errors.New(`no GitHub remotes found`))
28+
assert.Error(t, err, "no GitHub remotes found")
3729
}
3830

3931
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, 4, len(r))
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, "mona", r[0].Name)
22+
assert.Equal(t, "ssh://git@github.com/monalisa/myfork.git", r[0].FetchURL.String())
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, "origin", r[1].Name)
27+
assert.Equal(t, "/monalisa/octo-cat.git", r[1].FetchURL.Path)
28+
assert.Equal(t, "/monalisa/octo-cat-push.git", r[1].PushURL.Path)
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, "upstream", r[2].Name)
31+
assert.Equal(t, "example.com", r[2].FetchURL.Host)
32+
assert.Equal(t, "github.com", r[2].PushURL.Host)
4033

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

internal/config/config_file_test.go

Lines changed: 23 additions & 31 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.NoError(t, err)
2921
user, err := config.Get("github.com", "user")
30-
eq(t, err, nil)
31-
eq(t, user, "monalisa")
22+
assert.NoError(t, err)
23+
assert.Equal(t, "monalisa", user)
3224
token, err := config.Get("github.com", "oauth_token")
33-
eq(t, err, nil)
34-
eq(t, token, "OTOKEN")
25+
assert.NoError(t, err)
26+
assert.Equal(t, "OTOKEN", token)
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.NoError(t, err)
4941
user, err := config.Get("github.com", "user")
50-
eq(t, err, nil)
51-
eq(t, user, "monalisa")
42+
assert.NoError(t, err)
43+
assert.Equal(t, "monalisa", user)
5244
token, err := config.Get("github.com", "oauth_token")
53-
eq(t, err, nil)
54-
eq(t, token, "OTOKEN")
45+
assert.NoError(t, err)
46+
assert.Equal(t, "OTOKEN", token)
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.NoError(t, err)
6557
user, err := config.Get("github.com", "user")
66-
eq(t, err, nil)
67-
eq(t, user, "monalisa")
58+
assert.NoError(t, err)
59+
assert.Equal(t, "monalisa", user)
6860
token, err := config.Get("github.com", "oauth_token")
69-
eq(t, err, nil)
70-
eq(t, token, "OTOKEN")
61+
assert.NoError(t, err)
62+
assert.Equal(t, "OTOKEN", token)
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.NoError(t, err)
8779
val, err := config.Get("example.com", "git_protocol")
88-
eq(t, err, nil)
89-
eq(t, val, "https")
80+
assert.NoError(t, err)
81+
assert.Equal(t, "https", val)
9082
val, err = config.Get("github.com", "git_protocol")
91-
eq(t, err, nil)
92-
eq(t, val, "ssh")
83+
assert.NoError(t, err)
84+
assert.Equal(t, "ssh", val)
9385
val, err = config.Get("nonexistent.io", "git_protocol")
94-
eq(t, err, nil)
95-
eq(t, val, "ssh")
86+
assert.NoError(t, err)
87+
assert.Equal(t, "ssh", val)
9688
}
9789

9890
func Test_ParseConfig_migrateConfig(t *testing.T) {
@@ -108,7 +100,7 @@ github.com:
108100
defer StubBackupConfig()()
109101

110102
_, err := ParseConfig("config.yml")
111-
assert.Nil(t, err)
103+
assert.NoError(t, err)
112104

113105
expectedHosts := `github.com:
114106
user: keiyuri

internal/config/config_type_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ func Test_defaultConfig(t *testing.T) {
5555
assert.Equal(t, "", hostsBuf.String())
5656

5757
proto, err := cfg.Get("", "git_protocol")
58-
assert.Nil(t, err)
58+
assert.NoError(t, err)
5959
assert.Equal(t, "https", proto)
6060

6161
editor, err := cfg.Get("", "editor")
62-
assert.Nil(t, err)
62+
assert.NoError(t, err)
6363
assert.Equal(t, "", editor)
6464

6565
aliases, err := cfg.Aliases()
66-
assert.Nil(t, err)
66+
assert.NoError(t, err)
6767
assert.Equal(t, len(aliases.All()), 1)
6868
expansion, _ := aliases.Get("co")
6969
assert.Equal(t, expansion, "pr checkout")
@@ -74,13 +74,13 @@ func Test_ValidateValue(t *testing.T) {
7474
assert.EqualError(t, err, "invalid value")
7575

7676
err = ValidateValue("git_protocol", "ssh")
77-
assert.Nil(t, err)
77+
assert.NoError(t, err)
7878

7979
err = ValidateValue("editor", "vim")
80-
assert.Nil(t, err)
80+
assert.NoError(t, err)
8181

8282
err = ValidateValue("got", "123")
83-
assert.Nil(t, err)
83+
assert.NoError(t, err)
8484
}
8585

8686
func Test_ValidateKey(t *testing.T) {

internal/config/from_env_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ func TestInheritEnv(t *testing.T) {
279279
assert.Equal(t, tt.wants.token, val)
280280

281281
err := cfg.CheckWriteable(tt.hostname, "oauth_token")
282-
assert.Equal(t, tt.wants.writeable, err == nil)
282+
if tt.wants.writeable != (err == nil) {
283+
t.Errorf("CheckWriteable() = %v, wants %v", err, tt.wants.writeable)
284+
}
283285
})
284286
}
285287
}

internal/ghinstance/host_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestHostnameValidator(t *testing.T) {
139139
assert.Error(t, err)
140140
return
141141
}
142-
assert.Equal(t, nil, err)
142+
assert.NoError(t, err)
143143
})
144144
}
145145
}

pkg/cmd/alias/delete/delete_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ func TestAliasDelete(t *testing.T) {
7676

7777
_, err = cmd.ExecuteC()
7878
if tt.wantErr != "" {
79-
if assert.Error(t, err) {
80-
assert.Equal(t, tt.wantErr, err.Error())
81-
}
79+
assert.EqualError(t, err, tt.wantErr)
8280
return
8381
}
8482
require.NoError(t, err)

pkg/cmd/alias/set/set_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ func TestAliasSet_gh_command(t *testing.T) {
6565
cfg := config.NewFromString(``)
6666

6767
_, err := runCommand(cfg, true, "pr 'pr status'")
68-
69-
if assert.Error(t, err) {
70-
assert.Equal(t, `could not create alias: "pr" is already a gh command`, err.Error())
71-
}
68+
assert.EqualError(t, err, `could not create alias: "pr" is already a gh command`)
7269
}
7370

7471
func TestAliasSet_empty_aliases(t *testing.T) {
@@ -210,9 +207,7 @@ func TestAliasSet_invalid_command(t *testing.T) {
210207
cfg := config.NewFromString(``)
211208

212209
_, err := runCommand(cfg, true, "co 'pe checkout'")
213-
if assert.Error(t, err) {
214-
assert.Equal(t, "could not create alias: pe checkout does not correspond to a gh command", err.Error())
215-
}
210+
assert.EqualError(t, err, "could not create alias: pe checkout does not correspond to a gh command")
216211
}
217212

218213
func TestShellAlias_flag(t *testing.T) {

0 commit comments

Comments
 (0)