github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/pr/reopen/reopen_test.go (about) 1 package reopen 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "testing" 8 9 "github.com/andrewhsu/cli/v2/api" 10 "github.com/andrewhsu/cli/v2/internal/ghrepo" 11 "github.com/andrewhsu/cli/v2/pkg/cmd/pr/shared" 12 "github.com/andrewhsu/cli/v2/pkg/cmdutil" 13 "github.com/andrewhsu/cli/v2/pkg/httpmock" 14 "github.com/andrewhsu/cli/v2/pkg/iostreams" 15 "github.com/andrewhsu/cli/v2/test" 16 "github.com/google/shlex" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) { 21 io, _, stdout, stderr := iostreams.Test() 22 io.SetStdoutTTY(isTTY) 23 io.SetStdinTTY(isTTY) 24 io.SetStderrTTY(isTTY) 25 26 factory := &cmdutil.Factory{ 27 IOStreams: io, 28 HttpClient: func() (*http.Client, error) { 29 return &http.Client{Transport: rt}, nil 30 }, 31 } 32 33 cmd := NewCmdReopen(factory, nil) 34 35 argv, err := shlex.Split(cli) 36 if err != nil { 37 return nil, err 38 } 39 cmd.SetArgs(argv) 40 41 cmd.SetIn(&bytes.Buffer{}) 42 cmd.SetOut(ioutil.Discard) 43 cmd.SetErr(ioutil.Discard) 44 45 _, err = cmd.ExecuteC() 46 return &test.CmdOut{ 47 OutBuf: stdout, 48 ErrBuf: stderr, 49 }, err 50 } 51 52 func TestPRReopen(t *testing.T) { 53 http := &httpmock.Registry{} 54 defer http.Verify(t) 55 56 shared.RunCommandFinder("123", &api.PullRequest{ 57 ID: "THE-ID", 58 Number: 123, 59 State: "CLOSED", 60 Title: "The title of the PR", 61 }, ghrepo.New("OWNER", "REPO")) 62 63 http.Register( 64 httpmock.GraphQL(`mutation PullRequestReopen\b`), 65 httpmock.GraphQLMutation(`{"id": "THE-ID"}`, 66 func(inputs map[string]interface{}) { 67 assert.Equal(t, inputs["pullRequestId"], "THE-ID") 68 }), 69 ) 70 71 output, err := runCommand(http, true, "123") 72 assert.NoError(t, err) 73 assert.Equal(t, "", output.String()) 74 assert.Equal(t, "✓ Reopened pull request #123 (The title of the PR)\n", output.Stderr()) 75 } 76 77 func TestPRReopen_alreadyOpen(t *testing.T) { 78 http := &httpmock.Registry{} 79 defer http.Verify(t) 80 81 shared.RunCommandFinder("123", &api.PullRequest{ 82 ID: "THE-ID", 83 Number: 123, 84 State: "OPEN", 85 Title: "The title of the PR", 86 }, ghrepo.New("OWNER", "REPO")) 87 88 output, err := runCommand(http, true, "123") 89 assert.NoError(t, err) 90 assert.Equal(t, "", output.String()) 91 assert.Equal(t, "! Pull request #123 (The title of the PR) is already open\n", output.Stderr()) 92 } 93 94 func TestPRReopen_alreadyMerged(t *testing.T) { 95 http := &httpmock.Registry{} 96 defer http.Verify(t) 97 98 shared.RunCommandFinder("123", &api.PullRequest{ 99 ID: "THE-ID", 100 Number: 123, 101 State: "MERGED", 102 Title: "The title of the PR", 103 }, ghrepo.New("OWNER", "REPO")) 104 105 output, err := runCommand(http, true, "123") 106 assert.EqualError(t, err, "SilentError") 107 assert.Equal(t, "", output.String()) 108 assert.Equal(t, "X Pull request #123 (The title of the PR) can't be reopened because it was already merged\n", output.Stderr()) 109 }