github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/label/edit_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/pkg/cmdutil" 10 "github.com/ungtb10d/cli/v2/pkg/httpmock" 11 "github.com/ungtb10d/cli/v2/pkg/iostreams" 12 "github.com/google/shlex" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestNewCmdEdit(t *testing.T) { 17 tests := []struct { 18 name string 19 input string 20 output editOptions 21 wantErr bool 22 errMsg string 23 }{ 24 { 25 name: "no argument", 26 input: "", 27 wantErr: true, 28 errMsg: "cannot update label: name argument required", 29 }, 30 { 31 name: "name argument", 32 input: "test", 33 wantErr: true, 34 errMsg: "specify at least one of `--color`, `--description`, or `--name`", 35 }, 36 { 37 name: "description flag", 38 input: "test --description 'some description'", 39 output: editOptions{Name: "test", Description: "some description"}, 40 }, 41 { 42 name: "color flag", 43 input: "test --color FFFFFF", 44 output: editOptions{Name: "test", Color: "FFFFFF"}, 45 }, 46 { 47 name: "color flag with pound sign", 48 input: "test --color '#AAAAAA'", 49 output: editOptions{Name: "test", Color: "AAAAAA"}, 50 }, 51 { 52 name: "name flag", 53 input: "test --name test1", 54 output: editOptions{Name: "test", NewName: "test1"}, 55 }, 56 } 57 58 for _, tt := range tests { 59 t.Run(tt.name, func(t *testing.T) { 60 io, _, _, _ := iostreams.Test() 61 f := &cmdutil.Factory{ 62 IOStreams: io, 63 } 64 argv, err := shlex.Split(tt.input) 65 assert.NoError(t, err) 66 var gotOpts *editOptions 67 cmd := newCmdEdit(f, func(opts *editOptions) error { 68 gotOpts = opts 69 return nil 70 }) 71 cmd.SetArgs(argv) 72 cmd.SetIn(&bytes.Buffer{}) 73 cmd.SetOut(&bytes.Buffer{}) 74 cmd.SetErr(&bytes.Buffer{}) 75 76 _, err = cmd.ExecuteC() 77 if tt.wantErr { 78 assert.EqualError(t, err, tt.errMsg) 79 return 80 } 81 82 assert.NoError(t, err) 83 assert.Equal(t, tt.output.Color, gotOpts.Color) 84 assert.Equal(t, tt.output.Description, gotOpts.Description) 85 assert.Equal(t, tt.output.Name, gotOpts.Name) 86 assert.Equal(t, tt.output.NewName, gotOpts.NewName) 87 }) 88 } 89 } 90 91 func TestEditRun(t *testing.T) { 92 tests := []struct { 93 name string 94 tty bool 95 opts *editOptions 96 httpStubs func(*httpmock.Registry) 97 wantStdout string 98 wantErrMsg string 99 }{ 100 { 101 name: "updates label", 102 tty: true, 103 opts: &editOptions{Name: "test", Description: "some description"}, 104 httpStubs: func(reg *httpmock.Registry) { 105 reg.Register( 106 httpmock.REST("PATCH", "repos/OWNER/REPO/labels/test"), 107 httpmock.StatusStringResponse(201, "{}"), 108 ) 109 }, 110 wantStdout: "✓ Label \"test\" updated in OWNER/REPO\n", 111 }, 112 { 113 name: "updates label notty", 114 tty: false, 115 opts: &editOptions{Name: "test", Description: "some description"}, 116 httpStubs: func(reg *httpmock.Registry) { 117 reg.Register( 118 httpmock.REST("PATCH", "repos/OWNER/REPO/labels/test"), 119 httpmock.StatusStringResponse(201, "{}"), 120 ) 121 }, 122 wantStdout: "", 123 }, 124 { 125 name: "updates missing label", 126 opts: &editOptions{Name: "invalid", Description: "some description"}, 127 httpStubs: func(reg *httpmock.Registry) { 128 reg.Register( 129 httpmock.REST("PATCH", "repos/OWNER/REPO/labels/invalid"), 130 httpmock.WithHeader( 131 httpmock.StatusStringResponse(404, `{"message":"Not Found"}`), 132 "Content-Type", 133 "application/json", 134 ), 135 ) 136 }, 137 wantErrMsg: "HTTP 404: Not Found (https://api.github.com/repos/OWNER/REPO/labels/invalid)", 138 }, 139 } 140 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 reg := &httpmock.Registry{} 144 if tt.httpStubs != nil { 145 tt.httpStubs(reg) 146 } 147 tt.opts.HttpClient = func() (*http.Client, error) { 148 return &http.Client{Transport: reg}, nil 149 } 150 io, _, stdout, _ := iostreams.Test() 151 io.SetStdoutTTY(tt.tty) 152 io.SetStdinTTY(tt.tty) 153 io.SetStderrTTY(tt.tty) 154 tt.opts.IO = io 155 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 156 return ghrepo.New("OWNER", "REPO"), nil 157 } 158 defer reg.Verify(t) 159 err := editRun(tt.opts) 160 161 if tt.wantErrMsg != "" { 162 assert.EqualError(t, err, tt.wantErrMsg) 163 } else { 164 assert.NoError(t, err) 165 } 166 167 assert.Equal(t, tt.wantStdout, stdout.String()) 168 }) 169 } 170 }