github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/workflow/enable/enable_test.go (about) 1 package enable 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "testing" 8 9 "github.com/cli/cli/internal/ghrepo" 10 "github.com/cli/cli/pkg/cmd/workflow/shared" 11 "github.com/cli/cli/pkg/cmdutil" 12 "github.com/cli/cli/pkg/httpmock" 13 "github.com/cli/cli/pkg/iostreams" 14 "github.com/cli/cli/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 io, _, _, _ := iostreams.Test() 58 io.SetStdinTTY(tt.tty) 59 io.SetStdoutTTY(tt.tty) 60 61 f := &cmdutil.Factory{ 62 IOStreams: io, 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(ioutil.Discard) 76 cmd.SetErr(ioutil.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.StubOne(0) 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/terrible workflow"), 137 httpmock.StatusStringResponse(404, "not found")) 138 reg.Register( 139 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 140 httpmock.JSONResponse(shared.WorkflowsPayload{ 141 Workflows: []shared.Workflow{ 142 shared.AWorkflow, 143 shared.DisabledWorkflow, 144 shared.UniqueDisabledWorkflow, 145 shared.AnotherWorkflow, 146 }, 147 })) 148 reg.Register( 149 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1314/enable"), 150 httpmock.StatusStringResponse(204, "{}")) 151 }, 152 wantOut: "✓ Enabled terrible workflow\n", 153 }, 154 { 155 name: "tty name arg nonunique", 156 opts: &EnableOptions{ 157 Selector: "a disabled workflow", 158 }, 159 tty: true, 160 httpStubs: func(reg *httpmock.Registry) { 161 reg.Register( 162 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/a disabled workflow"), 163 httpmock.StatusStringResponse(404, "not found")) 164 reg.Register( 165 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 166 httpmock.JSONResponse(shared.WorkflowsPayload{ 167 Workflows: []shared.Workflow{ 168 shared.AWorkflow, 169 shared.DisabledWorkflow, 170 shared.AnotherWorkflow, 171 shared.AnotherDisabledWorkflow, 172 }, 173 })) 174 reg.Register( 175 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1213/enable"), 176 httpmock.StatusStringResponse(204, "{}")) 177 }, 178 askStubs: func(as *prompt.AskStubber) { 179 as.StubOne(1) 180 }, 181 wantOut: "✓ Enabled a disabled workflow\n", 182 }, 183 { 184 name: "tty ID arg", 185 opts: &EnableOptions{ 186 Selector: "456", 187 }, 188 tty: true, 189 httpStubs: func(reg *httpmock.Registry) { 190 reg.Register( 191 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/456"), 192 httpmock.JSONResponse(shared.DisabledWorkflow)) 193 reg.Register( 194 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"), 195 httpmock.StatusStringResponse(204, "{}")) 196 }, 197 askStubs: func(as *prompt.AskStubber) { 198 as.StubOne(0) 199 }, 200 wantOut: "✓ Enabled a disabled workflow\n", 201 }, 202 { 203 name: "nontty ID arg", 204 opts: &EnableOptions{ 205 Selector: "456", 206 }, 207 httpStubs: func(reg *httpmock.Registry) { 208 reg.Register( 209 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/456"), 210 httpmock.JSONResponse(shared.DisabledWorkflow)) 211 reg.Register( 212 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"), 213 httpmock.StatusStringResponse(204, "{}")) 214 }, 215 }, 216 { 217 name: "nontty name arg", 218 opts: &EnableOptions{ 219 Selector: "terrible workflow", 220 }, 221 httpStubs: func(reg *httpmock.Registry) { 222 reg.Register( 223 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/terrible workflow"), 224 httpmock.StatusStringResponse(404, "not found")) 225 reg.Register( 226 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 227 httpmock.JSONResponse(shared.WorkflowsPayload{ 228 Workflows: []shared.Workflow{ 229 shared.AWorkflow, 230 shared.DisabledWorkflow, 231 shared.AnotherWorkflow, 232 shared.AnotherDisabledWorkflow, 233 shared.UniqueDisabledWorkflow, 234 }, 235 })) 236 reg.Register( 237 httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/1314/enable"), 238 httpmock.StatusStringResponse(204, "{}")) 239 }, 240 }, 241 { 242 name: "nontty name arg nonunique", 243 opts: &EnableOptions{ 244 Selector: "a disabled workflow", 245 }, 246 httpStubs: func(reg *httpmock.Registry) { 247 reg.Register( 248 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/a disabled workflow"), 249 httpmock.StatusStringResponse(404, "not found")) 250 reg.Register( 251 httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"), 252 httpmock.JSONResponse(shared.WorkflowsPayload{ 253 Workflows: []shared.Workflow{ 254 shared.AWorkflow, 255 shared.DisabledWorkflow, 256 shared.AnotherWorkflow, 257 shared.AnotherDisabledWorkflow, 258 shared.UniqueDisabledWorkflow, 259 }, 260 })) 261 }, 262 wantErr: true, 263 wantErrOut: "could not resolve to a unique workflow; found: disabled.yml anotherDisabled.yml", 264 }, 265 } 266 267 for _, tt := range tests { 268 reg := &httpmock.Registry{} 269 tt.httpStubs(reg) 270 tt.opts.HttpClient = func() (*http.Client, error) { 271 return &http.Client{Transport: reg}, nil 272 } 273 274 io, _, stdout, _ := iostreams.Test() 275 io.SetStdoutTTY(tt.tty) 276 io.SetStdinTTY(tt.tty) 277 tt.opts.IO = io 278 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 279 return ghrepo.FromFullName("OWNER/REPO") 280 } 281 282 as, teardown := prompt.InitAskStubber() 283 defer teardown() 284 if tt.askStubs != nil { 285 tt.askStubs(as) 286 } 287 288 t.Run(tt.name, func(t *testing.T) { 289 err := runEnable(tt.opts) 290 if tt.wantErr { 291 assert.Error(t, err) 292 assert.Equal(t, tt.wantErrOut, err.Error()) 293 return 294 } 295 assert.NoError(t, err) 296 assert.Equal(t, tt.wantOut, stdout.String()) 297 reg.Verify(t) 298 }) 299 } 300 }