11package delete
22
33import (
4+ "bytes"
45 "net/http"
56 "testing"
67
8+ "github.com/cli/cli/v2/pkg/cmdutil"
79 "github.com/cli/cli/v2/pkg/httpmock"
810 "github.com/cli/cli/v2/pkg/iostreams"
911 "github.com/cli/cli/v2/pkg/prompt"
12+ "github.com/google/shlex"
1013 "github.com/stretchr/testify/assert"
1114)
1215
16+ func TestNewCmdDelete (t * testing.T ) {
17+ tests := []struct {
18+ name string
19+ input string
20+ tty bool
21+ output DeleteOptions
22+ wantErr bool
23+ errMsg string
24+ }{
25+ {
26+ name : "confirm flag" ,
27+ input : "OWNER/REPO --confirm" ,
28+ output : DeleteOptions {RepoArg : "OWNER/REPO" , Confirmed : true },
29+ },
30+ {
31+ name : "no confirmation no tty" ,
32+ input : "OWNER/REPO" ,
33+ output : DeleteOptions {RepoArg : "OWNER/REPO" },
34+ wantErr : true ,
35+ errMsg : "could not prompt: confirmation with prompt or --confirm flag required" },
36+ {
37+ name : "no argument" ,
38+ input : "" ,
39+ wantErr : true ,
40+ errMsg : "cannot delete: repository argument required" ,
41+ tty : true ,
42+ },
43+ }
44+ for _ , tt := range tests {
45+ t .Run (tt .name , func (t * testing.T ) {
46+ io , _ , _ , _ := iostreams .Test ()
47+ io .SetStdinTTY (tt .tty )
48+ io .SetStdoutTTY (tt .tty )
49+ f := & cmdutil.Factory {
50+ IOStreams : io ,
51+ }
52+ argv , err := shlex .Split (tt .input )
53+ assert .NoError (t , err )
54+ var gotOpts * DeleteOptions
55+ cmd := NewCmdDelete (f , func (opts * DeleteOptions ) error {
56+ gotOpts = opts
57+ return nil
58+ })
59+ cmd .SetArgs (argv )
60+ cmd .SetIn (& bytes.Buffer {})
61+ cmd .SetOut (& bytes.Buffer {})
62+ cmd .SetErr (& bytes.Buffer {})
63+
64+ _ , err = cmd .ExecuteC ()
65+ if tt .wantErr {
66+ assert .Error (t , err )
67+ assert .Equal (t , tt .errMsg , err .Error ())
68+ return
69+ }
70+ assert .NoError (t , err )
71+ assert .Equal (t , tt .output .RepoArg , gotOpts .RepoArg )
72+ })
73+ }
74+ }
75+
1376func Test_deleteRun (t * testing.T ) {
1477 tests := []struct {
1578 name string
@@ -27,7 +90,7 @@ func Test_deleteRun(t *testing.T) {
2790 opts : & DeleteOptions {RepoArg : "OWNER/REPO" },
2891 wantStdout : "✓ Deleted repository OWNER/REPO\n " ,
2992 askStubs : func (q * prompt.AskStubber ) {
30- // TODO: survey stubber doesn't have WithValidation support
93+ // TODO: survey stubber doesn't have WithValidator support
3194 // so this always passes regardless of prompt input
3295 q .StubOne ("OWNER/REPO" )
3396 },
@@ -49,12 +112,6 @@ func Test_deleteRun(t *testing.T) {
49112 httpmock .StatusStringResponse (204 , "{}" ))
50113 },
51114 },
52- {
53- name : "no confirmation no tty" ,
54- opts : & DeleteOptions {RepoArg : "OWNER/REPO" },
55- wantErr : true ,
56- errMsg : "could not prompt: confirmation with prompt or --confirm flag required" ,
57- },
58115 {
59116 name : "short repo name" ,
60117 opts : & DeleteOptions {RepoArg : "REPO" },
0 commit comments