|
| 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