github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/issue/reopen/reopen_test.go (about) 1 package reopen 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "regexp" 8 "testing" 9 10 "github.com/cli/cli/internal/config" 11 "github.com/cli/cli/internal/ghrepo" 12 "github.com/cli/cli/pkg/cmdutil" 13 "github.com/cli/cli/pkg/httpmock" 14 "github.com/cli/cli/pkg/iostreams" 15 "github.com/cli/cli/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 Config: func() (config.Config, error) { 32 return config.NewBlankConfig(), nil 33 }, 34 BaseRepo: func() (ghrepo.Interface, error) { 35 return ghrepo.New("OWNER", "REPO"), nil 36 }, 37 } 38 39 cmd := NewCmdReopen(factory, nil) 40 41 argv, err := shlex.Split(cli) 42 if err != nil { 43 return nil, err 44 } 45 cmd.SetArgs(argv) 46 47 cmd.SetIn(&bytes.Buffer{}) 48 cmd.SetOut(ioutil.Discard) 49 cmd.SetErr(ioutil.Discard) 50 51 _, err = cmd.ExecuteC() 52 return &test.CmdOut{ 53 OutBuf: stdout, 54 ErrBuf: stderr, 55 }, err 56 } 57 58 func TestIssueReopen(t *testing.T) { 59 http := &httpmock.Registry{} 60 defer http.Verify(t) 61 62 http.Register( 63 httpmock.GraphQL(`query IssueByNumber\b`), 64 httpmock.StringResponse(` 65 { "data": { "repository": { 66 "hasIssuesEnabled": true, 67 "issue": { "id": "THE-ID", "number": 2, "state": "CLOSED", "title": "The title of the issue"} 68 } } }`), 69 ) 70 http.Register( 71 httpmock.GraphQL(`mutation IssueReopen\b`), 72 httpmock.GraphQLMutation(`{"id": "THE-ID"}`, 73 func(inputs map[string]interface{}) { 74 assert.Equal(t, inputs["issueId"], "THE-ID") 75 }), 76 ) 77 78 output, err := runCommand(http, true, "2") 79 if err != nil { 80 t.Fatalf("error running command `issue reopen`: %v", err) 81 } 82 83 r := regexp.MustCompile(`Reopened issue #2 \(The title of the issue\)`) 84 85 if !r.MatchString(output.Stderr()) { 86 t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr()) 87 } 88 } 89 90 func TestIssueReopen_alreadyOpen(t *testing.T) { 91 http := &httpmock.Registry{} 92 defer http.Verify(t) 93 94 http.Register( 95 httpmock.GraphQL(`query IssueByNumber\b`), 96 httpmock.StringResponse(` 97 { "data": { "repository": { 98 "hasIssuesEnabled": true, 99 "issue": { "number": 2, "state": "OPEN", "title": "The title of the issue"} 100 } } }`), 101 ) 102 103 output, err := runCommand(http, true, "2") 104 if err != nil { 105 t.Fatalf("error running command `issue reopen`: %v", err) 106 } 107 108 r := regexp.MustCompile(`Issue #2 \(The title of the issue\) is already open`) 109 110 if !r.MatchString(output.Stderr()) { 111 t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr()) 112 } 113 } 114 115 func TestIssueReopen_issuesDisabled(t *testing.T) { 116 http := &httpmock.Registry{} 117 defer http.Verify(t) 118 119 http.Register( 120 httpmock.GraphQL(`query IssueByNumber\b`), 121 httpmock.StringResponse(` 122 { "data": { "repository": { 123 "hasIssuesEnabled": false 124 } } }`), 125 ) 126 127 _, err := runCommand(http, true, "2") 128 if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { 129 t.Fatalf("got error: %v", err) 130 } 131 }