|
| 1 | +package cancel |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cli/cli/v2/internal/ghrepo" |
| 10 | + "github.com/cli/cli/v2/pkg/cmd/run/shared" |
| 11 | + "github.com/cli/cli/v2/pkg/cmdutil" |
| 12 | + "github.com/cli/cli/v2/pkg/httpmock" |
| 13 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 14 | + "github.com/cli/cli/v2/pkg/prompt" |
| 15 | + "github.com/google/shlex" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestNewCmdCancel(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + cli string |
| 23 | + tty bool |
| 24 | + wants CancelOptions |
| 25 | + wantsErr bool |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "blank tty", |
| 29 | + tty: true, |
| 30 | + wants: CancelOptions{ |
| 31 | + Prompt: true, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "blank nontty", |
| 36 | + wantsErr: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "with arg", |
| 40 | + cli: "1234", |
| 41 | + wants: CancelOptions{ |
| 42 | + RunID: "1234", |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + io, _, _, _ := iostreams.Test() |
| 50 | + io.SetStdinTTY(tt.tty) |
| 51 | + io.SetStdoutTTY(tt.tty) |
| 52 | + |
| 53 | + f := &cmdutil.Factory{ |
| 54 | + IOStreams: io, |
| 55 | + } |
| 56 | + |
| 57 | + argv, err := shlex.Split(tt.cli) |
| 58 | + assert.NoError(t, err) |
| 59 | + |
| 60 | + var gotOpts *CancelOptions |
| 61 | + cmd := NewCmdCancel(f, func(opts *CancelOptions) error { |
| 62 | + gotOpts = opts |
| 63 | + return nil |
| 64 | + }) |
| 65 | + |
| 66 | + cmd.SetArgs(argv) |
| 67 | + cmd.SetIn(&bytes.Buffer{}) |
| 68 | + cmd.SetOut(ioutil.Discard) |
| 69 | + cmd.SetErr(ioutil.Discard) |
| 70 | + |
| 71 | + _, err = cmd.ExecuteC() |
| 72 | + if tt.wantsErr { |
| 73 | + assert.Error(t, err) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + assert.NoError(t, err) |
| 78 | + |
| 79 | + assert.Equal(t, tt.wants.RunID, gotOpts.RunID) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestRunCancel(t *testing.T) { |
| 85 | + inProgressRun := shared.TestRun("more runs", 1234, shared.InProgress, "") |
| 86 | + completedRun := shared.TestRun("more runs", 4567, shared.Completed, shared.Failure) |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + httpStubs func(*httpmock.Registry) |
| 90 | + askStubs func(*prompt.AskStubber) |
| 91 | + opts *CancelOptions |
| 92 | + wantErr bool |
| 93 | + wantOut string |
| 94 | + errMsg string |
| 95 | + }{ |
| 96 | + { |
| 97 | + name: "cancel run", |
| 98 | + opts: &CancelOptions{ |
| 99 | + RunID: "1234", |
| 100 | + }, |
| 101 | + wantErr: false, |
| 102 | + httpStubs: func(reg *httpmock.Registry) { |
| 103 | + reg.Register( |
| 104 | + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/1234"), |
| 105 | + httpmock.JSONResponse(inProgressRun)) |
| 106 | + reg.Register( |
| 107 | + httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/1234/cancel"), |
| 108 | + httpmock.StatusStringResponse(202, "{}")) |
| 109 | + }, |
| 110 | + wantOut: "✓ Request to cancel workflow submitted.\n", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "not found", |
| 114 | + opts: &CancelOptions{ |
| 115 | + RunID: "1234", |
| 116 | + }, |
| 117 | + wantErr: true, |
| 118 | + errMsg: "Could not find any workflow run with ID 1234", |
| 119 | + httpStubs: func(reg *httpmock.Registry) { |
| 120 | + reg.Register( |
| 121 | + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/1234"), |
| 122 | + httpmock.StatusStringResponse(404, "")) |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "completed", |
| 127 | + opts: &CancelOptions{ |
| 128 | + RunID: "4567", |
| 129 | + }, |
| 130 | + wantErr: true, |
| 131 | + errMsg: "Cannot cancel a workflow run that is completed", |
| 132 | + httpStubs: func(reg *httpmock.Registry) { |
| 133 | + reg.Register( |
| 134 | + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/4567"), |
| 135 | + httpmock.JSONResponse(completedRun)) |
| 136 | + reg.Register( |
| 137 | + httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/4567/cancel"), |
| 138 | + httpmock.StatusStringResponse(409, ""), |
| 139 | + ) |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "prompt, no in progress runs", |
| 144 | + opts: &CancelOptions{ |
| 145 | + Prompt: true, |
| 146 | + }, |
| 147 | + wantErr: true, |
| 148 | + errMsg: "found no in progress runs to cancel", |
| 149 | + httpStubs: func(reg *httpmock.Registry) { |
| 150 | + reg.Register( |
| 151 | + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs"), |
| 152 | + httpmock.JSONResponse(shared.RunsPayload{ |
| 153 | + WorkflowRuns: []shared.Run{ |
| 154 | + completedRun, |
| 155 | + }, |
| 156 | + })) |
| 157 | + }, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "prompt, cancel", |
| 161 | + opts: &CancelOptions{ |
| 162 | + Prompt: true, |
| 163 | + }, |
| 164 | + httpStubs: func(reg *httpmock.Registry) { |
| 165 | + reg.Register( |
| 166 | + httpmock.REST("GET", "repos/OWNER/REPO/actions/runs"), |
| 167 | + httpmock.JSONResponse(shared.RunsPayload{ |
| 168 | + WorkflowRuns: []shared.Run{ |
| 169 | + inProgressRun, |
| 170 | + }, |
| 171 | + })) |
| 172 | + reg.Register( |
| 173 | + httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/1234/cancel"), |
| 174 | + httpmock.StatusStringResponse(202, "{}")) |
| 175 | + }, |
| 176 | + askStubs: func(as *prompt.AskStubber) { |
| 177 | + as.StubOne(0) |
| 178 | + }, |
| 179 | + wantOut: "✓ Request to cancel workflow submitted.\n", |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + for _, tt := range tests { |
| 184 | + reg := &httpmock.Registry{} |
| 185 | + tt.httpStubs(reg) |
| 186 | + tt.opts.HttpClient = func() (*http.Client, error) { |
| 187 | + return &http.Client{Transport: reg}, nil |
| 188 | + } |
| 189 | + |
| 190 | + io, _, stdout, _ := iostreams.Test() |
| 191 | + io.SetStdoutTTY(true) |
| 192 | + io.SetStdinTTY(true) |
| 193 | + tt.opts.IO = io |
| 194 | + tt.opts.BaseRepo = func() (ghrepo.Interface, error) { |
| 195 | + return ghrepo.FromFullName("OWNER/REPO") |
| 196 | + } |
| 197 | + |
| 198 | + as, teardown := prompt.InitAskStubber() |
| 199 | + defer teardown() |
| 200 | + if tt.askStubs != nil { |
| 201 | + tt.askStubs(as) |
| 202 | + } |
| 203 | + |
| 204 | + t.Run(tt.name, func(t *testing.T) { |
| 205 | + err := runCancel(tt.opts) |
| 206 | + if tt.wantErr { |
| 207 | + assert.Error(t, err) |
| 208 | + if tt.errMsg != "" { |
| 209 | + assert.Equal(t, tt.errMsg, err.Error()) |
| 210 | + } |
| 211 | + } else { |
| 212 | + assert.NoError(t, err) |
| 213 | + } |
| 214 | + assert.Equal(t, tt.wantOut, stdout.String()) |
| 215 | + reg.Verify(t) |
| 216 | + }) |
| 217 | + } |
| 218 | +} |
0 commit comments