github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/list/list_test.go (about) 1 package list 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "testing" 9 10 "github.com/MakeNowJust/heredoc" 11 "github.com/cli/cli/internal/ghrepo" 12 "github.com/cli/cli/internal/run" 13 "github.com/cli/cli/pkg/cmdutil" 14 "github.com/cli/cli/pkg/httpmock" 15 "github.com/cli/cli/pkg/iostreams" 16 "github.com/cli/cli/test" 17 "github.com/google/shlex" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) { 22 io, _, stdout, stderr := iostreams.Test() 23 io.SetStdoutTTY(isTTY) 24 io.SetStdinTTY(isTTY) 25 io.SetStderrTTY(isTTY) 26 27 browser := &cmdutil.TestBrowser{} 28 factory := &cmdutil.Factory{ 29 IOStreams: io, 30 Browser: browser, 31 HttpClient: func() (*http.Client, error) { 32 return &http.Client{Transport: rt}, nil 33 }, 34 BaseRepo: func() (ghrepo.Interface, error) { 35 return ghrepo.New("OWNER", "REPO"), nil 36 }, 37 } 38 39 cmd := NewCmdList(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 BrowsedURL: browser.BrowsedURL(), 56 }, err 57 } 58 59 func initFakeHTTP() *httpmock.Registry { 60 return &httpmock.Registry{} 61 } 62 63 func TestPRList(t *testing.T) { 64 http := initFakeHTTP() 65 defer http.Verify(t) 66 67 http.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) 68 69 output, err := runCommand(http, true, "") 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 assert.Equal(t, heredoc.Doc(` 75 76 Showing 3 of 3 open pull requests in OWNER/REPO 77 78 #32 New feature feature 79 #29 Fixed bad bug hubot:bug-fix 80 #28 Improve documentation docs 81 `), output.String()) 82 assert.Equal(t, ``, output.Stderr()) 83 } 84 85 func TestPRList_nontty(t *testing.T) { 86 http := initFakeHTTP() 87 defer http.Verify(t) 88 89 http.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) 90 91 output, err := runCommand(http, false, "") 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 assert.Equal(t, "", output.Stderr()) 97 98 assert.Equal(t, `32 New feature feature DRAFT 99 29 Fixed bad bug hubot:bug-fix OPEN 100 28 Improve documentation docs MERGED 101 `, output.String()) 102 } 103 104 func TestPRList_filtering(t *testing.T) { 105 http := initFakeHTTP() 106 defer http.Verify(t) 107 108 http.Register( 109 httpmock.GraphQL(`query PullRequestList\b`), 110 httpmock.GraphQLQuery(`{}`, func(_ string, params map[string]interface{}) { 111 assert.Equal(t, []interface{}{"OPEN", "CLOSED", "MERGED"}, params["state"].([]interface{})) 112 })) 113 114 output, err := runCommand(http, true, `-s all`) 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 assert.Equal(t, "", output.Stderr()) 120 assert.Equal(t, ` 121 No pull requests match your search in OWNER/REPO 122 123 `, output.String()) 124 } 125 126 func TestPRList_filteringRemoveDuplicate(t *testing.T) { 127 http := initFakeHTTP() 128 defer http.Verify(t) 129 130 http.Register( 131 httpmock.GraphQL(`query PullRequestList\b`), 132 httpmock.FileResponse("./fixtures/prListWithDuplicates.json")) 133 134 output, err := runCommand(http, true, "") 135 if err != nil { 136 t.Fatal(err) 137 } 138 139 out := output.String() 140 idx := strings.Index(out, "New feature") 141 if idx < 0 { 142 t.Fatalf("text %q not found in %q", "New feature", out) 143 } 144 assert.Equal(t, idx, strings.LastIndex(out, "New feature")) 145 } 146 147 func TestPRList_filteringClosed(t *testing.T) { 148 http := initFakeHTTP() 149 defer http.Verify(t) 150 151 http.Register( 152 httpmock.GraphQL(`query PullRequestList\b`), 153 httpmock.GraphQLQuery(`{}`, func(_ string, params map[string]interface{}) { 154 assert.Equal(t, []interface{}{"CLOSED", "MERGED"}, params["state"].([]interface{})) 155 })) 156 157 _, err := runCommand(http, true, `-s closed`) 158 if err != nil { 159 t.Fatal(err) 160 } 161 } 162 163 func TestPRList_filteringAssignee(t *testing.T) { 164 http := initFakeHTTP() 165 defer http.Verify(t) 166 167 http.Register( 168 httpmock.GraphQL(`query PullRequestSearch\b`), 169 httpmock.GraphQLQuery(`{}`, func(_ string, params map[string]interface{}) { 170 assert.Equal(t, `repo:OWNER/REPO is:pr is:merged assignee:hubot label:"needs tests" base:develop`, params["q"].(string)) 171 })) 172 173 _, err := runCommand(http, true, `-s merged -l "needs tests" -a hubot -B develop`) 174 if err != nil { 175 t.Fatal(err) 176 } 177 } 178 179 func TestPRList_filteringAssigneeLabels(t *testing.T) { 180 http := initFakeHTTP() 181 defer http.Verify(t) 182 183 _, err := runCommand(http, true, `-l one,two -a hubot`) 184 if err == nil && err.Error() != "multiple labels with --assignee are not supported" { 185 t.Fatal(err) 186 } 187 } 188 189 func TestPRList_withInvalidLimitFlag(t *testing.T) { 190 http := initFakeHTTP() 191 defer http.Verify(t) 192 193 _, err := runCommand(http, true, `--limit=0`) 194 if err == nil && err.Error() != "invalid limit: 0" { 195 t.Errorf("error running command `issue list`: %v", err) 196 } 197 } 198 199 func TestPRList_web(t *testing.T) { 200 http := initFakeHTTP() 201 defer http.Verify(t) 202 203 _, cmdTeardown := run.Stub() 204 defer cmdTeardown(t) 205 206 output, err := runCommand(http, true, "--web -a peter -l bug -l docs -L 10 -s merged -B trunk") 207 if err != nil { 208 t.Errorf("error running command `pr list` with `--web` flag: %v", err) 209 } 210 211 assert.Equal(t, "", output.String()) 212 assert.Equal(t, "Opening github.com/OWNER/REPO/pulls in your browser.\n", output.Stderr()) 213 assert.Equal(t, "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Amerged+assignee%3Apeter+label%3Abug+label%3Adocs+base%3Atrunk", output.BrowsedURL) 214 }