diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index 26c832ab5..a9073ffa0 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -6,7 +6,6 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" - "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/issue/shared" @@ -20,7 +19,6 @@ type DevelopOptions struct { Config func() (config.Config, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) - Browser browser.Browser IssueRepo string IssueSelector string @@ -34,7 +32,6 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. IO: f.IOStreams, HttpClient: f.HttpClient, Config: f.Config, - Browser: f.Browser, BaseRepo: f.BaseRepo, } @@ -72,7 +69,6 @@ func developRun(opts *DevelopOptions) (err error) { if err != nil { return err } - opts.IO.StartProgressIndicator() repo, err := api.GitHubRepo(apiClient, baseRepo) if err != nil { return err @@ -102,7 +98,6 @@ func developRun(opts *DevelopOptions) (err error) { } ref, err := api.CreateBranchIssueReference(apiClient, repo, params) - opts.IO.StopProgressIndicator() if ref != nil { fmt.Fprintf(opts.IO.Out, "Created %s\n", ref.BranchName) } diff --git a/pkg/cmd/issue/develop/develop_test.go b/pkg/cmd/issue/develop/develop_test.go index 41f3cbd37..5967cedac 100644 --- a/pkg/cmd/issue/develop/develop_test.go +++ b/pkg/cmd/issue/develop/develop_test.go @@ -1,7 +1,91 @@ package develop -import "testing" +import ( + "net/http" + "testing" -func TestNewCmdDevelop(t *testing.T) { + "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/run" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/cli/cli/v2/pkg/prompt" + "github.com/stretchr/testify/assert" +) +func Test_developRun(t *testing.T) { + tests := []struct { + name string + setup func(*DevelopOptions, *testing.T) func() + cmdStubs func(*run.CommandStubber) + askStubs func(*prompt.AskStubber) // TODO eventually migrate to PrompterMock + httpStubs func(*httpmock.Registry, *testing.T) + expectedOut string + expectedErrOut string + expectedBrowse string + wantErr string + tty bool + }{ + {name: "develop new branch", + setup: func(opts *DevelopOptions, t *testing.T) func() { + opts.Name = "my-branch" + opts.BaseBranch = "main" + opts.IssueSelector = "123" + return func() {} + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.StubRepoResponse("OWNER", "REPO") + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"id":1, "number":123, "title":"my issue"}, + }}}`)) + reg.Register( + httpmock.GraphQL(`query BranchIssueReferenceFindBaseOid\b`), + httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) + + reg.Register( + httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQLMutation(` + { "data": { "createLinkedBranch": { "linkedBranch": 1 } } }`, + func(inputs map[string]interface{}) { + }), + ) + + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + defer reg.Verify(t) + if tt.httpStubs != nil { + tt.httpStubs(reg, t) + } + + opts := DevelopOptions{} + + opts.BaseRepo = func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + } + opts.HttpClient = func() (*http.Client, error) { + return &http.Client{Transport: reg}, nil + } + opts.Config = func() (config.Config, error) { + return config.NewBlankConfig(), nil + } + cleanSetup := func() {} + if tt.setup != nil { + cleanSetup = tt.setup(&opts, t) + } + defer cleanSetup() + + err := developRun(&opts) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + } else { + assert.NoError(t, err) + } + }) + } }