github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/workflow/list/list_test.go (about) 1 package list 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "net/http" 8 "testing" 9 10 "github.com/ungtb10d/cli/v2/internal/ghrepo" 11 "github.com/ungtb10d/cli/v2/pkg/cmd/workflow/shared" 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/google/shlex" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func Test_NewCmdList(t *testing.T) { 20 tests := []struct { 21 name string 22 cli string 23 tty bool 24 wants ListOptions 25 wantsErr bool 26 }{ 27 { 28 name: "blank tty", 29 tty: true, 30 wants: ListOptions{ 31 Limit: defaultLimit, 32 }, 33 }, 34 { 35 name: "blank nontty", 36 wants: ListOptions{ 37 Limit: defaultLimit, 38 PlainOutput: true, 39 }, 40 }, 41 { 42 name: "all", 43 cli: "--all", 44 wants: ListOptions{ 45 Limit: defaultLimit, 46 PlainOutput: true, 47 All: true, 48 }, 49 }, 50 { 51 name: "limit", 52 cli: "--limit 100", 53 wants: ListOptions{ 54 Limit: 100, 55 PlainOutput: true, 56 }, 57 }, 58 { 59 name: "bad limit", 60 cli: "--limit hi", 61 wantsErr: true, 62 }, 63 } 64 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 ios, _, _, _ := iostreams.Test() 68 ios.SetStdinTTY(tt.tty) 69 ios.SetStdoutTTY(tt.tty) 70 71 f := &cmdutil.Factory{ 72 IOStreams: ios, 73 } 74 75 argv, err := shlex.Split(tt.cli) 76 assert.NoError(t, err) 77 78 var gotOpts *ListOptions 79 cmd := NewCmdList(f, func(opts *ListOptions) error { 80 gotOpts = opts 81 return nil 82 }) 83 cmd.SetArgs(argv) 84 cmd.SetIn(&bytes.Buffer{}) 85 cmd.SetOut(io.Discard) 86 cmd.SetErr(io.Discard) 87 88 _, err = cmd.ExecuteC() 89 if tt.wantsErr { 90 assert.Error(t, err) 91 return 92 } 93 94 assert.Equal(t, tt.wants.Limit, gotOpts.Limit) 95 assert.Equal(t, tt.wants.PlainOutput, gotOpts.PlainOutput) 96 }) 97 } 98 } 99 100 func TestListRun(t *testing.T) { 101 workflows := []shared.Workflow{ 102 { 103 Name: "Go", 104 State: shared.Active, 105 ID: 707, 106 }, 107 { 108 Name: "Linter", 109 State: shared.Active, 110 ID: 666, 111 }, 112 { 113 Name: "Release", 114 State: shared.DisabledManually, 115 ID: 451, 116 }, 117 } 118 payload := shared.WorkflowsPayload{Workflows: workflows} 119 120 tests := []struct { 121 name string 122 opts *ListOptions 123 wantErr bool 124 wantOut string 125 wantErrOut string 126 stubs func(*httpmock.Registry) 127 tty bool 128 }{ 129 { 130 name: "blank tty", 131 tty: true, 132 opts: &ListOptions{ 133 Limit: defaultLimit, 134 }, 135 wantOut: "Go active 707\nLinter active 666\n", 136 }, 137 { 138 name: "blank nontty", 139 opts: &ListOptions{ 140 Limit: defaultLimit, 141 PlainOutput: true, 142 }, 143 wantOut: "Go\tactive\t707\nLinter\tactive\t666\n", 144 }, 145 { 146 name: "pagination", 147 opts: &ListOptions{ 148 Limit: 101, 149 }, 150 stubs: func(reg *httpmock.Registry) { 151 workflows := []shared.Workflow{} 152 var flowID int64 153 for flowID = 0; flowID < 103; flowID++ { 154 workflows = append(workflows, shared.Workflow{ 155 ID: flowID, 156 Name: fmt.Sprintf("flow %d", flowID), 157 State: shared.Active, 158 }) 159 } 160 reg.Register( 161 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 162 httpmock.JSONResponse(shared.WorkflowsPayload{ 163 Workflows: workflows[0:100], 164 })) 165 reg.Register( 166 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 167 httpmock.JSONResponse(shared.WorkflowsPayload{ 168 Workflows: workflows[100:], 169 })) 170 }, 171 wantOut: longOutput, 172 }, 173 { 174 name: "no results", 175 opts: &ListOptions{ 176 Limit: defaultLimit, 177 PlainOutput: true, 178 }, 179 stubs: func(reg *httpmock.Registry) { 180 reg.Register( 181 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 182 httpmock.JSONResponse(shared.WorkflowsPayload{}), 183 ) 184 }, 185 wantErr: true, 186 }, 187 { 188 name: "show all workflows", 189 opts: &ListOptions{ 190 Limit: defaultLimit, 191 All: true, 192 }, 193 tty: true, 194 wantOut: "Go active 707\nLinter active 666\nRelease disabled_manually 451\n", 195 }, 196 { 197 name: "respects limit", 198 opts: &ListOptions{ 199 Limit: 1, 200 All: true, 201 }, 202 tty: true, 203 wantOut: "Go active 707\n", 204 }, 205 } 206 207 for _, tt := range tests { 208 t.Run(tt.name, func(t *testing.T) { 209 reg := &httpmock.Registry{} 210 defer reg.Verify(t) 211 if tt.stubs == nil { 212 reg.Register( 213 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 214 httpmock.JSONResponse(payload), 215 ) 216 } else { 217 tt.stubs(reg) 218 } 219 220 tt.opts.HttpClient = func() (*http.Client, error) { 221 return &http.Client{Transport: reg}, nil 222 } 223 224 ios, _, stdout, stderr := iostreams.Test() 225 ios.SetStdoutTTY(tt.tty) 226 tt.opts.IO = ios 227 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 228 return ghrepo.FromFullName("OWNER/REPO") 229 } 230 231 err := listRun(tt.opts) 232 if tt.wantErr { 233 assert.Error(t, err) 234 } else { 235 assert.NoError(t, err) 236 } 237 238 assert.Equal(t, tt.wantOut, stdout.String()) 239 assert.Equal(t, tt.wantErrOut, stderr.String()) 240 }) 241 } 242 } 243 244 const longOutput = "flow 0\tactive\t0\nflow 1\tactive\t1\nflow 2\tactive\t2\nflow 3\tactive\t3\nflow 4\tactive\t4\nflow 5\tactive\t5\nflow 6\tactive\t6\nflow 7\tactive\t7\nflow 8\tactive\t8\nflow 9\tactive\t9\nflow 10\tactive\t10\nflow 11\tactive\t11\nflow 12\tactive\t12\nflow 13\tactive\t13\nflow 14\tactive\t14\nflow 15\tactive\t15\nflow 16\tactive\t16\nflow 17\tactive\t17\nflow 18\tactive\t18\nflow 19\tactive\t19\nflow 20\tactive\t20\nflow 21\tactive\t21\nflow 22\tactive\t22\nflow 23\tactive\t23\nflow 24\tactive\t24\nflow 25\tactive\t25\nflow 26\tactive\t26\nflow 27\tactive\t27\nflow 28\tactive\t28\nflow 29\tactive\t29\nflow 30\tactive\t30\nflow 31\tactive\t31\nflow 32\tactive\t32\nflow 33\tactive\t33\nflow 34\tactive\t34\nflow 35\tactive\t35\nflow 36\tactive\t36\nflow 37\tactive\t37\nflow 38\tactive\t38\nflow 39\tactive\t39\nflow 40\tactive\t40\nflow 41\tactive\t41\nflow 42\tactive\t42\nflow 43\tactive\t43\nflow 44\tactive\t44\nflow 45\tactive\t45\nflow 46\tactive\t46\nflow 47\tactive\t47\nflow 48\tactive\t48\nflow 49\tactive\t49\nflow 50\tactive\t50\nflow 51\tactive\t51\nflow 52\tactive\t52\nflow 53\tactive\t53\nflow 54\tactive\t54\nflow 55\tactive\t55\nflow 56\tactive\t56\nflow 57\tactive\t57\nflow 58\tactive\t58\nflow 59\tactive\t59\nflow 60\tactive\t60\nflow 61\tactive\t61\nflow 62\tactive\t62\nflow 63\tactive\t63\nflow 64\tactive\t64\nflow 65\tactive\t65\nflow 66\tactive\t66\nflow 67\tactive\t67\nflow 68\tactive\t68\nflow 69\tactive\t69\nflow 70\tactive\t70\nflow 71\tactive\t71\nflow 72\tactive\t72\nflow 73\tactive\t73\nflow 74\tactive\t74\nflow 75\tactive\t75\nflow 76\tactive\t76\nflow 77\tactive\t77\nflow 78\tactive\t78\nflow 79\tactive\t79\nflow 80\tactive\t80\nflow 81\tactive\t81\nflow 82\tactive\t82\nflow 83\tactive\t83\nflow 84\tactive\t84\nflow 85\tactive\t85\nflow 86\tactive\t86\nflow 87\tactive\t87\nflow 88\tactive\t88\nflow 89\tactive\t89\nflow 90\tactive\t90\nflow 91\tactive\t91\nflow 92\tactive\t92\nflow 93\tactive\t93\nflow 94\tactive\t94\nflow 95\tactive\t95\nflow 96\tactive\t96\nflow 97\tactive\t97\nflow 98\tactive\t98\nflow 99\tactive\t99\nflow 100\tactive\t100\n"