-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_init_test.go
More file actions
54 lines (45 loc) · 1.17 KB
/
command_init_test.go
File metadata and controls
54 lines (45 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package redant
import "testing"
func TestCommandInitIsIdempotentForGlobalFlags(t *testing.T) {
root := &Command{Use: "app"}
if err := root.init(); err != nil {
t.Fatalf("first init failed: %v", err)
}
if err := root.init(); err != nil {
t.Fatalf("second init failed: %v", err)
}
counts := map[string]int{}
for _, opt := range root.Options {
if opt.Flag == "" {
continue
}
counts[opt.Flag]++
}
for _, flag := range []string{"help", "list-commands", "list-flags", "env", "env-file", internalArgsOverrideFlag} {
if counts[flag] != 1 {
t.Fatalf("expected global flag %q exactly once, got %d", flag, counts[flag])
}
}
globals := root.GetGlobalFlags()
_ = globals.FlagSet(root.Name())
}
func TestCommandInitDoesNotOverrideExistingRootGlobalFlag(t *testing.T) {
root := &Command{
Use: "app",
Options: OptionSet{
{Flag: "env", Description: "custom env", Value: StringArrayOf(new([]string))},
},
}
if err := root.init(); err != nil {
t.Fatalf("init failed: %v", err)
}
envCount := 0
for _, opt := range root.Options {
if opt.Flag == "env" {
envCount++
}
}
if envCount != 1 {
t.Fatalf("expected env flag exactly once, got %d", envCount)
}
}