-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap_test.go
137 lines (123 loc) · 3.42 KB
/
wrap_test.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package nicecmd
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"os"
"reflect"
"strings"
"testing"
)
type TrivialConf struct {
Foo string
Bar []int
}
func trivialRun(cfg TrivialConf, cmd *cobra.Command, args []string) error {
if cfg.Foo == "foo" {
return nil
} else {
return fmt.Errorf(`expected cfg.Foo="foo", got %q`, cfg.Foo)
}
}
func TestCommand_Execute(t *testing.T) {
cmd := Command("TEST", Run(trivialRun), cobra.Command{Use: "test"}, TrivialConf{})
if reflect.ValueOf(cmd.Args).Pointer() != reflect.ValueOf(cobra.NoArgs).Pointer() {
t.Errorf("expected cmd to accept no args, to validator %p", cmd.Args)
}
cmd.SetArgs([]string{"--foo", "foo"})
if err := cmd.Execute(); err != nil {
t.Errorf("execute: %v", err)
}
}
func TestCommand_Sub(t *testing.T) {
type contextKey struct{}
rootMain := func(cfg TrivialConf, cmd *cobra.Command, args []string) error {
cmd.SetContext(context.WithValue(cmd.Context(), contextKey{}, cfg.Foo))
return nil
}
rootCmd := Command("TEST", PersistentPreRun(rootMain), cobra.Command{
Use: "root",
}, TrivialConf{
Foo: "foo default",
})
type SubConf struct {
Bar string
}
subHooksCalled := 0
subHook := func(cfg SubConf, cmd *cobra.Command, args []string) error {
subHooksCalled++
return nil
}
subMain := func(cfg SubConf, cmd *cobra.Command, args []string) error {
if foo, ok := cmd.Context().Value(contextKey{}).(string); !ok || foo != "foo" {
return errors.New("parent state did not propagate via context")
} else if len(args) != 1 || args[0] != "baz" {
return errors.New("did not get my baz as only arg")
} else if cfg.Bar != "bar" {
return fmt.Errorf(`expected non-default value --bar=bar, but got %q`, cfg.Bar)
} else {
return nil
}
}
rootCmd.AddCommand(Command("TEST", RunFuncs[SubConf]{
PersistentPreRun: subHook,
PreRun: subHook,
Run: subMain,
PostRun: subHook,
PersistentPostRun: subHook,
}, cobra.Command{
Use: "sub",
Args: cobra.ArbitraryArgs,
}, SubConf{
Bar: "bar default",
}))
rootCmd.SetArgs([]string{"--foo", "foo", "sub", "--bar", "bar", "baz"})
if err := rootCmd.Execute(); err != nil {
t.Errorf("execute: %v", err)
}
if subHooksCalled != 4 {
t.Errorf("expected 4 hooks to be called on sub-command, got %d", subHooksCalled)
}
}
func TestCommand_MissingUsage(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic")
} else if !strings.Contains(r.(string), "use line") {
t.Errorf("unexpected panic: %v", r)
}
}()
Command("", Run(trivialRun), cobra.Command{}, TrivialConf{})
}
func TestCommand_UsageAndExitOnBadConfig(t *testing.T) {
exitCalled := false
osExitOrTestHook = func(code int) {
exitCalled = true
}
defer func() { osExitOrTestHook = os.Exit }()
type EnvConfig struct {
Bad int
}
if err := os.Setenv("NICECMD_TESTCMD_BAD", "value"); err != nil {
t.Errorf("setenv: %v", err)
return
}
buf := &bytes.Buffer{}
cmdTemplate := cobra.Command{Use: "test"}
cmdTemplate.SetOut(buf)
run := func(cfg EnvConfig, cmd *cobra.Command, args []string) error {
return nil
}
cmd := Command("NICECMD_TESTCMD", Run(run), cmdTemplate, EnvConfig{})
if cmd != nil {
t.Error("expected Command to fail")
}
if !exitCalled {
t.Error("expected os.Exit to be called")
}
if out := buf.String(); !strings.Contains(out, "Usage:") {
t.Errorf("expected Command to print usage on invalid env, but got output: %v", out)
}
}