github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/label/delete_test.go (about) 1 package label 2 3 import ( 4 "bytes" 5 "net/http" 6 "testing" 7 8 "github.com/ungtb10d/cli/v2/internal/ghrepo" 9 "github.com/ungtb10d/cli/v2/internal/prompter" 10 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 11 "github.com/ungtb10d/cli/v2/pkg/httpmock" 12 "github.com/ungtb10d/cli/v2/pkg/iostreams" 13 "github.com/google/shlex" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestNewCmdDelete(t *testing.T) { 18 tests := []struct { 19 name string 20 tty bool 21 input string 22 output deleteOptions 23 wantErr bool 24 wantErrMsg string 25 }{ 26 { 27 name: "no argument", 28 input: "", 29 wantErr: true, 30 wantErrMsg: "cannot delete label: name argument required", 31 }, 32 { 33 name: "name argument", 34 tty: true, 35 input: "test", 36 output: deleteOptions{Name: "test"}, 37 }, 38 { 39 name: "confirm argument", 40 input: "test --confirm", 41 output: deleteOptions{Name: "test", Confirmed: true}, 42 }, 43 { 44 name: "confirm no tty", 45 input: "test", 46 wantErr: true, 47 wantErrMsg: "--confirm required when not running interactively", 48 }, 49 } 50 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 io, _, _, _ := iostreams.Test() 54 f := &cmdutil.Factory{ 55 IOStreams: io, 56 } 57 io.SetStdinTTY(tt.tty) 58 io.SetStdoutTTY(tt.tty) 59 60 argv, err := shlex.Split(tt.input) 61 assert.NoError(t, err) 62 var gotOpts *deleteOptions 63 cmd := newCmdDelete(f, func(opts *deleteOptions) error { 64 gotOpts = opts 65 return nil 66 }) 67 cmd.SetArgs(argv) 68 cmd.SetIn(&bytes.Buffer{}) 69 cmd.SetOut(&bytes.Buffer{}) 70 cmd.SetErr(&bytes.Buffer{}) 71 72 _, err = cmd.ExecuteC() 73 if tt.wantErr { 74 assert.EqualError(t, err, tt.wantErrMsg) 75 return 76 } 77 78 assert.NoError(t, err) 79 assert.Equal(t, tt.output.Name, gotOpts.Name) 80 }) 81 } 82 } 83 84 func TestDeleteRun(t *testing.T) { 85 tests := []struct { 86 name string 87 tty bool 88 opts *deleteOptions 89 httpStubs func(*httpmock.Registry) 90 prompterStubs func(*prompter.PrompterMock) 91 wantStdout string 92 wantErr bool 93 errMsg string 94 }{ 95 { 96 name: "deletes label", 97 tty: true, 98 opts: &deleteOptions{Name: "test"}, 99 httpStubs: func(reg *httpmock.Registry) { 100 reg.Register( 101 httpmock.REST("DELETE", "repos/OWNER/REPO/labels/test"), 102 httpmock.StatusStringResponse(204, "{}"), 103 ) 104 }, 105 prompterStubs: func(pm *prompter.PrompterMock) { 106 pm.ConfirmDeletionFunc = func(_ string) error { 107 return nil 108 } 109 }, 110 wantStdout: "✓ Label \"test\" deleted from OWNER/REPO\n", 111 }, 112 { 113 name: "deletes label notty", 114 tty: false, 115 opts: &deleteOptions{Name: "test", Confirmed: true}, 116 httpStubs: func(reg *httpmock.Registry) { 117 reg.Register( 118 httpmock.REST("DELETE", "repos/OWNER/REPO/labels/test"), 119 httpmock.StatusStringResponse(204, "{}"), 120 ) 121 }, 122 wantStdout: "", 123 }, 124 { 125 name: "missing label", 126 tty: false, 127 opts: &deleteOptions{Name: "missing", Confirmed: true}, 128 httpStubs: func(reg *httpmock.Registry) { 129 reg.Register( 130 httpmock.REST("DELETE", "repos/OWNER/REPO/labels/missing"), 131 httpmock.WithHeader( 132 httpmock.StatusStringResponse(422, ` 133 { 134 "message":"Not Found" 135 }`), 136 "Content-Type", 137 "application/json", 138 ), 139 ) 140 }, 141 wantErr: true, 142 errMsg: "HTTP 422: Not Found (https://api.github.com/repos/OWNER/REPO/labels/missing)", 143 }, 144 } 145 146 for _, tt := range tests { 147 t.Run(tt.name, func(t *testing.T) { 148 reg := &httpmock.Registry{} 149 if tt.httpStubs != nil { 150 tt.httpStubs(reg) 151 } 152 tt.opts.HttpClient = func() (*http.Client, error) { 153 return &http.Client{Transport: reg}, nil 154 } 155 156 pm := &prompter.PrompterMock{} 157 if tt.prompterStubs != nil { 158 tt.prompterStubs(pm) 159 } 160 tt.opts.Prompter = pm 161 162 io, _, stdout, _ := iostreams.Test() 163 io.SetStdoutTTY(tt.tty) 164 io.SetStdinTTY(tt.tty) 165 io.SetStderrTTY(tt.tty) 166 tt.opts.IO = io 167 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 168 return ghrepo.New("OWNER", "REPO"), nil 169 } 170 defer reg.Verify(t) 171 err := deleteRun(tt.opts) 172 173 if tt.wantErr { 174 assert.EqualError(t, err, tt.errMsg) 175 return 176 } 177 178 assert.NoError(t, err) 179 assert.Equal(t, tt.wantStdout, stdout.String()) 180 }) 181 } 182 }