forked from urfave/sflags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcli_test.go
155 lines (142 loc) · 3.29 KB
/
gcli_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
package gcli
import (
"errors"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
"github.com/urfave/sflags"
)
type cfg1 struct {
StringValue1 string
StringValue2 string `flag:"string-value-two s"`
CounterValue1 sflags.Counter
StringSliceValue1 []string
}
func TestParse(t *testing.T) {
tests := []struct {
name string
cfg interface{}
args []string
expCfg interface{}
expErr1 error // sflag Parse error
expErr2 error // cli Parse error
}{
{
name: "Test cfg1",
cfg: &cfg1{
StringValue1: "string_value1_value",
StringValue2: "string_value2_value",
CounterValue1: 1,
StringSliceValue1: []string{"one", "two"},
},
expCfg: &cfg1{
StringValue1: "string_value1_value2",
StringValue2: "string_value2_value2",
CounterValue1: 3,
StringSliceValue1: []string{
"one2", "two2", "three", "4"},
},
args: []string{
"--string-value1", "string_value1_value2",
"--string-value-two", "string_value2_value2",
"--counter-value1", "--counter-value1",
"--string-slice-value1", "one2",
"--string-slice-value1", "two2",
"--string-slice-value1", "three,4",
},
},
{
name: "Test cfg1 no args",
cfg: &cfg1{
StringValue1: "string_value1_value",
StringValue2: "",
},
expCfg: &cfg1{
StringValue1: "string_value1_value",
StringValue2: "",
},
args: []string{},
},
{
name: "Test cfg1 short option",
cfg: &cfg1{
StringValue2: "string_value2_value",
},
expCfg: &cfg1{
StringValue2: "string_value2_value2",
},
args: []string{
"-s=string_value2_value2",
},
},
{
name: "Test cfg1 without default values",
cfg: &cfg1{},
expCfg: &cfg1{
StringValue1: "string_value1_value2",
StringValue2: "string_value2_value2",
CounterValue1: 3,
},
args: []string{
"--string-value1", "string_value1_value2",
"--string-value-two", "string_value2_value2",
"--counter-value1=2", "--counter-value1",
},
},
{
name: "Test cfg1 bad option",
cfg: &cfg1{
StringValue1: "string_value1_value",
},
args: []string{
"--bad-value=string_value1_value2",
},
expErr2: errors.New("flag provided but not defined: -bad-value"),
},
{
name: "Test bad cfg value",
cfg: "bad config",
expErr1: errors.New("object must be a pointer to struct or interface"),
},
}
// forbid urfave/cli to exit
cli.OsExiter = func(i int) {}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
flags, err := Parse(test.cfg)
if test.expErr1 != nil {
require.Error(t, err)
require.Equal(t, test.expErr1, err)
} else {
require.NoError(t, err)
}
if err != nil {
return
}
cliApp := cli.NewApp()
cliApp.Action = func(c *cli.Context) error {
return nil
}
cliApp.UseShortOptionHandling = true
cli.ErrWriter = io.Discard
cliApp.OnUsageError = func(_ *cli.Context, err error, _ bool) error {
return err
}
cliApp.Flags = flags
args := append([]string{"cliApp"}, test.args...)
err = cliApp.Run(args)
if test.expErr2 != nil {
require.Error(t, err)
require.Equal(t, test.expErr2, err)
} else {
require.NoError(t, err)
}
if err != nil {
return
}
assert.Equal(t, test.expCfg, test.cfg)
})
}
}