-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli_test.go
169 lines (162 loc) · 4.47 KB
/
cli_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
160
161
162
163
164
165
166
167
168
169
package cli
import (
"bytes"
"io"
"regexp"
"strings"
"testing"
"github.com/spf13/pflag"
)
func Test_state_run(t *testing.T) {
t.Parallel()
tests := []struct {
name string
giveInput string
giveArgs []string
wantOutput string
wantErrorMatch string
}{
{
name: "parse arg",
giveInput: `{"fruits": {"mango": "yummy"}}`,
giveArgs: []string{"{{.fruits.mango}}"},
wantOutput: "yummy\n",
},
{
name: "parse file",
giveInput: `{"fruits": {"mango": "yummy"}}`,
giveArgs: []string{"--file", "testdata/mango.tpl"},
wantOutput: "yummy\n\n",
},
{
name: "parse glob single",
giveInput: `{"fruits": {"mango": "yummy"}}`,
giveArgs: []string{"--glob", "testdata/ma*.tpl"},
wantOutput: "yummy\n\n",
},
{
name: "parse multi with positional",
giveInput: `{"fruits": {"kiwi": "yay"}}`,
giveArgs: []string{"{{.fruits.kiwi}}", "--file", "testdata/apple.tpl", "--glob", "testdata/ma*.tpl"},
wantOutput: "yay\n",
},
{
name: "parse multi without positional",
giveInput: `{"fruits": {"avocado": "impossibru"}}`,
giveArgs: []string{"--glob", "testdata/*.tpl", "--name", "avocado.tpl"},
wantOutput: "impossibru\n\n",
},
{
name: "require name",
giveInput: `{}`,
giveArgs: []string{"--glob", "testdata/*.tpl"},
wantErrorMatch: `the --name flag is required when multiple templates are defined`,
},
{
name: "require template",
giveInput: `{}`,
wantErrorMatch: "no templates found",
},
{
name: "flag help",
giveArgs: []string{"-h"},
wantErrorMatch: "pflag: help requested",
},
{
name: "no value default",
giveInput: `{}`,
giveArgs: []string{"{{.nope}}"},
wantOutput: "<no value>\n",
},
{
name: "no value error",
giveInput: `{}`,
giveArgs: []string{"{{.nope}}", "--option", "missingkey=error"},
wantErrorMatch: `map has no entry for key "nope"`,
},
{
name: "decoder yaml",
giveInput: "foo: bar",
giveArgs: []string{"--no-newline", "--decoder=yaml", `test: {{.foo}}`},
wantOutput: "test: bar",
},
{
name: "decoder json",
giveInput: `{"foo":"bar"}`,
giveArgs: []string{"--no-newline", "--decoder=json", `test: {{.foo}}`},
wantOutput: "test: bar",
},
{
name: "decoder toml",
giveInput: `foo = "bar"`,
giveArgs: []string{"--no-newline", "--decoder=toml", `test: {{.foo}}`},
wantOutput: "test: bar",
},
{
name: "decoder invalid",
giveInput: `foo = "bar"`,
giveArgs: []string{"--no-newline", "--decoder=yikes", `test: {{.foo}}`},
wantErrorMatch: `unsupported decoder "yikes"`,
},
{
name: "parse file with equal flag",
giveInput: `{"fruits": {"mango": "yummy"}}`,
giveArgs: []string{"--file=testdata/mango.tpl"},
wantOutput: "yummy\n\n",
},
{
name: "yaml string keys",
giveInput: `key: value`,
giveArgs: []string{"--no-newline", "-d=yaml", `{{printf "%#v" .}}`},
wantOutput: `map[string]interface {}{"key":"value"}`,
},
{
name: "nil data",
giveInput: "",
giveArgs: []string{`{{.}}`},
wantOutput: "<no value>\n",
},
{
name: "issue-19-1",
giveInput: `{"fruits": {"mango": "yummy"}}`,
giveArgs: []string{"-f", "testdata/apple.tpl", "-f", "testdata/mango.tpl", "-n", "mango.tpl"},
wantOutput: "yummy\n\n",
},
{
name: "issue-19-2",
giveArgs: []string{"-f", "testdata/apple.tpl", "-f", "testdata/mango.tpl"},
wantErrorMatch: `the --name flag is required when multiple templates are defined`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
output := &bytes.Buffer{}
var in io.Reader
if tt.giveInput != "" {
in = strings.NewReader(tt.giveInput)
}
err := New("test", pflag.NewFlagSet(tt.name, pflag.ContinueOnError)).Run(tt.giveArgs, in, output)
if len(tt.wantErrorMatch) > 0 {
if err == nil {
t.Fatalf("want error but got none")
}
ok, err2 := regexp.MatchString(tt.wantErrorMatch, err.Error())
if err2 != nil {
t.Fatal(err2)
}
if !ok {
t.Fatalf("wrong error: got %v but want %q", err, tt.wantErrorMatch)
}
return
} else if err != nil {
t.Fatal(err)
} else {
if gotOutput := output.String(); gotOutput != tt.wantOutput {
t.Errorf("wrong run output:\ngot:\n%q\n\nwant\n%q\n", gotOutput, tt.wantOutput)
}
}
})
}
}