Skip to content

Commit c9bc185

Browse files
committed
Test gh pr create --reviewer tab completion
1 parent 8f20f0a commit c9bc185

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

pkg/cmd/pr/create/create_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,3 +2879,136 @@ 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+
}

0 commit comments

Comments
 (0)