77 "testing"
88
99 "github.com/cli/cli/v2/internal/ghrepo"
10+ "github.com/cli/cli/v2/pkg/cmd/run/shared"
1011 "github.com/cli/cli/v2/pkg/cmdutil"
1112 "github.com/cli/cli/v2/pkg/httpmock"
1213 "github.com/cli/cli/v2/pkg/iostreams"
@@ -18,11 +19,19 @@ func TestNewCmdCancel(t *testing.T) {
1819 tests := []struct {
1920 name string
2021 cli string
22+ tty bool
2123 wants CancelOptions
2224 wantsErr bool
2325 }{
2426 {
25- name : "blank" ,
27+ name : "blank tty" ,
28+ tty : true ,
29+ wants : CancelOptions {
30+ Prompt : true ,
31+ },
32+ },
33+ {
34+ name : "blank nontty" ,
2635 wantsErr : true ,
2736 },
2837 {
@@ -37,8 +46,8 @@ func TestNewCmdCancel(t *testing.T) {
3746 for _ , tt := range tests {
3847 t .Run (tt .name , func (t * testing.T ) {
3948 io , _ , _ , _ := iostreams .Test ()
40- io .SetStdinTTY (true )
41- io .SetStdoutTTY (true )
49+ io .SetStdinTTY (tt . tty )
50+ io .SetStdoutTTY (tt . tty )
4251
4352 f := & cmdutil.Factory {
4453 IOStreams : io ,
@@ -72,6 +81,8 @@ func TestNewCmdCancel(t *testing.T) {
7281}
7382
7483func TestRunCancel (t * testing.T ) {
84+ inProgressRun := shared .TestRun ("more runs" , 1234 , shared .InProgress , "" )
85+ completedRun := shared .TestRun ("more runs" , 4567 , shared .Completed , shared .Failure )
7586 tests := []struct {
7687 name string
7788 httpStubs func (* httpmock.Registry )
@@ -87,12 +98,15 @@ func TestRunCancel(t *testing.T) {
8798 },
8899 wantErr : false ,
89100 httpStubs : func (reg * httpmock.Registry ) {
101+ reg .Register (
102+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/runs/1234" ),
103+ httpmock .JSONResponse (inProgressRun ))
90104 reg .Register (
91105 httpmock .REST ("POST" , "repos/OWNER/REPO/actions/runs/1234/cancel" ),
92106 httpmock .StatusStringResponse (202 , "{}" ),
93107 )
94108 },
95- wantOut : "✓ You have successfully requested the workflow to be canceled. " ,
109+ wantOut : "✓ Request to cancel workflow submitted. \n " ,
96110 },
97111 {
98112 name : "not found" ,
@@ -103,21 +117,23 @@ func TestRunCancel(t *testing.T) {
103117 errMsg : "Could not find any workflow run with ID 1234" ,
104118 httpStubs : func (reg * httpmock.Registry ) {
105119 reg .Register (
106- httpmock .REST ("POST" , "repos/OWNER/REPO/actions/runs/1234/cancel" ),
107- httpmock .StatusStringResponse (404 , "" ),
108- )
120+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/runs/1234" ),
121+ httpmock .StatusStringResponse (404 , "" ))
109122 },
110123 },
111124 {
112125 name : "completed" ,
113126 opts : & CancelOptions {
114- RunID : "1234 " ,
127+ RunID : "4567 " ,
115128 },
116129 wantErr : true ,
117130 errMsg : "Cannot cancel a workflow run that is completed" ,
118131 httpStubs : func (reg * httpmock.Registry ) {
119132 reg .Register (
120- httpmock .REST ("POST" , "repos/OWNER/REPO/actions/runs/1234/cancel" ),
133+ httpmock .REST ("GET" , "repos/OWNER/REPO/actions/runs/4567" ),
134+ httpmock .JSONResponse (completedRun ))
135+ reg .Register (
136+ httpmock .REST ("POST" , "repos/OWNER/REPO/actions/runs/4567/cancel" ),
121137 httpmock .StatusStringResponse (409 , "" ),
122138 )
123139 },
@@ -133,6 +149,7 @@ func TestRunCancel(t *testing.T) {
133149
134150 io , _ , stdout , _ := iostreams .Test ()
135151 io .SetStdoutTTY (true )
152+ io .SetStdinTTY (true )
136153 tt .opts .IO = io
137154 tt .opts .BaseRepo = func () (ghrepo.Interface , error ) {
138155 return ghrepo .FromFullName ("OWNER/REPO" )
@@ -145,6 +162,8 @@ func TestRunCancel(t *testing.T) {
145162 if tt .errMsg != "" {
146163 assert .Equal (t , tt .errMsg , err .Error ())
147164 }
165+ } else {
166+ assert .NoError (t , err )
148167 }
149168 assert .Equal (t , tt .wantOut , stdout .String ())
150169 reg .Verify (t )
0 commit comments