Skip to content

Commit bbc3d02

Browse files
committed
Refactor tab completion test
This commit moves the `gh pr create` tab completion test closer to the logic rather than the commands that use it. This should ensure that any command or flag that lists reviewers will present teams and users as expected.
1 parent c9bc185 commit bbc3d02

2 files changed

Lines changed: 120 additions & 133 deletions

File tree

pkg/cmd/pr/create/create_test.go

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,136 +2879,3 @@ func TestProjectsV1Deprecation(t *testing.T) {
28792879
})
28802880
})
28812881
}
2882-
2883-
func Test_requestableReviewersForCompletion(t *testing.T) {
2884-
tests := []struct {
2885-
name string
2886-
tty bool
2887-
expectedReviewers []string
2888-
httpStubs func(*httpmock.Registry, *testing.T)
2889-
}{
2890-
{
2891-
name: "when users and teams are both available, both are listed",
2892-
expectedReviewers: []string{"MonaLisa\tMona Display Name", "OWNER/core", "OWNER/robots", "hubot"},
2893-
httpStubs: func(reg *httpmock.Registry, t *testing.T) {
2894-
reg.Register(
2895-
httpmock.GraphQL(`query UserCurrent\b`),
2896-
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
2897-
reg.Register(
2898-
httpmock.GraphQL(`query RepositoryAssignableUsers\b`),
2899-
httpmock.StringResponse(`
2900-
{ "data": { "repository": { "assignableUsers": {
2901-
"nodes": [
2902-
{ "login": "hubot", "id": "HUBOTID", "name": "" },
2903-
{ "login": "MonaLisa", "id": "MONAID", "name": "Mona Display Name" }
2904-
],
2905-
"pageInfo": { "hasNextPage": false }
2906-
} } } }
2907-
`))
2908-
reg.Register(
2909-
httpmock.GraphQL(`query OrganizationTeamList\b`),
2910-
httpmock.StringResponse(`
2911-
{ "data": { "organization": { "teams": {
2912-
"nodes": [
2913-
{ "slug": "core", "id": "COREID" },
2914-
{ "slug": "robots", "id": "ROBOTID" }
2915-
],
2916-
"pageInfo": { "hasNextPage": false }
2917-
} } } }
2918-
`))
2919-
},
2920-
},
2921-
{
2922-
name: "when users are available but teams aren't, users are listed",
2923-
expectedReviewers: []string{"MonaLisa\tMona Display Name", "hubot"},
2924-
httpStubs: func(reg *httpmock.Registry, t *testing.T) {
2925-
reg.Register(
2926-
httpmock.GraphQL(`query UserCurrent\b`),
2927-
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
2928-
reg.Register(
2929-
httpmock.GraphQL(`query RepositoryAssignableUsers\b`),
2930-
httpmock.StringResponse(`
2931-
{ "data": { "repository": { "assignableUsers": {
2932-
"nodes": [
2933-
{ "login": "hubot", "id": "HUBOTID", "name": "" },
2934-
{ "login": "MonaLisa", "id": "MONAID", "name": "Mona Display Name" }
2935-
],
2936-
"pageInfo": { "hasNextPage": false }
2937-
} } } }
2938-
`))
2939-
reg.Register(
2940-
httpmock.GraphQL(`query OrganizationTeamList\b`),
2941-
httpmock.StringResponse(`
2942-
{ "data": { "organization": { "teams": {
2943-
"nodes": [],
2944-
"pageInfo": { "hasNextPage": false }
2945-
} } } }
2946-
`))
2947-
},
2948-
},
2949-
{
2950-
name: "when teams are available but users aren't, teams are listed",
2951-
expectedReviewers: []string{"OWNER/core", "OWNER/robots"},
2952-
httpStubs: func(reg *httpmock.Registry, t *testing.T) {
2953-
reg.Register(
2954-
httpmock.GraphQL(`query UserCurrent\b`),
2955-
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
2956-
reg.Register(
2957-
httpmock.GraphQL(`query RepositoryAssignableUsers\b`),
2958-
httpmock.StringResponse(`
2959-
{ "data": { "repository": { "assignableUsers": {
2960-
"nodes": [],
2961-
"pageInfo": { "hasNextPage": false }
2962-
} } } }
2963-
`))
2964-
reg.Register(
2965-
httpmock.GraphQL(`query OrganizationTeamList\b`),
2966-
httpmock.StringResponse(`
2967-
{ "data": { "organization": { "teams": {
2968-
"nodes": [
2969-
{ "slug": "core", "id": "COREID" },
2970-
{ "slug": "robots", "id": "ROBOTID" }
2971-
],
2972-
"pageInfo": { "hasNextPage": false }
2973-
} } } }
2974-
`))
2975-
},
2976-
},
2977-
}
2978-
2979-
for _, tt := range tests {
2980-
t.Run(tt.name, func(t *testing.T) {
2981-
reg := &httpmock.Registry{}
2982-
defer reg.Verify(t)
2983-
if tt.httpStubs != nil {
2984-
tt.httpStubs(reg, t)
2985-
}
2986-
2987-
ios, _, _, _ := iostreams.Test()
2988-
ios.SetStdoutTTY(tt.tty)
2989-
ios.SetStdinTTY(tt.tty)
2990-
ios.SetStderrTTY(tt.tty)
2991-
2992-
opts := &CreateOptions{}
2993-
opts.IO = ios
2994-
opts.HttpClient = func() (*http.Client, error) {
2995-
return &http.Client{Transport: reg}, nil
2996-
}
2997-
opts.Remotes = func() (context.Remotes, error) {
2998-
return context.Remotes{
2999-
{
3000-
Remote: &git.Remote{
3001-
Name: "origin",
3002-
Resolved: "base",
3003-
},
3004-
Repo: ghrepo.New("OWNER", "REPO"),
3005-
},
3006-
}, nil
3007-
}
3008-
3009-
reviewers, err := requestableReviewersForCompletion(opts)
3010-
require.NoError(t, err)
3011-
require.Equal(t, tt.expectedReviewers, reviewers)
3012-
})
3013-
}
3014-
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package shared
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/cli/cli/v2/internal/ghrepo"
8+
"github.com/cli/cli/v2/pkg/httpmock"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestRequestableReviewersForCompletion(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
expectedReviewers []string
16+
httpStubs func(*httpmock.Registry, *testing.T)
17+
}{
18+
{
19+
name: "when users and teams are both available, both are listed",
20+
expectedReviewers: []string{"MonaLisa\tMona Display Name", "OWNER/core", "OWNER/robots", "hubot"},
21+
httpStubs: func(reg *httpmock.Registry, t *testing.T) {
22+
reg.Register(
23+
httpmock.GraphQL(`query UserCurrent\b`),
24+
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
25+
reg.Register(
26+
httpmock.GraphQL(`query RepositoryAssignableUsers\b`),
27+
httpmock.StringResponse(`
28+
{ "data": { "repository": { "assignableUsers": {
29+
"nodes": [
30+
{ "login": "hubot", "id": "HUBOTID", "name": "" },
31+
{ "login": "MonaLisa", "id": "MONAID", "name": "Mona Display Name" }
32+
],
33+
"pageInfo": { "hasNextPage": false }
34+
} } } }
35+
`))
36+
reg.Register(
37+
httpmock.GraphQL(`query OrganizationTeamList\b`),
38+
httpmock.StringResponse(`
39+
{ "data": { "organization": { "teams": {
40+
"nodes": [
41+
{ "slug": "core", "id": "COREID" },
42+
{ "slug": "robots", "id": "ROBOTID" }
43+
],
44+
"pageInfo": { "hasNextPage": false }
45+
} } } }
46+
`))
47+
},
48+
},
49+
{
50+
name: "when users are available but teams aren't, users are listed",
51+
expectedReviewers: []string{"MonaLisa\tMona Display Name", "hubot"},
52+
httpStubs: func(reg *httpmock.Registry, t *testing.T) {
53+
reg.Register(
54+
httpmock.GraphQL(`query UserCurrent\b`),
55+
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
56+
reg.Register(
57+
httpmock.GraphQL(`query RepositoryAssignableUsers\b`),
58+
httpmock.StringResponse(`
59+
{ "data": { "repository": { "assignableUsers": {
60+
"nodes": [
61+
{ "login": "hubot", "id": "HUBOTID", "name": "" },
62+
{ "login": "MonaLisa", "id": "MONAID", "name": "Mona Display Name" }
63+
],
64+
"pageInfo": { "hasNextPage": false }
65+
} } } }
66+
`))
67+
reg.Register(
68+
httpmock.GraphQL(`query OrganizationTeamList\b`),
69+
httpmock.StringResponse(`
70+
{ "data": { "organization": { "teams": {
71+
"nodes": [],
72+
"pageInfo": { "hasNextPage": false }
73+
} } } }
74+
`))
75+
},
76+
},
77+
{
78+
name: "when teams are available but users aren't, teams are listed",
79+
expectedReviewers: []string{"OWNER/core", "OWNER/robots"},
80+
httpStubs: func(reg *httpmock.Registry, t *testing.T) {
81+
reg.Register(
82+
httpmock.GraphQL(`query UserCurrent\b`),
83+
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
84+
reg.Register(
85+
httpmock.GraphQL(`query RepositoryAssignableUsers\b`),
86+
httpmock.StringResponse(`
87+
{ "data": { "repository": { "assignableUsers": {
88+
"nodes": [],
89+
"pageInfo": { "hasNextPage": false }
90+
} } } }
91+
`))
92+
reg.Register(
93+
httpmock.GraphQL(`query OrganizationTeamList\b`),
94+
httpmock.StringResponse(`
95+
{ "data": { "organization": { "teams": {
96+
"nodes": [
97+
{ "slug": "core", "id": "COREID" },
98+
{ "slug": "robots", "id": "ROBOTID" }
99+
],
100+
"pageInfo": { "hasNextPage": false }
101+
} } } }
102+
`))
103+
},
104+
},
105+
}
106+
107+
for _, tt := range tests {
108+
t.Run(tt.name, func(t *testing.T) {
109+
reg := &httpmock.Registry{}
110+
defer reg.Verify(t)
111+
if tt.httpStubs != nil {
112+
tt.httpStubs(reg, t)
113+
}
114+
115+
reviewers, err := RequestableReviewersForCompletion(&http.Client{Transport: reg}, ghrepo.New("OWNER", "REPO"))
116+
require.NoError(t, err)
117+
require.Equal(t, tt.expectedReviewers, reviewers)
118+
})
119+
}
120+
}

0 commit comments

Comments
 (0)