forked from wagoodman/bashful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
223 lines (184 loc) · 6 KB
/
config_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
package main
import (
"strings"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/alecthomas/repr"
)
func TestMinMax(t *testing.T) {
tester := func(arr []float64, exMin, exMax float64, exError bool) {
min, max, err := MinMax(arr)
if min != exMin {
t.Error("Expected min=", exMin, "got", min)
}
if max != exMax {
t.Error("Expected max=", exMax, "got", max)
}
if err != nil && !exError {
t.Error("Expected no error, got error:", err)
} else if err == nil && exError {
t.Error("Expected an error but there wasn't one")
}
}
tester([]float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8}, 1.1, 8.8, false)
tester([]float64{1.1, 1.1, 1.1, 1.1, 1.1, 1.1}, 1.1, 1.1, false)
tester([]float64{}, 0, 0, true)
}
func TestRemoveOneValue(t *testing.T) {
eq := func(a, b []float64) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
tester := func(arr []float64, value float64, exArr []float64) {
testArr := removeOneValue(arr, value)
if !eq(testArr, exArr) {
t.Error("Expected", repr.String(exArr), "got", repr.String(testArr))
}
}
tester([]float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8}, 1.1, []float64{2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8})
tester([]float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8}, 3.14159, []float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8})
tester([]float64{1.1, 1.1, 1.1, 1.1, 1.1, 1.1}, 1.1, []float64{1.1, 1.1, 1.1, 1.1, 1.1})
tester([]float64{}, 3.14159, []float64{})
}
func TestCreateTasks_SuccessfulParse(t *testing.T) {
var expStr, actStr string
var expOpt, actOpt bool
var exNum int
simpleYamlStr := `
tasks:
- cmd: random-worker.sh 10
- name: Compiling source
parallel-tasks:
- cmd: compile-something.sh 2
- cmd: compile-something.sh 9
stop-on-failure: false
show-output: false
- cmd: compile-something.sh 6
- cmd: compile-something.sh 4 ?
for-each:
- plug 4
- plug 5
- plug 6
- name: some random task name ?
cmd: random-worker.sh 2
for-each:
- plug 3
- cmd: random-worker.sh 10 ?
for-each:
- plug 1
- plug 2
`
// load test time cache
config.commandTimeCache = make(map[string]time.Duration)
config.commandTimeCache["compile-something.sh 2"] = time.Duration(2 * time.Second)
config.commandTimeCache["compile-something.sh 4"] = time.Duration(4 * time.Second)
config.commandTimeCache["compile-something.sh 6"] = time.Duration(6 * time.Second)
config.commandTimeCache["compile-something.sh 9"] = time.Duration(9 * time.Second)
config.commandTimeCache["compile-something.sh 10"] = time.Duration(10 * time.Second)
// load test config yaml
config.Options = NewOptionsConfig()
err := yaml.Unmarshal([]byte(simpleYamlStr), &config)
if err != nil {
t.Error("Expected no error, got error:", err)
}
// create and inflate tasks
tasks := CreateTasks()
// validate test task yaml
exNum = 5
if len(tasks) != exNum {
t.Error("Expected", exNum, "tasks got", len(tasks))
}
exNum = 6
if len(tasks[1].Children) != exNum {
t.Error("Expected", exNum, "parallel tasks got", len(tasks[1].Children))
}
// ensure that names are set properly
expStr, actStr = "Compiling source", tasks[1].Config.Name
if actStr != expStr {
t.Error("Expected name:", expStr, "got name:", actStr)
}
expStr, actStr = "some random task name plug 3", tasks[2].Config.Name
if actStr != expStr {
t.Error("Expected name:", expStr, "got name:", actStr)
}
// check the names of the top task list
for _, taskIndex := range []int{0, 3, 4} {
expStr, actStr = tasks[taskIndex].Config.CmdString, tasks[taskIndex].Config.Name
if actStr != expStr {
t.Error("Expected name:", expStr, "got name:", actStr)
}
}
// check the names of the top parallel list
for taskIndex := 0; taskIndex < len(tasks[1].Children); taskIndex++ {
expStr, actStr = tasks[1].Children[taskIndex].Config.CmdString, tasks[1].Children[taskIndex].Config.Name
if actStr != expStr {
t.Error("Expected name:", expStr, "got name:", actStr)
}
}
// ensure that names and commands of for-each tasks fill in the ? character with params
for _, taskIndex := range []int{3, 4} {
expStr, actStr = tasks[taskIndex].Config.CmdString, tasks[taskIndex].Config.Name
if actStr != expStr {
t.Error("Expected name:", expStr, "got name:", actStr)
}
if !strings.Contains(tasks[taskIndex].Config.Name, "plug") {
t.Error("Expected name to contain 'plug' but got:", actStr)
}
if !strings.Contains(tasks[taskIndex].Config.CmdString, "plug") {
t.Error("Expected cmd to contain 'plug' but got:", actStr)
}
}
expStr, actStr = "some random task name plug 3", tasks[2].Config.Name
if actStr != expStr {
t.Error("Expected name:", expStr, "got name:", actStr)
}
expStr, actStr = "random-worker.sh 2", tasks[2].Config.CmdString
if actStr != expStr {
t.Error("Expected cmd:", expStr, "got cmd:", actStr)
}
// ensure stop on fail and show output can be overridden to false
expOpt, actOpt = false, tasks[1].Children[1].Config.ShowTaskOutput
if actOpt != expOpt {
t.Error("Expected name:", expOpt, "got name:", actOpt)
}
expOpt, actOpt = false, tasks[1].Children[1].Config.StopOnFailure
if actOpt != expOpt {
t.Error("Expected name:", expOpt, "got name:", actOpt)
}
}
// TODO: learn mocking in go... which framework?
// func TestNoCmdGiven(t *testing.T) {
// simpleYamlStr := `
// tasks:
// - name: woops
// `
// // load test time cache (empty)
// config.commandTimeCache = make(map[string]time.Duration)
// // load test config yaml
// config.Options = defaultOptions()
// err := yaml.Unmarshal([]byte(simpleYamlStr), &config)
// if err != nil {
// t.Error("Expected no error, got error:", err)
// }
// // create and inflate tasks
// CreateTasks()
// // validate test task yaml
// expStr, actStr := "Compiling source", config.Tasks[1].Config.Name
// if actStr != expStr {
// t.Error("Expected name:", expStr, "got name:", actStr)
// }
// }