github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/workflow/enable/enable_test.go (about) 1 package enable 2 3 import ( 4 "bytes" 5 "io" 6 "net/http" 7 "testing" 8 9 "github.com/ungtb10d/cli/v2/internal/ghrepo" 10 "github.com/ungtb10d/cli/v2/pkg/cmd/workflow/shared" 11 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 12 "github.com/ungtb10d/cli/v2/pkg/httpmock" 13 "github.com/ungtb10d/cli/v2/pkg/iostreams" 14 "github.com/ungtb10d/cli/v2/pkg/prompt" 15 "github.com/google/shlex" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestNewCmdEnable(t *testing.T) { 20 tests := []struct { 21 name string 22 cli string 23 tty bool 24 wants EnableOptions 25 wantsErr bool 26 }{ 27 { 28 name: "blank tty", 29 tty: true, 30 wants: EnableOptions{ 31 Prompt: true, 32 }, 33 }, 34 { 35 name: "blank nontty", 36 wantsErr: true, 37 }, 38 { 39 name: "arg tty", 40 cli: "123", 41 tty: true, 42 wants: EnableOptions{ 43 Selector: "123", 44 }, 45 }, 46 { 47 name: "arg nontty", 48 cli: "123", 49 wants: EnableOptions{ 50 Selector: "123", 51 }, 52 }, 53 } 54 55 for _, tt := range tests { 56 t.Run(tt.name, func(t *testing.T) { 57 ios, _, _, _ := iostreams.Test() 58 ios.SetStdinTTY(tt.tty) 59 ios.SetStdoutTTY(tt.tty) 60 61 f := &cmdutil.Factory{ 62 IOStreams: ios, 63 } 64 65 argv, err := shlex.Split(tt.cli) 66 assert.NoError(t, err) 67 68 var gotOpts *EnableOptions 69 cmd := NewCmdEnable(f, func(opts *EnableOptions) error { 70 gotOpts = opts 71 return nil 72 }) 73 cmd.SetArgs(argv) 74 cmd.SetIn(&bytes.Buffer{}) 75 cmd.SetOut(io.Discard) 76 cmd.SetErr(io.Discard) 77 78 _, err = cmd.ExecuteC() 79 if tt.wantsErr { 80 assert.Error(t, err) 81 return 82 } 83 84 assert.NoError(t, err) 85 86 assert.Equal(t, tt.wants.Selector, gotOpts.Selector) 87 assert.Equal(t, tt.wants.Prompt, gotOpts.Prompt) 88 }) 89 } 90 } 91 92 func TestEnableRun(t *testing.T) { 93 tests := []struct { 94 name string 95 opts *EnableOptions 96 httpStubs func(*httpmock.Registry) 97 askStubs func(*prompt.AskStubber) 98 tty bool 99 wantOut string 100 wantErrOut string 101 wantErr bool 102 }{ 103 { 104 name: "tty no arg", 105 opts: &EnableOptions{ 106 Prompt: true, 107 }, 108 tty: true, 109 httpStubs: func(reg *httpmock.Registry) { 110 reg.Register( 111 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 112 httpmock.JSONResponse(shared.WorkflowsPayload{ 113 Workflows: []shared.Workflow{ 114 shared.AWorkflow, 115 shared.DisabledWorkflow, 116 shared.AnotherWorkflow, 117 }, 118 })) 119 reg.Register( 120 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"), 121 httpmock.StatusStringResponse(204, "{}")) 122 }, 123 askStubs: func(as *prompt.AskStubber) { 124 as.StubPrompt("Select a workflow").AnswerWith("a disabled workflow (disabled.yml)") 125 }, 126 wantOut: "✓ Enabled a disabled workflow\n", 127 }, 128 { 129 name: "tty name arg", 130 opts: &EnableOptions{ 131 Selector: "terrible workflow", 132 }, 133 tty: true, 134 httpStubs: func(reg *httpmock.Registry) { 135 reg.Register( 136 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 137 httpmock.JSONResponse(shared.WorkflowsPayload{ 138 Workflows: []shared.Workflow{ 139 shared.AWorkflow, 140 shared.DisabledWorkflow, 141 shared.UniqueDisabledWorkflow, 142 shared.AnotherWorkflow, 143 }, 144 })) 145 reg.Register( 146 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1314/enable"), 147 httpmock.StatusStringResponse(204, "{}")) 148 }, 149 wantOut: "✓ Enabled terrible workflow\n", 150 }, 151 { 152 name: "tty name arg nonunique", 153 opts: &EnableOptions{ 154 Selector: "a disabled workflow", 155 }, 156 tty: true, 157 httpStubs: func(reg *httpmock.Registry) { 158 reg.Register( 159 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 160 httpmock.JSONResponse(shared.WorkflowsPayload{ 161 Workflows: []shared.Workflow{ 162 shared.AWorkflow, 163 shared.DisabledWorkflow, 164 shared.AnotherWorkflow, 165 shared.AnotherDisabledWorkflow, 166 }, 167 })) 168 reg.Register( 169 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1213/enable"), 170 httpmock.StatusStringResponse(204, "{}")) 171 }, 172 askStubs: func(as *prompt.AskStubber) { 173 as.StubPrompt("Which workflow do you mean?").AnswerWith("a disabled workflow (anotherDisabled.yml)") 174 }, 175 wantOut: "✓ Enabled a disabled workflow\n", 176 }, 177 { 178 name: "tty name arg inactivity workflow", 179 opts: &EnableOptions{ 180 Selector: "a disabled inactivity workflow", 181 }, 182 tty: true, 183 httpStubs: func(reg *httpmock.Registry) { 184 reg.Register( 185 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 186 httpmock.JSONResponse(shared.WorkflowsPayload{ 187 Workflows: []shared.Workflow{ 188 shared.AWorkflow, 189 shared.DisabledInactivityWorkflow, 190 shared.UniqueDisabledWorkflow, 191 shared.AnotherWorkflow, 192 }, 193 })) 194 reg.Register( 195 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1206/enable"), 196 httpmock.StatusStringResponse(204, "{}")) 197 }, 198 wantOut: "✓ Enabled a disabled inactivity workflow\n", 199 }, 200 { 201 name: "tty ID arg", 202 opts: &EnableOptions{ 203 Selector: "456", 204 }, 205 tty: true, 206 httpStubs: func(reg *httpmock.Registry) { 207 reg.Register( 208 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/456"), 209 httpmock.JSONResponse(shared.DisabledWorkflow)) 210 reg.Register( 211 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"), 212 httpmock.StatusStringResponse(204, "{}")) 213 }, 214 wantOut: "✓ Enabled a disabled workflow\n", 215 }, 216 { 217 name: "nontty ID arg", 218 opts: &EnableOptions{ 219 Selector: "456", 220 }, 221 httpStubs: func(reg *httpmock.Registry) { 222 reg.Register( 223 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/456"), 224 httpmock.JSONResponse(shared.DisabledWorkflow)) 225 reg.Register( 226 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"), 227 httpmock.StatusStringResponse(204, "{}")) 228 }, 229 }, 230 { 231 name: "nontty name arg", 232 opts: &EnableOptions{ 233 Selector: "terrible workflow", 234 }, 235 httpStubs: func(reg *httpmock.Registry) { 236 reg.Register( 237 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 238 httpmock.JSONResponse(shared.WorkflowsPayload{ 239 Workflows: []shared.Workflow{ 240 shared.AWorkflow, 241 shared.DisabledWorkflow, 242 shared.AnotherWorkflow, 243 shared.AnotherDisabledWorkflow, 244 shared.UniqueDisabledWorkflow, 245 }, 246 })) 247 reg.Register( 248 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1314/enable"), 249 httpmock.StatusStringResponse(204, "{}")) 250 }, 251 }, 252 { 253 name: "nontty name arg inactivity workflow", 254 opts: &EnableOptions{ 255 Selector: "a disabled inactivity workflow", 256 }, 257 httpStubs: func(reg *httpmock.Registry) { 258 reg.Register( 259 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 260 httpmock.JSONResponse(shared.WorkflowsPayload{ 261 Workflows: []shared.Workflow{ 262 shared.AWorkflow, 263 shared.DisabledInactivityWorkflow, 264 shared.UniqueDisabledWorkflow, 265 shared.AnotherWorkflow, 266 }, 267 })) 268 reg.Register( 269 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1206/enable"), 270 httpmock.StatusStringResponse(204, "{}")) 271 }, 272 }, 273 { 274 name: "nontty name arg nonunique", 275 opts: &EnableOptions{ 276 Selector: "a disabled workflow", 277 }, 278 httpStubs: func(reg *httpmock.Registry) { 279 reg.Register( 280 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 281 httpmock.JSONResponse(shared.WorkflowsPayload{ 282 Workflows: []shared.Workflow{ 283 shared.AWorkflow, 284 shared.DisabledWorkflow, 285 shared.AnotherWorkflow, 286 shared.AnotherDisabledWorkflow, 287 shared.UniqueDisabledWorkflow, 288 }, 289 })) 290 }, 291 wantErr: true, 292 wantErrOut: "could not resolve to a unique workflow; found: disabled.yml anotherDisabled.yml", 293 }, 294 } 295 296 for _, tt := range tests { 297 reg := &httpmock.Registry{} 298 tt.httpStubs(reg) 299 tt.opts.HttpClient = func() (*http.Client, error) { 300 return &http.Client{Transport: reg}, nil 301 } 302 303 ios, _, stdout, _ := iostreams.Test() 304 ios.SetStdoutTTY(tt.tty) 305 ios.SetStdinTTY(tt.tty) 306 tt.opts.IO = ios 307 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 308 return ghrepo.FromFullName("OWNER/REPO") 309 } 310 311 t.Run(tt.name, func(t *testing.T) { 312 //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock 313 as := prompt.NewAskStubber(t) 314 if tt.askStubs != nil { 315 tt.askStubs(as) 316 } 317 318 err := runEnable(tt.opts) 319 if tt.wantErr { 320 assert.Error(t, err) 321 assert.Equal(t, tt.wantErrOut, err.Error()) 322 return 323 } 324 assert.NoError(t, err) 325 assert.Equal(t, tt.wantOut, stdout.String()) 326 reg.Verify(t) 327 }) 328 } 329 }