Skip to content

Commit d0d6b66

Browse files
committed
Add tests for FirstStringOf
1 parent dace1fc commit d0d6b66

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

flags/flags_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,122 @@ func TestParseFingerprintFormat(t *testing.T) {
230230
})
231231
}
232232
}
233+
234+
func TestFirstStringOf(t *testing.T) {
235+
getAppSet := func() (*cli.App, *flag.FlagSet) {
236+
app := &cli.App{}
237+
set := flag.NewFlagSet("contrive", 0)
238+
return app, set
239+
}
240+
tests := []struct {
241+
name string
242+
getContext func() *cli.Context
243+
inputs []string
244+
want string
245+
}{
246+
{
247+
name: "no-flags-empty",
248+
getContext: func() *cli.Context {
249+
app, set := getAppSet()
250+
//_ = set.String("ca-url", "", "")
251+
return cli.NewContext(app, set, nil)
252+
},
253+
inputs: []string{"foo", "bar"},
254+
want: "",
255+
},
256+
{
257+
name: "return-first-set-flag",
258+
getContext: func() *cli.Context {
259+
app, set := getAppSet()
260+
_ = set.String("foo", "", "")
261+
_ = set.String("bar", "", "")
262+
_ = set.String("baz", "", "")
263+
ctx := cli.NewContext(app, set, nil)
264+
ctx.Set("bar", "test1")
265+
ctx.Set("baz", "test2")
266+
return ctx
267+
},
268+
inputs: []string{"foo", "bar", "baz"},
269+
want: "test1",
270+
},
271+
{
272+
name: "return-first-default-flag",
273+
getContext: func() *cli.Context {
274+
app, set := getAppSet()
275+
_ = set.String("foo", "", "")
276+
_ = set.String("bar", "", "")
277+
_ = set.String("baz", "test1", "")
278+
ctx := cli.NewContext(app, set, nil)
279+
return ctx
280+
},
281+
inputs: []string{"foo", "bar", "baz"},
282+
want: "test1",
283+
},
284+
{
285+
name: "all-empty",
286+
getContext: func() *cli.Context {
287+
app, set := getAppSet()
288+
_ = set.String("foo", "", "")
289+
_ = set.String("bar", "", "")
290+
_ = set.String("baz", "", "")
291+
ctx := cli.NewContext(app, set, nil)
292+
return ctx
293+
},
294+
inputs: []string{"foo", "bar", "baz"},
295+
want: "",
296+
},
297+
/*
298+
{
299+
name: "negative",
300+
minLength: -5,
301+
promptRun: promptRunner([]string{"foobar"}, nil),
302+
want: "foobar",
303+
wantErr: false,
304+
},
305+
{
306+
name: "zero",
307+
minLength: 0,
308+
promptRun: promptRunner([]string{"foobar"}, nil),
309+
want: "foobar",
310+
wantErr: false,
311+
},
312+
{
313+
name: "greater-than-min-length",
314+
minLength: 5,
315+
promptRun: promptRunner([]string{"foobar"}, nil),
316+
want: "foobar",
317+
wantErr: false,
318+
},
319+
{
320+
name: "equal-min-length",
321+
minLength: 6,
322+
promptRun: promptRunner([]string{"foobar"}, nil),
323+
want: "foobar",
324+
wantErr: false,
325+
},
326+
{
327+
name: "less-than-min-length",
328+
minLength: 8,
329+
promptRun: promptRunner([]string{"pass", "foobar", "password"}, nil),
330+
want: "password",
331+
wantErr: false,
332+
},
333+
{
334+
name: "ignore-post-whitespace-characters",
335+
minLength: 7,
336+
promptRun: promptRunner([]string{"pass ", "foobar ", "password "}, nil),
337+
want: "password",
338+
wantErr: false,
339+
},
340+
*/
341+
}
342+
for _, tt := range tests {
343+
t.Run(tt.name, func(t *testing.T) {
344+
ctx := tt.getContext()
345+
val := FirstStringOf(ctx, tt.inputs...)
346+
if val != tt.want {
347+
t.Errorf("expected %v, but got %v", tt.want, val)
348+
}
349+
})
350+
}
351+
}

utils/cautils/bootstrap.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type bootstrapAPIResponse struct {
2828
CaURL string `json:"url"`
2929
Fingerprint string `json:"fingerprint"`
3030
RedirectURL string `json:"redirect-url"`
31-
Provisioner string `json:"issuer,omitempty"`
31+
Provisioner string `json:"provisioner,omitempty"`
3232
MinPasswordLength int `json:"min-password-length,omitempty"`
3333
}
3434

@@ -61,9 +61,9 @@ type bootstrapContext struct {
6161
minPasswordLength int
6262
}
6363

64-
func withProvisioner(issuer string) bootstrapOption {
64+
func withProvisioner(provisioner string) bootstrapOption {
6565
return func(bc *bootstrapContext) {
66-
bc.provisioner = issuer
66+
bc.provisioner = provisioner
6767
}
6868
}
6969

0 commit comments

Comments
 (0)