forked from samsarahq/taskrunner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_internal_test.go
More file actions
130 lines (122 loc) · 3.61 KB
/
runner_internal_test.go
File metadata and controls
130 lines (122 loc) · 3.61 KB
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
package taskrunner
import (
"context"
"testing"
"github.com/samsarahq/taskrunner/shell"
"github.com/stretchr/testify/assert"
)
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
}{
{
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 flags and flag args passed directly to taskrunner",
cliArgs: []string{"--config", "pathtoconfig", "--listAll", "--watch", "mock/task/1", "--longFlagA"},
expectedGroups: map[string][]string{
"mock/task/1": {"--longFlagA"},
},
},
{
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'"},
},
},
{
// 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 {
taskFlagGroups := 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)
}
}