-
Notifications
You must be signed in to change notification settings - Fork 3
/
command_test.go
159 lines (153 loc) · 3.52 KB
/
command_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package task
import (
"context"
"fmt"
"sort"
"strings"
"testing"
)
func TestCommand(t *testing.T) {
showVar := ActionFunc(func(ctx context.Context, st *State, sc Script) error {
kk := make([]string, 0, len(st.bucket))
for k := range st.bucket {
kk = append(kk, k)
}
sort.Strings(kk)
for _, k := range kk {
fmt.Fprintf(st.Stdout, "var %s = %[2]v (%[2]T)\n", k, st.Get(k))
}
return nil
})
list := []struct {
Name string
Command *Command
Args string
ENV map[string]string
Output string
Error string
}{
{
Name: "default",
Command: &Command{
Name: "cmder",
Usage: "Example Commander",
Flags: []*Flag{
{Name: "f1", Usage: "set the current f1", Default: "ghi"},
{Name: "f2", Usage: "set the current f2", Default: "nmo"},
{Name: "f3", Usage: "set the current f3", Default: "fhg", ENV: "CMDER_F3"},
},
Action: showVar,
},
ENV: map[string]string{},
Args: "",
Output: `
var f1 = ghi (string)
var f2 = nmo (string)
var f3 = fhg (string)
`,
},
{
Name: "env",
Command: &Command{
Name: "cmder",
Usage: "Example Commander",
Flags: []*Flag{
{Name: "f1", Usage: "set the current f1", Default: "ghi"},
{Name: "f2", Usage: "set the current f2", Default: "nmo"},
{Name: "f3", Usage: "set the current f3", Default: "fhg", ENV: "CMDER_F3"},
},
Action: showVar,
},
ENV: map[string]string{
"CMDER_F3": "sky",
},
Args: "",
Output: `
var f1 = ghi (string)
var f2 = nmo (string)
var f3 = sky (string)
`,
},
{
Name: "override",
Command: &Command{
Name: "cmder",
Usage: "Example Commander",
Flags: []*Flag{
{Name: "f1", Usage: "set the current f1", Default: "ghi"},
{Name: "f2", Usage: "set the current f2", Default: "nmo"},
{Name: "f3", Usage: "set the current f3", Default: "fhg", ENV: "CMDER_F3"},
},
Action: showVar,
},
ENV: map[string]string{
"CMDER_F3": "sky",
},
Args: "-f3 box",
Output: `
var f1 = ghi (string)
var f2 = nmo (string)
var f3 = box (string)
`,
},
{
Name: "required-missing",
Command: &Command{
Name: "cmder",
Usage: "Example Commander",
Flags: []*Flag{
{Name: "f1", Usage: "set the current f1", Default: "ghi", Required: true},
{Name: "f2", Usage: "set the current f2", Default: "nmo"},
{Name: "f3", Usage: "set the current f3", Default: "fhg", ENV: "CMDER_F3", Required: true},
},
Action: showVar,
},
ENV: map[string]string{
"CMDER_F3": "sky",
},
Args: "-f2 box",
Error: `
flag "f1" required
cmder - Example Commander
-*f1 - set the current f1 (ghi)
-f2 - set the current f2 (nmo)
-*f3 [CMDER_F3] - set the current f3 (fhg)
`,
},
}
ts := strings.TrimSpace
for _, item := range list {
t.Run(item.Name, func(t *testing.T) {
ctx := context.Background()
stdout := &strings.Builder{}
stderr := &strings.Builder{}
st := &State{
Env: item.ENV,
Dir: t.TempDir(),
Stdout: stdout,
Stderr: stderr,
ErrorLogger: func(err error) {
t.Error(err)
},
MsgLogger: func(msg string) {
t.Log(msg)
},
}
if st.Env == nil {
st.Env = map[string]string{}
}
ff := strings.Fields(item.Args)
err := Run(ctx, st, item.Command.Exec(ff))
var gotErr string
if err != nil {
gotErr = err.Error()
}
if g, w := ts(gotErr), ts(item.Error); g != w {
t.Fatalf("error want:\n%s\n\ngot:\n%s\n", w, g)
}
if w, g := ts(item.Output), ts(stdout.String()); w != g {
t.Fatalf("output want:\n%s\n\ngot:\n%s\n", w, g)
}
})
}
}