@@ -2,10 +2,14 @@ package list
22
33import (
44 "bytes"
5+ "fmt"
56 "io/ioutil"
7+ "net/http"
68 "testing"
79
10+ "github.com/cli/cli/internal/ghrepo"
811 "github.com/cli/cli/pkg/cmdutil"
12+ "github.com/cli/cli/pkg/httpmock"
913 "github.com/cli/cli/pkg/iostreams"
1014 "github.com/google/shlex"
1115 "github.com/stretchr/testify/assert"
@@ -92,4 +96,146 @@ func Test_NewCmdList(t *testing.T) {
9296 }
9397}
9498
95- // TODO execution tests
99+ func TestListRun (t * testing.T ) {
100+ workflows := []Workflow {
101+ {
102+ Name : "Go" ,
103+ State : Active ,
104+ ID : 707 ,
105+ },
106+ {
107+ Name : "Linter" ,
108+ State : Active ,
109+ ID : 666 ,
110+ },
111+ {
112+ Name : "Release" ,
113+ State : DisabledManually ,
114+ ID : 451 ,
115+ },
116+ }
117+ payload := WorkflowsPayload {Workflows : workflows }
118+
119+ tests := []struct {
120+ name string
121+ opts * ListOptions
122+ wantOut string
123+ wantErrOut string
124+ stubs func (* httpmock.Registry )
125+ tty bool
126+ }{
127+ {
128+ name : "blank tty" ,
129+ tty : true ,
130+ opts : & ListOptions {
131+ Limit : defaultLimit ,
132+ },
133+ stubs : func (reg * httpmock.Registry ) {
134+ reg .Register (
135+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/workflows" ),
136+ httpmock .JSONResponse (payload ))
137+ },
138+ wantOut : "Go active 707\n Linter active 666\n " ,
139+ },
140+ {
141+ name : "blank nontty" ,
142+ opts : & ListOptions {
143+ Limit : defaultLimit ,
144+ PlainOutput : true ,
145+ },
146+ stubs : func (reg * httpmock.Registry ) {
147+ reg .Register (
148+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/workflows" ),
149+ httpmock .JSONResponse (payload ))
150+ },
151+ wantOut : "Go\t active\t 707\n Linter\t active\t 666\n " ,
152+ },
153+ {
154+ name : "pagination" ,
155+ opts : & ListOptions {
156+ Limit : 101 ,
157+ },
158+ stubs : func (reg * httpmock.Registry ) {
159+ workflows := []Workflow {}
160+ for flowID := 0 ; flowID < 103 ; flowID ++ {
161+ workflows = append (workflows , Workflow {
162+ ID : flowID ,
163+ Name : fmt .Sprintf ("flow %d" , flowID ),
164+ State : Active ,
165+ })
166+ }
167+ reg .Register (
168+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/workflows" ),
169+ httpmock .JSONResponse (WorkflowsPayload {
170+ Workflows : workflows [0 :100 ],
171+ }))
172+ reg .Register (
173+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/workflows" ),
174+ httpmock .JSONResponse (WorkflowsPayload {
175+ Workflows : workflows [100 :],
176+ }))
177+ },
178+ wantOut : longOutput ,
179+ },
180+ /*
181+ {
182+ name: "no results nontty",
183+ opts: &ListOptions{
184+ Limit: defaultLimit,
185+ PlainOutput: true,
186+ },
187+ stubs: func(reg *httpmock.Registry) {
188+ reg.Register(
189+ httpmock.REST("GET", "TODO"),
190+ httpmock.JSONResponse(shared.RunsPayload{}),
191+ )
192+ },
193+ nontty: true,
194+ wantOut: "",
195+ },
196+ {
197+ name: "no results tty",
198+ opts: &ListOptions{
199+ Limit: defaultLimit,
200+ },
201+ stubs: func(reg *httpmock.Registry) {
202+ reg.Register(
203+ httpmock.REST("GET", "TODO"),
204+ httpmock.JSONResponse(shared.RunsPayload{}),
205+ )
206+ },
207+ wantOut: "",
208+ wantErrOut: "No workflows found\n",
209+ },
210+
211+ // TODO showing all workflows
212+ */
213+ }
214+
215+ for _ , tt := range tests {
216+ t .Run (tt .name , func (t * testing.T ) {
217+ reg := & httpmock.Registry {}
218+ tt .stubs (reg )
219+
220+ tt .opts .HttpClient = func () (* http.Client , error ) {
221+ return & http.Client {Transport : reg }, nil
222+ }
223+
224+ io , _ , stdout , stderr := iostreams .Test ()
225+ io .SetStdoutTTY (tt .tty )
226+ tt .opts .IO = io
227+ tt .opts .BaseRepo = func () (ghrepo.Interface , error ) {
228+ return ghrepo .FromFullName ("OWNER/REPO" )
229+ }
230+
231+ err := listRun (tt .opts )
232+ assert .NoError (t , err )
233+
234+ assert .Equal (t , tt .wantOut , stdout .String ())
235+ assert .Equal (t , tt .wantErrOut , stderr .String ())
236+ reg .Verify (t )
237+ })
238+ }
239+ }
240+
241+ const longOutput = "flow 0\t active\t 0\n flow 1\t active\t 1\n flow 2\t active\t 2\n flow 3\t active\t 3\n flow 4\t active\t 4\n flow 5\t active\t 5\n flow 6\t active\t 6\n flow 7\t active\t 7\n flow 8\t active\t 8\n flow 9\t active\t 9\n flow 10\t active\t 10\n flow 11\t active\t 11\n flow 12\t active\t 12\n flow 13\t active\t 13\n flow 14\t active\t 14\n flow 15\t active\t 15\n flow 16\t active\t 16\n flow 17\t active\t 17\n flow 18\t active\t 18\n flow 19\t active\t 19\n flow 20\t active\t 20\n flow 21\t active\t 21\n flow 22\t active\t 22\n flow 23\t active\t 23\n flow 24\t active\t 24\n flow 25\t active\t 25\n flow 26\t active\t 26\n flow 27\t active\t 27\n flow 28\t active\t 28\n flow 29\t active\t 29\n flow 30\t active\t 30\n flow 31\t active\t 31\n flow 32\t active\t 32\n flow 33\t active\t 33\n flow 34\t active\t 34\n flow 35\t active\t 35\n flow 36\t active\t 36\n flow 37\t active\t 37\n flow 38\t active\t 38\n flow 39\t active\t 39\n flow 40\t active\t 40\n flow 41\t active\t 41\n flow 42\t active\t 42\n flow 43\t active\t 43\n flow 44\t active\t 44\n flow 45\t active\t 45\n flow 46\t active\t 46\n flow 47\t active\t 47\n flow 48\t active\t 48\n flow 49\t active\t 49\n flow 50\t active\t 50\n flow 51\t active\t 51\n flow 52\t active\t 52\n flow 53\t active\t 53\n flow 54\t active\t 54\n flow 55\t active\t 55\n flow 56\t active\t 56\n flow 57\t active\t 57\n flow 58\t active\t 58\n flow 59\t active\t 59\n flow 60\t active\t 60\n flow 61\t active\t 61\n flow 62\t active\t 62\n flow 63\t active\t 63\n flow 64\t active\t 64\n flow 65\t active\t 65\n flow 66\t active\t 66\n flow 67\t active\t 67\n flow 68\t active\t 68\n flow 69\t active\t 69\n flow 70\t active\t 70\n flow 71\t active\t 71\n flow 72\t active\t 72\n flow 73\t active\t 73\n flow 74\t active\t 74\n flow 75\t active\t 75\n flow 76\t active\t 76\n flow 77\t active\t 77\n flow 78\t active\t 78\n flow 79\t active\t 79\n flow 80\t active\t 80\n flow 81\t active\t 81\n flow 82\t active\t 82\n flow 83\t active\t 83\n flow 84\t active\t 84\n flow 85\t active\t 85\n flow 86\t active\t 86\n flow 87\t active\t 87\n flow 88\t active\t 88\n flow 89\t active\t 89\n flow 90\t active\t 90\n flow 91\t active\t 91\n flow 92\t active\t 92\n flow 93\t active\t 93\n flow 94\t active\t 94\n flow 95\t active\t 95\n flow 96\t active\t 96\n flow 97\t active\t 97\n flow 98\t active\t 98\n flow 99\t active\t 99\n flow 100\t active\t 100\n "
0 commit comments