X Tutup
Skip to content

Commit d79eb49

Browse files
authored
Merge pull request cli#2279 from malaDev/1818-pr-checks-empty
add message on `pr checks` returns no CI with non-empty PR
2 parents 72ec5be + 6b953f2 commit d79eb49

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

pkg/cmd/pr/checks/checks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func checksRun(opts *ChecksOptions) error {
7676
}
7777

7878
if len(pr.Commits.Nodes) == 0 {
79-
return nil
79+
return fmt.Errorf("no commit found on the pull request")
8080
}
8181

8282
rollup := pr.Commits.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes
8383
if len(rollup) == 0 {
84-
return nil
84+
return fmt.Errorf("no checks reported on the '%s' branch", pr.BaseRefName)
8585
}
8686

8787
passing := 0

pkg/cmd/pr/checks/checks_test.go

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,107 +66,100 @@ func Test_checksRun(t *testing.T) {
6666
name string
6767
fixture string
6868
stubs func(*httpmock.Registry)
69-
wantOut string
7069
nontty bool
71-
wantErr bool
70+
wantOut string
71+
wantErr string
7272
}{
7373
{
7474
name: "no commits",
7575
stubs: func(reg *httpmock.Registry) {
7676
reg.Register(
7777
httpmock.GraphQL(`query PullRequestByNumber\b`),
78-
httpmock.JSONResponse(
79-
bytes.NewBufferString(`
80-
{ "data": { "repository": {
81-
"pullRequest": { "number": 123 }
82-
} } }
83-
`)))
78+
httpmock.StringResponse(`
79+
{ "data": { "repository": {
80+
"pullRequest": { "number": 123 }
81+
} } }
82+
`))
8483
},
84+
wantOut: "",
85+
wantErr: "no commit found on the pull request",
8586
},
8687
{
8788
name: "no checks",
8889
stubs: func(reg *httpmock.Registry) {
8990
reg.StubResponse(200, bytes.NewBufferString(`
9091
{ "data": { "repository": {
91-
"pullRequest": { "number": 123, "commits": { "nodes": [{"commit": {"oid": "abc"}}]} }
92+
"pullRequest": { "number": 123, "commits": { "nodes": [{"commit": {"oid": "abc"}}]}, "baseRefName": "master" }
9293
} } }
9394
`))
9495
},
96+
wantOut: "",
97+
wantErr: "no checks reported on the 'master' branch",
9598
},
9699
{
97100
name: "some failing",
98101
fixture: "./fixtures/someFailing.json",
99102
wantOut: "Some checks were not successful\n1 failing, 1 successful, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n- slow tests 1m26s sweet link\n",
100-
wantErr: true,
103+
wantErr: "SilentError",
101104
},
102105
{
103106
name: "some pending",
104107
fixture: "./fixtures/somePending.json",
105108
wantOut: "Some checks are still pending\n0 failing, 2 successful, and 1 pending checks\n\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n- slow tests 1m26s sweet link\n",
106-
wantErr: true,
109+
wantErr: "SilentError",
107110
},
108111
{
109112
name: "all passing",
110113
fixture: "./fixtures/allPassing.json",
111114
wantOut: "All checks were successful\n0 failing, 3 successful, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n",
115+
wantErr: "",
112116
},
113117
{
114118
name: "with statuses",
115119
fixture: "./fixtures/withStatuses.json",
116120
wantOut: "Some checks were not successful\n1 failing, 2 successful, and 0 pending checks\n\nX a status sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n",
117-
wantErr: true,
118-
},
119-
{
120-
name: "no commits",
121-
nontty: true,
122-
stubs: func(reg *httpmock.Registry) {
123-
reg.Register(
124-
httpmock.GraphQL(`query PullRequestByNumber\b`),
125-
httpmock.JSONResponse(
126-
bytes.NewBufferString(`
127-
{ "data": { "repository": {
128-
"pullRequest": { "number": 123 }
129-
} } }
130-
`)))
131-
},
121+
wantErr: "SilentError",
132122
},
133123
{
134124
name: "no checks",
135125
nontty: true,
136126
stubs: func(reg *httpmock.Registry) {
137127
reg.StubResponse(200, bytes.NewBufferString(`
138128
{ "data": { "repository": {
139-
"pullRequest": { "number": 123, "commits": { "nodes": [{"commit": {"oid": "abc"}}]} }
129+
"pullRequest": { "number": 123, "commits": { "nodes": [{"commit": {"oid": "abc"}}]}, "baseRefName": "master" }
140130
} } }
141131
`))
142132
},
133+
wantOut: "",
134+
wantErr: "no checks reported on the 'master' branch",
143135
},
144136
{
145137
name: "some failing",
146138
nontty: true,
147139
fixture: "./fixtures/someFailing.json",
148140
wantOut: "sad tests\tfail\t1m26s\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nslow tests\tpending\t1m26s\tsweet link\n",
149-
wantErr: true,
141+
wantErr: "SilentError",
150142
},
151143
{
152144
name: "some pending",
153145
nontty: true,
154146
fixture: "./fixtures/somePending.json",
155147
wantOut: "cool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\nslow tests\tpending\t1m26s\tsweet link\n",
156-
wantErr: true,
148+
wantErr: "SilentError",
157149
},
158150
{
159151
name: "all passing",
160152
nontty: true,
161153
fixture: "./fixtures/allPassing.json",
162154
wantOut: "awesome tests\tpass\t1m26s\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\n",
155+
wantErr: "",
163156
},
164157
{
165158
name: "with statuses",
166159
nontty: true,
167160
fixture: "./fixtures/withStatuses.json",
168161
wantOut: "a status\tfail\t0\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\n",
169-
wantErr: true,
162+
wantErr: "SilentError",
170163
},
171164
}
172165

@@ -184,28 +177,26 @@ func Test_checksRun(t *testing.T) {
184177
}
185178

186179
reg := &httpmock.Registry{}
180+
defer reg.Verify(t)
181+
187182
if tt.stubs != nil {
188183
tt.stubs(reg)
189184
} else if tt.fixture != "" {
190-
reg.Register(
191-
httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.FileResponse(tt.fixture))
192-
} else {
193-
panic("need either stubs or fixture key")
185+
reg.Register(httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.FileResponse(tt.fixture))
194186
}
195187

196188
opts.HttpClient = func() (*http.Client, error) {
197189
return &http.Client{Transport: reg}, nil
198190
}
199191

200192
err := checksRun(opts)
201-
if tt.wantErr {
202-
assert.Equal(t, "SilentError", err.Error())
203-
} else {
204-
assert.NoError(t, err)
193+
if err != nil {
194+
assert.Equal(t, tt.wantErr, err.Error())
195+
} else if tt.wantErr != "" {
196+
t.Errorf("expected %q, got nil error", tt.wantErr)
205197
}
206198

207199
assert.Equal(t, tt.wantOut, stdout.String())
208-
reg.Verify(t)
209200
})
210201
}
211202
}

0 commit comments

Comments
 (0)
X Tutup