-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexecutor_internal_test.go
290 lines (262 loc) · 8.95 KB
/
executor_internal_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package taskrunner
import (
"context"
"testing"
"time"
"github.com/samsarahq/taskrunner/config"
"github.com/samsarahq/taskrunner/shell"
"github.com/stretchr/testify/assert"
)
func boolPtr(i bool) *bool {
return &i
}
func intPtr(i int) *int {
return &i
}
func float64Ptr(i float64) *float64 {
return &i
}
func stringPtr(i string) *string {
return &i
}
func durationPtr(i time.Duration) *time.Duration {
return &i
}
func TestParseTaskOptionsListToMap(t *testing.T) {
config := &config.Config{}
mockRunWithFlags := func(ctx context.Context, shellRun shell.ShellRun, flags map[string]FlagArg) error {
return nil
}
taskFlagA := TaskFlag{
Description: "This is a description of what bool flag A does.",
LongName: "flagA",
ShortName: []rune("a")[0],
ValueType: BoolTypeFlag,
Default: "true",
}
taskFlagB := TaskFlag{
Description: "This is a description of what string flag B does.",
ShortName: []rune("b")[0],
ValueType: StringTypeFlag,
Default: "presidential dolphin",
}
taskFlagC := TaskFlag{
Description: "This is a description of what int flag C does.",
ShortName: []rune("c")[0],
ValueType: IntTypeFlag,
Default: "10",
}
taskFlagD := TaskFlag{
Description: "This is a description of what float64 flag D does.",
ShortName: []rune("d")[0],
ValueType: Float64TypeFlag,
Default: "1.72",
}
taskFlagE := TaskFlag{
Description: "This is a description of what duration flag E does.",
ShortName: []rune("e")[0],
ValueType: DurationTypeFlag,
Default: "11h",
}
taskFlagF := TaskFlag{
Description: "This is a description of what duration flag F does.",
ShortName: []rune("f")[0],
ValueType: BoolTypeFlag,
}
mockTaskName := "mock/task/1"
mockTask1 := &Task{
Name: mockTaskName,
RunWithFlags: mockRunWithFlags,
Flags: []TaskFlag{taskFlagA, taskFlagB, taskFlagC, taskFlagD, taskFlagE},
}
mockTaskFlagRegistry := map[string]map[string]TaskFlag{
mockTaskName: {
"flagA": taskFlagA,
"a": taskFlagA,
"b": taskFlagB,
"c": taskFlagC,
"d": taskFlagD,
"e": taskFlagE,
"f": taskFlagF,
},
}
tasks := []*Task{mockTask1}
testCases := []struct {
description string
flagArgs []string
expectedFlagAVal *bool
expectedLongFlagAVal *bool
expectedFlagBVal *string
expectedFlagCVal *int
expectedFlagDVal *float64
expectedFlagEVal *time.Duration
expectFlagFValIsNil bool
invalidFlagPanicVal string
}{
{
description: "Should parse BoolTypeFlags correctly and include value for both Short and Long Names",
flagArgs: []string{"-a"},
expectedFlagAVal: boolPtr(true),
expectedLongFlagAVal: boolPtr(true),
},
{
description: "Should parse String/Duration/Int/Float64TypeFlags wrapped in \"\"s correctly",
flagArgs: []string{"-b=\"test\"", "-c=\"1\"", "-d=\"1.3\"", "-e=\"10h\""},
expectedFlagBVal: stringPtr("test"),
expectedFlagCVal: intPtr(1),
expectedFlagDVal: float64Ptr(1.3),
expectedFlagEVal: durationPtr(time.Duration(10 * time.Hour)),
},
{
description: "Should parse String/Duration/Int/Float64TypeFlags not wrapped in \"\"s correctly",
flagArgs: []string{"-b=test", "-c=1", "-d=1.3", "-e=10h"},
expectedFlagBVal: stringPtr("test"),
expectedFlagCVal: intPtr(1),
expectedFlagDVal: float64Ptr(1.3),
expectedFlagEVal: durationPtr(time.Duration(10 * time.Hour)),
},
{
description: "Should respect Default str value if no arg passed to variable flag",
flagArgs: []string{"-b"},
expectedFlagBVal: stringPtr("presidential dolphin"),
},
{
description: "Should respect Default int value if no arg passed to variable flag",
flagArgs: []string{"-c"},
expectedFlagCVal: intPtr(10),
},
{
description: "Should respect Default float64 value if no arg passed to variable flag",
flagArgs: []string{"-d"},
expectedFlagDVal: float64Ptr(1.72),
},
{
description: "Should respect Default duration value if no arg passed to variable flag",
flagArgs: []string{"-e"},
expectedFlagEVal: durationPtr(time.Duration(11 * time.Hour)),
},
{
description: "Should return nil if no arg passed to variable flag and no Default defined",
flagArgs: []string{"-f"},
expectFlagFValIsNil: true,
},
{
description: "Should store Default values for all flags and Value nil when no args are passed",
flagArgs: []string{},
expectedFlagAVal: boolPtr(true),
expectedLongFlagAVal: boolPtr(true),
expectedFlagBVal: stringPtr("presidential dolphin"),
expectedFlagCVal: intPtr(10),
expectedFlagDVal: float64Ptr(1.72),
expectFlagFValIsNil: true,
},
{
description: "Should panic if there are multiple `=`s in the flag",
flagArgs: []string{"-b=\"testing\"=\"testing123\""},
invalidFlagPanicVal: "Invalid flag syntax for mock/task/1: `-b=\"testing\"=\"testing123\"`",
},
{
description: "Should panic if an unsupported flag is passed",
flagArgs: []string{"--invalidFlag"},
invalidFlagPanicVal: "Unsupported flag passed to mock/task/1: `--invalidFlag`. See error: Unsupported flag: --invalidFlag",
},
{
description: "Should panic if not possible to cast string to int for IntTypeFlag args",
flagArgs: []string{"-c=\"\"00\"10\"b\"\""},
},
{
description: "Should panic if not possible to cast string to float64 for Float64TypeFlag args",
flagArgs: []string{"-d=\"\"00\"10.3\"b\"\""},
},
{
description: "Should panic if not possible to cast string to duration for DurationTypeFlag args",
flagArgs: []string{"-e=\"\"00\"10.3\"b\"\"09m"},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
executorOptions := []ExecutorOption{
func(e *Executor) {
e.taskFlagArgs = map[string][]string{
mockTaskName: tc.flagArgs,
}
},
func(e *Executor) {
e.taskFlagsRegistry = mockTaskFlagRegistry
},
}
executor := NewExecutor(config, tasks, executorOptions...)
if tc.invalidFlagPanicVal != "" {
assert.PanicsWithValue(t, tc.invalidFlagPanicVal, func() { executor.parseTaskFlagsIntoMap(mockTaskName, tc.flagArgs) })
} else {
flagsMap := executor.parseTaskFlagsIntoMap(mockTaskName, tc.flagArgs)
if tc.expectedFlagAVal != nil {
assert.Equal(t, *tc.expectedFlagAVal, *flagsMap["a"].BoolVal())
assert.Equal(t, *tc.expectedLongFlagAVal, *flagsMap["flagA"].BoolVal())
flagArgShort := flagsMap["a"]
assert.Panics(t, func() { flagArgShort.IntVal() })
assert.Panics(t, func() { flagArgShort.StringVal() })
assert.Panics(t, func() { flagArgShort.Float64Val() })
assert.Panics(t, func() { flagArgShort.DurationVal() })
flagArgLong := flagsMap["flagA"]
assert.Panics(t, func() { flagArgLong.IntVal() })
assert.Panics(t, func() { flagArgLong.StringVal() })
assert.Panics(t, func() { flagArgLong.Float64Val() })
assert.Panics(t, func() { flagArgLong.DurationVal() })
if len(tc.flagArgs) == 0 {
assert.Nil(t, flagArgShort.Value)
assert.Nil(t, flagArgLong.Value)
}
}
if tc.expectedFlagBVal != nil {
assert.Equal(t, *tc.expectedFlagBVal, *flagsMap["b"].StringVal())
flagArg := flagsMap["b"]
assert.Panics(t, func() { flagArg.IntVal() })
assert.Panics(t, func() { flagArg.BoolVal() })
assert.Panics(t, func() { flagArg.Float64Val() })
assert.Panics(t, func() { flagArg.DurationVal() })
if len(tc.flagArgs) == 0 {
assert.Nil(t, flagArg.Value)
}
}
if tc.expectedFlagCVal != nil {
assert.Equal(t, *tc.expectedFlagCVal, *flagsMap["c"].IntVal())
flagArg := flagsMap["c"]
assert.Panics(t, func() { flagArg.StringVal() })
assert.Panics(t, func() { flagArg.BoolVal() })
assert.Panics(t, func() { flagArg.Float64Val() })
assert.Panics(t, func() { flagArg.DurationVal() })
if len(tc.flagArgs) == 0 {
assert.Nil(t, flagArg.Value)
}
}
if tc.expectedFlagDVal != nil {
assert.Equal(t, *tc.expectedFlagDVal, *flagsMap["d"].Float64Val())
flagArg := flagsMap["d"]
assert.Panics(t, func() { flagArg.StringVal() })
assert.Panics(t, func() { flagArg.BoolVal() })
assert.Panics(t, func() { flagArg.IntVal() })
assert.Panics(t, func() { flagArg.DurationVal() })
if len(tc.flagArgs) == 0 {
assert.Nil(t, flagArg.Value)
}
}
if tc.expectedFlagEVal != nil {
assert.Equal(t, *tc.expectedFlagEVal, *flagsMap["e"].DurationVal())
flagArg := flagsMap["e"]
assert.Panics(t, func() { flagArg.StringVal() })
assert.Panics(t, func() { flagArg.BoolVal() })
assert.Panics(t, func() { flagArg.IntVal() })
assert.Panics(t, func() { flagArg.Float64Val() })
if len(tc.flagArgs) == 0 {
assert.Nil(t, flagArg.Value)
}
}
if tc.expectFlagFValIsNil {
flagArg := flagsMap["f"]
assert.Nil(t, flagArg.BoolVal())
}
}
})
}
}