-
Notifications
You must be signed in to change notification settings - Fork 10
/
runner_internal_test.go
213 lines (197 loc) · 6.13 KB
/
runner_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
package taskrunner
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/samsarahq/taskrunner/shell"
)
// MockRunnerLogger is a mock RunnerLogger implementation that
// tracks whether a fatal method was called. Not thread-safe, and
// a separate instance must be used for each test.
type MockRunnerLogger struct {
fatal bool
}
func (l *MockRunnerLogger) Printf(format string, v ...interface{}) {}
func (l *MockRunnerLogger) Println(v ...interface{}) {}
func (l *MockRunnerLogger) Fatalf(format string, v ...interface{}) {
l.fatal = true
}
func (l *MockRunnerLogger) Fatalln(v ...interface{}) {
l.fatal = true
}
func TestRunnerRun_FatalScenarios(t *testing.T) {
// setup mock config and task
testConfigFile := "config/testdata/base.taskrunner.json"
mockRunWithFlags := func(ctx context.Context, shellRun shell.ShellRun, flags map[string]FlagArg) error {
return nil
}
mockTask := &Task{
Name: "basic", // must match a task defined in "desiredTasks" of the config file.
RunWithFlags: mockRunWithFlags,
}
testCases := []struct {
description string
tasks []*Task
cliArgs []string // the first arg should be the command, which is irrelevant for testing.
expectedFatal bool
}{
{
description: "Fatals on invalid config path",
cliArgs: []string{"", "--config", "unknownPath"},
expectedFatal: true,
},
{
description: "Fatals on no registered tasks",
cliArgs: []string{"", "--config", testConfigFile},
expectedFatal: true,
},
{
description: "Fatals when -list and -listAll are both specified",
tasks: []*Task{mockTask},
cliArgs: []string{"", "--config", testConfigFile, "--list", "--listAll"},
expectedFatal: true,
},
{
description: "Does not fatal when no tasks are specified",
tasks: []*Task{mockTask},
cliArgs: []string{"", "--config", testConfigFile, "--non-interactive"}, // non-interactive mode to ensure test exits when task completes
expectedFatal: false,
},
{
description: "Fatals when task is specified but unknown",
tasks: []*Task{mockTask},
cliArgs: []string{"", "--config", testConfigFile, "unknown/task", "--someFlag"},
expectedFatal: true,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
// override logger
mockLogger := &MockRunnerLogger{}
logger = mockLogger
// reset registry for each test
DefaultRegistry = NewRegistry()
for _, task := range tc.tasks {
DefaultRegistry.Add(task)
}
// run command and assert fatal status
os.Args = tc.cliArgs
Run()
assert.Equal(t, tc.expectedFatal, mockLogger.fatal, "fatal called")
})
}
}
func TestRunnerGroupTaskAndFlagArgs(t *testing.T) {
mockRunWithFlags := func(ctx context.Context, shellRun shell.ShellRun, flags map[string]FlagArg) error {
return nil
}
mockTask1 := &Task{
Name: "mock/task/1",
RunWithFlags: mockRunWithFlags,
Flags: []TaskFlag{
{
Description: "This is a description of what the bool flag A does.",
LongName: "longFlagA",
ShortName: []rune("a")[0],
ValueType: BoolTypeFlag,
},
{
Description: "This is a description of what the bool flag B does.",
LongName: "longFlagB",
ShortName: []rune("b")[0],
ValueType: BoolTypeFlag,
},
},
}
mockTask2 := &Task{
Name: "mock/task/2",
RunWithFlags: mockRunWithFlags,
Flags: []TaskFlag{
{
Description: "This is a description of what string flag C does.",
ShortName: []rune("c")[0],
ValueType: StringTypeFlag,
},
},
}
mockTask3 := &Task{
Name: "mock/task/3",
RunWithFlags: mockRunWithFlags,
Flags: []TaskFlag{
{
Description: "This is a description of what int flag D does.",
ShortName: []rune("d")[0],
ValueType: IntTypeFlag,
},
},
}
testCases := []struct {
description string
cliArgs []string
expectedGroups map[string][]string
expectedFoundUnknownTasks bool
}{
{
description: "Should group single flag to single task",
cliArgs: []string{"mock/task/1", "--longFlagA"},
expectedGroups: map[string][]string{
"mock/task/1": {"--longFlagA"},
},
},
{
description: "Should group multiple flags (long or short) to single task",
cliArgs: []string{"mock/task/1", "--longFlagA", "-b"},
expectedGroups: map[string][]string{
"mock/task/1": {"--longFlagA", "-b"},
},
},
{
description: "Should group multiple flags to multiple tasks",
cliArgs: []string{"mock/task/1", "--longFlagA", "-b", "mock/task/2", "-c='test'", "mock/task/3", "-d=1"},
expectedGroups: map[string][]string{
"mock/task/1": {"--longFlagA", "-b"},
"mock/task/2": {"-c='test'"},
"mock/task/3": {"-d=1"},
},
},
{
description: "Should exclude unsupported tasks and any flags passed to it",
cliArgs: []string{"mock/task/1", "--longFlagA", "thisisnotarealtask", "--longInvalidFlag", "mock/task/2", "-c='test'"},
expectedGroups: map[string][]string{
"mock/task/1": {"--longFlagA"},
"mock/task/2": {"-c='test'"},
},
expectedFoundUnknownTasks: true,
},
{
// Validation for supported flags happens in the executor.
description: "Should include unsupported flag args in group",
cliArgs: []string{"mock/task/1", "--longInvalidFlag"},
expectedGroups: map[string][]string{
"mock/task/1": {"--longInvalidFlag"},
},
},
}
runner := newRuntime()
runner.registry.Add(mockTask1)
runner.registry.Add(mockTask2)
runner.registry.Add(mockTask3)
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
taskFlagGroups, foundUnknownTasks := runner.groupTaskAndFlagArgs(tc.cliArgs)
var expectedTaskGroups []string
for key := range tc.expectedGroups {
expectedTaskGroups = append(expectedTaskGroups, key)
}
var taskGroups []string
for key, val := range taskFlagGroups {
taskGroups = append(taskGroups, key)
flags := tc.expectedGroups[key]
assert.ElementsMatch(t, flags, val)
}
assert.ElementsMatch(t, expectedTaskGroups, taskGroups)
assert.Equal(t, tc.expectedFoundUnknownTasks, foundUnknownTasks)
})
}
}