Skip to content

Commit e53d02b

Browse files
committed
Add back isolated tests for issue/PR lookup by argument
1 parent 111e8db commit e53d02b

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package shared
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/cli/cli/api"
8+
"github.com/cli/cli/internal/ghrepo"
9+
"github.com/cli/cli/pkg/httpmock"
10+
)
11+
12+
func TestIssueFromArg(t *testing.T) {
13+
type args struct {
14+
baseRepoFn func() (ghrepo.Interface, error)
15+
selector string
16+
}
17+
tests := []struct {
18+
name string
19+
args args
20+
httpStub func(*httpmock.Registry)
21+
wantIssue int
22+
wantRepo string
23+
wantErr bool
24+
}{
25+
{
26+
name: "number argument",
27+
args: args{
28+
selector: "13",
29+
baseRepoFn: func() (ghrepo.Interface, error) {
30+
return ghrepo.FromFullName("OWNER/REPO")
31+
},
32+
},
33+
httpStub: func(r *httpmock.Registry) {
34+
r.Register(
35+
httpmock.GraphQL(`query IssueByNumber\b`),
36+
httpmock.StringResponse(`{"data":{"repository":{
37+
"hasIssuesEnabled": true,
38+
"issue":{"number":13}
39+
}}}`))
40+
},
41+
wantIssue: 13,
42+
wantRepo: "https://github.com/OWNER/REPO",
43+
},
44+
{
45+
name: "number with hash argument",
46+
args: args{
47+
selector: "#13",
48+
baseRepoFn: func() (ghrepo.Interface, error) {
49+
return ghrepo.FromFullName("OWNER/REPO")
50+
},
51+
},
52+
httpStub: func(r *httpmock.Registry) {
53+
r.Register(
54+
httpmock.GraphQL(`query IssueByNumber\b`),
55+
httpmock.StringResponse(`{"data":{"repository":{
56+
"hasIssuesEnabled": true,
57+
"issue":{"number":13}
58+
}}}`))
59+
},
60+
wantIssue: 13,
61+
wantRepo: "https://github.com/OWNER/REPO",
62+
},
63+
{
64+
name: "URL argument",
65+
args: args{
66+
selector: "https://example.org/OWNER/REPO/issues/13#comment-123",
67+
baseRepoFn: nil,
68+
},
69+
httpStub: func(r *httpmock.Registry) {
70+
r.Register(
71+
httpmock.GraphQL(`query IssueByNumber\b`),
72+
httpmock.StringResponse(`{"data":{"repository":{
73+
"hasIssuesEnabled": true,
74+
"issue":{"number":13}
75+
}}}`))
76+
},
77+
wantIssue: 13,
78+
wantRepo: "https://example.org/OWNER/REPO",
79+
},
80+
}
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
reg := &httpmock.Registry{}
84+
if tt.httpStub != nil {
85+
tt.httpStub(reg)
86+
}
87+
httpClient := &http.Client{Transport: reg}
88+
issue, repo, err := IssueFromArg(api.NewClientFromHTTP(httpClient), tt.args.baseRepoFn, tt.args.selector)
89+
if (err != nil) != tt.wantErr {
90+
t.Errorf("IssueFromArg() error = %v, wantErr %v", err, tt.wantErr)
91+
return
92+
}
93+
if issue.Number != tt.wantIssue {
94+
t.Errorf("want issue #%d, got #%d", tt.wantIssue, issue.Number)
95+
}
96+
repoURL := ghrepo.GenerateRepoURL(repo, "")
97+
if repoURL != tt.wantRepo {
98+
t.Errorf("want repo %s, got %s", tt.wantRepo, repoURL)
99+
}
100+
})
101+
}
102+
}

pkg/cmd/pr/shared/lookup_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package shared
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/cli/cli/api"
8+
"github.com/cli/cli/context"
9+
"github.com/cli/cli/internal/ghrepo"
10+
"github.com/cli/cli/pkg/httpmock"
11+
)
12+
13+
func TestPRFromArgs(t *testing.T) {
14+
type args struct {
15+
baseRepoFn func() (ghrepo.Interface, error)
16+
branchFn func() (string, error)
17+
remotesFn func() (context.Remotes, error)
18+
selector string
19+
}
20+
tests := []struct {
21+
name string
22+
args args
23+
httpStub func(*httpmock.Registry)
24+
wantPR int
25+
wantRepo string
26+
wantErr bool
27+
}{
28+
{
29+
name: "number argument",
30+
args: args{
31+
selector: "13",
32+
baseRepoFn: func() (ghrepo.Interface, error) {
33+
return ghrepo.FromFullName("OWNER/REPO")
34+
},
35+
},
36+
httpStub: func(r *httpmock.Registry) {
37+
r.Register(
38+
httpmock.GraphQL(`query PullRequestByNumber\b`),
39+
httpmock.StringResponse(`{"data":{"repository":{
40+
"pullRequest":{"number":13}
41+
}}}`))
42+
},
43+
wantPR: 13,
44+
wantRepo: "https://github.com/OWNER/REPO",
45+
},
46+
{
47+
name: "number with hash argument",
48+
args: args{
49+
selector: "#13",
50+
baseRepoFn: func() (ghrepo.Interface, error) {
51+
return ghrepo.FromFullName("OWNER/REPO")
52+
},
53+
},
54+
httpStub: func(r *httpmock.Registry) {
55+
r.Register(
56+
httpmock.GraphQL(`query PullRequestByNumber\b`),
57+
httpmock.StringResponse(`{"data":{"repository":{
58+
"pullRequest":{"number":13}
59+
}}}`))
60+
},
61+
wantPR: 13,
62+
wantRepo: "https://github.com/OWNER/REPO",
63+
},
64+
{
65+
name: "URL argument",
66+
args: args{
67+
selector: "https://example.org/OWNER/REPO/pull/13/files",
68+
baseRepoFn: nil,
69+
},
70+
httpStub: func(r *httpmock.Registry) {
71+
r.Register(
72+
httpmock.GraphQL(`query PullRequest_fields\b`),
73+
httpmock.StringResponse(`{"data":{}}`))
74+
r.Register(
75+
httpmock.GraphQL(`query PullRequest_fields2\b`),
76+
httpmock.StringResponse(`{"data":{}}`))
77+
r.Register(
78+
httpmock.GraphQL(`query PullRequestByNumber\b`),
79+
httpmock.StringResponse(`{"data":{"repository":{
80+
"pullRequest":{"number":13}
81+
}}}`))
82+
},
83+
wantPR: 13,
84+
wantRepo: "https://example.org/OWNER/REPO",
85+
},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
reg := &httpmock.Registry{}
90+
if tt.httpStub != nil {
91+
tt.httpStub(reg)
92+
}
93+
httpClient := &http.Client{Transport: reg}
94+
pr, repo, err := PRFromArgs(api.NewClientFromHTTP(httpClient), tt.args.baseRepoFn, tt.args.branchFn, tt.args.remotesFn, tt.args.selector)
95+
if (err != nil) != tt.wantErr {
96+
t.Errorf("IssueFromArg() error = %v, wantErr %v", err, tt.wantErr)
97+
return
98+
}
99+
if pr.Number != tt.wantPR {
100+
t.Errorf("want issue #%d, got #%d", tt.wantPR, pr.Number)
101+
}
102+
repoURL := ghrepo.GenerateRepoURL(repo, "")
103+
if repoURL != tt.wantRepo {
104+
t.Errorf("want repo %s, got %s", tt.wantRepo, repoURL)
105+
}
106+
})
107+
}
108+
}

0 commit comments

Comments
 (0)