github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/reopen/reopen_test.go (about) 1 package reopen 2 3 import ( 4 "bytes" 5 "io" 6 "net/http" 7 "regexp" 8 "testing" 9 10 "github.com/ungtb10d/cli/v2/internal/config" 11 "github.com/ungtb10d/cli/v2/internal/ghrepo" 12 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 13 "github.com/ungtb10d/cli/v2/pkg/httpmock" 14 "github.com/ungtb10d/cli/v2/pkg/iostreams" 15 "github.com/ungtb10d/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 ios, _, stdout, stderr := iostreams.Test() 22 ios.SetStdoutTTY(isTTY) 23 ios.SetStdinTTY(isTTY) 24 ios.SetStderrTTY(isTTY) 25 26 factory := &cmdutil.Factory{ 27 IOStreams: ios, 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(io.Discard) 49 cmd.SetErr(io.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 { 123 "data": { 124 "repository": { 125 "hasIssuesEnabled": false, 126 "issue": null 127 } 128 }, 129 "errors": [ 130 { 131 "type": "NOT_FOUND", 132 "path": [ 133 "repository", 134 "issue" 135 ], 136 "message": "Could not resolve to an issue or pull request with the number of 2." 137 } 138 ] 139 }`), 140 ) 141 142 _, err := runCommand(http, true, "2") 143 if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { 144 t.Fatalf("got error: %v", err) 145 } 146 } 147 148 func TestIssueReopen_withComment(t *testing.T) { 149 http := &httpmock.Registry{} 150 defer http.Verify(t) 151 152 http.Register( 153 httpmock.GraphQL(`query IssueByNumber\b`), 154 httpmock.StringResponse(` 155 { "data": { "repository": { 156 "hasIssuesEnabled": true, 157 "issue": { "id": "THE-ID", "number": 2, "state": "CLOSED", "title": "The title of the issue"} 158 } } }`), 159 ) 160 http.Register( 161 httpmock.GraphQL(`mutation CommentCreate\b`), 162 httpmock.GraphQLMutation(` 163 { "data": { "addComment": { "commentEdge": { "node": { 164 "url": "https://github.com/OWNER/REPO/issues/123#issuecomment-456" 165 } } } } }`, 166 func(inputs map[string]interface{}) { 167 assert.Equal(t, "THE-ID", inputs["subjectId"]) 168 assert.Equal(t, "reopening comment", inputs["body"]) 169 }), 170 ) 171 http.Register( 172 httpmock.GraphQL(`mutation IssueReopen\b`), 173 httpmock.GraphQLMutation(`{"id": "THE-ID"}`, 174 func(inputs map[string]interface{}) { 175 assert.Equal(t, inputs["issueId"], "THE-ID") 176 }), 177 ) 178 179 output, err := runCommand(http, true, "2 --comment 'reopening comment'") 180 if err != nil { 181 t.Fatalf("error running command `issue reopen`: %v", err) 182 } 183 184 r := regexp.MustCompile(`Reopened issue #2 \(The title of the issue\)`) 185 186 if !r.MatchString(output.Stderr()) { 187 t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr()) 188 } 189 }