From e53d02b680b1e8dd3ecc84462cdb53bf883a5c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 22 Mar 2021 12:55:04 +0100 Subject: [PATCH] Add back isolated tests for issue/PR lookup by argument --- pkg/cmd/issue/shared/lookup_test.go | 102 ++++++++++++++++++++++++++ pkg/cmd/pr/shared/lookup_test.go | 108 ++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 pkg/cmd/issue/shared/lookup_test.go create mode 100644 pkg/cmd/pr/shared/lookup_test.go diff --git a/pkg/cmd/issue/shared/lookup_test.go b/pkg/cmd/issue/shared/lookup_test.go new file mode 100644 index 000000000..a34c1fbb5 --- /dev/null +++ b/pkg/cmd/issue/shared/lookup_test.go @@ -0,0 +1,102 @@ +package shared + +import ( + "net/http" + "testing" + + "github.com/cli/cli/api" + "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/pkg/httpmock" +) + +func TestIssueFromArg(t *testing.T) { + type args struct { + baseRepoFn func() (ghrepo.Interface, error) + selector string + } + tests := []struct { + name string + args args + httpStub func(*httpmock.Registry) + wantIssue int + wantRepo string + wantErr bool + }{ + { + name: "number argument", + args: args{ + selector: "13", + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("OWNER/REPO") + }, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":13} + }}}`)) + }, + wantIssue: 13, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "number with hash argument", + args: args{ + selector: "#13", + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("OWNER/REPO") + }, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":13} + }}}`)) + }, + wantIssue: 13, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "URL argument", + args: args{ + selector: "https://example.org/OWNER/REPO/issues/13#comment-123", + baseRepoFn: nil, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":13} + }}}`)) + }, + wantIssue: 13, + wantRepo: "https://example.org/OWNER/REPO", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + if tt.httpStub != nil { + tt.httpStub(reg) + } + httpClient := &http.Client{Transport: reg} + issue, repo, err := IssueFromArg(api.NewClientFromHTTP(httpClient), tt.args.baseRepoFn, tt.args.selector) + if (err != nil) != tt.wantErr { + t.Errorf("IssueFromArg() error = %v, wantErr %v", err, tt.wantErr) + return + } + if issue.Number != tt.wantIssue { + t.Errorf("want issue #%d, got #%d", tt.wantIssue, issue.Number) + } + repoURL := ghrepo.GenerateRepoURL(repo, "") + if repoURL != tt.wantRepo { + t.Errorf("want repo %s, got %s", tt.wantRepo, repoURL) + } + }) + } +} diff --git a/pkg/cmd/pr/shared/lookup_test.go b/pkg/cmd/pr/shared/lookup_test.go new file mode 100644 index 000000000..4d843d7ae --- /dev/null +++ b/pkg/cmd/pr/shared/lookup_test.go @@ -0,0 +1,108 @@ +package shared + +import ( + "net/http" + "testing" + + "github.com/cli/cli/api" + "github.com/cli/cli/context" + "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/pkg/httpmock" +) + +func TestPRFromArgs(t *testing.T) { + type args struct { + baseRepoFn func() (ghrepo.Interface, error) + branchFn func() (string, error) + remotesFn func() (context.Remotes, error) + selector string + } + tests := []struct { + name string + args args + httpStub func(*httpmock.Registry) + wantPR int + wantRepo string + wantErr bool + }{ + { + name: "number argument", + args: args{ + selector: "13", + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("OWNER/REPO") + }, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query PullRequestByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "pullRequest":{"number":13} + }}}`)) + }, + wantPR: 13, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "number with hash argument", + args: args{ + selector: "#13", + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("OWNER/REPO") + }, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query PullRequestByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "pullRequest":{"number":13} + }}}`)) + }, + wantPR: 13, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "URL argument", + args: args{ + selector: "https://example.org/OWNER/REPO/pull/13/files", + baseRepoFn: nil, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query PullRequest_fields\b`), + httpmock.StringResponse(`{"data":{}}`)) + r.Register( + httpmock.GraphQL(`query PullRequest_fields2\b`), + httpmock.StringResponse(`{"data":{}}`)) + r.Register( + httpmock.GraphQL(`query PullRequestByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "pullRequest":{"number":13} + }}}`)) + }, + wantPR: 13, + wantRepo: "https://example.org/OWNER/REPO", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + if tt.httpStub != nil { + tt.httpStub(reg) + } + httpClient := &http.Client{Transport: reg} + pr, repo, err := PRFromArgs(api.NewClientFromHTTP(httpClient), tt.args.baseRepoFn, tt.args.branchFn, tt.args.remotesFn, tt.args.selector) + if (err != nil) != tt.wantErr { + t.Errorf("IssueFromArg() error = %v, wantErr %v", err, tt.wantErr) + return + } + if pr.Number != tt.wantPR { + t.Errorf("want issue #%d, got #%d", tt.wantPR, pr.Number) + } + repoURL := ghrepo.GenerateRepoURL(repo, "") + if repoURL != tt.wantRepo { + t.Errorf("want repo %s, got %s", tt.wantRepo, repoURL) + } + }) + } +}