-
Notifications
You must be signed in to change notification settings - Fork 1
/
oJobTest.yaml
331 lines (315 loc) · 10.3 KB
/
oJobTest.yaml
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Test oJob functionality
# Copyright 2023 Nuno Aguiar
jobs:
# Test assert
# -----------------
- name: oJob Assert
typeArgs:
noLog: false
shortcut:
name : testAssert
keyArg: msg
args :
a: a
b: b
help:
text : This job will assert that args.a and args.b provided are equal. Otherwise it will display the args.msg message.
expects:
- name: a
desc: The 'a' value.
- name: b
desc: The 'b' value.
- name: msg
desc: The message to display if the assert fails.
exec: |
if(isUnDef(args.a) || isUnDef(args.b)) throw "Please define args.a and args.b."
if(isUnDef(args.msg)) args.msg = "A is different from B"
ow.loadTest().assert(isString(args.a) ? templify(args.a, args) : args.a, isString(args.b) ? templify(args.b, args) : args.b, args.msg)
# Test functions
#
# ---------------
- name: oJob Test
typeArgs:
noLog: false
shortcut:
name : test
keyArg: name
args :
func : func
job : job
count : count
asserts: asserts
debug : debug
help:
text : This job will test an args.func (function) or an args.job provided. The test will use the current oJob job name. Optionally you can specify args.count (defaults to 1) to test more than one time.
expects:
- name: name
desc: If defined will use as the test name instead of the oJob job name.
- name: func
desc: The function to execute and test.
- name: job
desc: The ojob to execute and test.
- name: count
desc: The number of test repeats (defaults to 1).
- name: asserts
desc: Array of asserts maps to test the results (composed of 'path', 'value' and 'msg')
- name: debug
desc: If true will output debug information.
exec: |
if (isUnDef(args.func) && isUnDef(args.job)) throw "Please define args.func or an args.job."
var count = _$(args.count, "count").toNumber().isNumber().default(1)
var jname = _$(args.name, "name").isString().default(job.name)
if (isDef(args.func)) {
if (!(isFunction(args.func))) args.func = new Function(args.func)
ow.loadTest()
args.asserts = _$(args.asserts, "asserts").isArray().default([])
for(var c = 0; c < count; c++) {
ow.test.test(jname, () => {
var _r = args.func()
if (args.debug) {
print("DEBUG | test function:")
cprint(_r)
}
args.asserts.forEach(a => {
a.path = _$(a.path, "path").isString().default("@")
a.msg = _$(a.msg, "msg").isString().default("Assert failed " + af.toSLON(a))
ow.test.assert($path(_r, a.path), a.value, a.msg)
})
})
}
} else {
if (isDef(args.job)) {
for(var c = 0; c < count; c++) ow.loadTest().test(jname, () => {
id = "|" + genUUID()
var _r = $job(args.job, __, id)
if (args.debug) {
print("DEBUG | test function:")
cprint(_r)
}
if (!isMap(_r)) throw $from( ow.oJob.getLogCh().get({ ojobId: ow.oJob.getID() + id, name: args.job }).log ).last().error
args.asserts.forEach(a => {
a.path = _$(a.path, "path").isString().default("@")
a.msg = _$(a.msg, "msg").isString().default("Assert failed " + af.toSLON(a))
ow.test.assert($path(_r, a.path), a.value, a.msg)
})
})
}
}
# test outside commands
#
# ------------------
- name: oJob Test sh
typeArgs:
noLog: false
shortcut:
name : testSh
keyArg: name
args :
cmd : cmd
help:
text : This job will test a args.cmd (shell command) provided. The test will use the current oJob job name.
expects:
- name: name
desc: If defined will use as the test name instead of the oJob job name.
- name: cmd
desc: The shell command to test.
exec: |
var jname = _$(args.name, "name").isString().default(job.name)
if (isUnDef(args.cmd)) throw "Please define args.cmd."
ow.loadTest().test(jname, args.cmd)
# Test results jobs
#
# ------------------------
- name : oJob Test Results
typeArgs:
noLog: false
shortcut:
name : testResults
keyArg: quiet
args :
noprofile: noprofile
key : key
help :
text : "Prints or outputs (if args.quiet = true) a args.results with: count of tests, tests passed, tests failed and all profile information."
expects:
- name: quiet
desc: Boolean flag that if true wont output any test results to stdout.
- name: noprofile
desc: Boolean flag that if true will not include profile results
- name: key
desc: If defined will output the results to the provided key.
check:
in:
quiet : toBoolean.isBoolean.default(false)
noprofile: toBoolean.isBoolean.default(false)
exec : |
var prof = ow.loadTest().__profile
args.results = {
count: ow.loadTest().getCountTest(),
pass: ow.loadTest().getCountPass(),
fail: ow.loadTest().getCountFail(),
asserts: ow.loadTest().getCountAssert(),
profile: Object.keys(prof).map(r => ({
name: r,
hits: prof[r].hits,
sum : prof[r].sum,
last: prof[r].last,
max : prof[r].max,
min : prof[r].min,
avg : prof[r].sum / prof[r].hits
}))
}
if (args.noprofile) delete args.results.profile
if (isDef(args.key)) $set(args.key, args.results)
if ((isDef(args.quiet) && !args.quiet) || isUnDef(args.quiet)) ow.oJob.output(args.results, args)
# ----------------------------
- name: oJob Test Todo results
typeArgs:
noLog: false
shortcut:
name : testTodoResults
keyArg: quiet
args : {}
help:
text : "Prints or outputs (if args.quiet = true) a args.results with: count of tests, tests passed and tests failed."
expects:
- name: quiet
desc: Boolean flag that if true wont output any test results to stdout.
exec: |
args.results = {
count: $ch("oJob::log").size(),
pass: $from($ch("oJob::log").getAll()).equals("success", true).count(),
fail: $from($ch("oJob::log").getAll()).equals("error", true).count()
};
if ((isDef(args.quiet) && !args.quiet) || isUnDef(args.quiet)) ow.oJob.output(args.results, args)
# -----------------------------
- name : oJob Generate Markdown
typeArgs:
noLog: false
shortcut:
name : testGenMD
keyArg: key
args :
path: path
file: file
includeLogs: includeLogs
help :
text : "This job will generate a Markdown with the current test results and optionally logs."
expects:
- name: key
desc: If a 'file' is not defined will output to provided 'key' (including 'args') and 'path'
- name: path
desc: The path withing the provided 'key' to store the markdown output
- name: file
desc: If defined will output the markdown content to the provided file.
- name: includeLogs
desc: If ojob.channels.recordLog is true will include the logs in the markdown.
check:
in:
key : isString.default(__)
path: isString.default(__)
file: isString.default(__)
includeLogs: isBoolean.default(false)
exec : |
var _r = ow.loadTest().toMarkdown()
if (args.includeLogs && $ch().list().indexOf("__log") >= 0)
_r += "\n## Logs\n\n" + ow.template.md.table($ch("__log").getAll().map(r => ({
timestamp: r.d,
level : r.t,
message : r.m
})))
if (isDef(args.file)) {
io.writeFileString(args.file, _r)
} else {
if (args.key == "args") {
$$(args).set(args.path, _r)
} else {
var _c = $get(args.key) || {}
$$(_c).set(args.path, _r)
$set(args.key, _c)
}
}
# JSON results
# ------------
- name: oJob Generate JSON results
typeArgs:
noLog: false
shortcut:
name : testGenJSON
keyArg: file
args :
includeLogs: includeLogs
noProfile : noProfile
help:
text : "This job will generate a JSON with the current test results and optionally logs."
expects:
- name: file
desc: If defined will output the JSON content to the provided file (if '-' will output to stdout).
- name: noprofile
desc: Boolean flag that if true will not include profile results
- name: includeLogs
desc: If ojob.channels.recordLog is true will include the logs in the markdown.
check:
in:
file: isString.default("-")
includeLogs: isBoolean.default(false)
noProfile: isBoolean.default(false)
exec : |
var prof = ow.loadTest().__profile
args.results = {
count: ow.loadTest().getCountTest(),
pass: ow.loadTest().getCountPass(),
fail: ow.loadTest().getCountFail(),
asserts: ow.loadTest().getCountAssert(),
profile: Object.keys(prof).map(r => ({
name: r,
hits: prof[r].hits,
sum : prof[r].sum,
last: prof[r].last,
max : prof[r].max,
min : prof[r].min,
avg : prof[r].sum / prof[r].hits
}))
}
if (args.noprofile) delete args.results.profile
var _r = args.results
if (args.includeLogs && $ch().list().indexOf("__log") >= 0) {
_r = merge(_r, {
logs: $ch("__log").getAll().map(r => ({
timestamp: r.d,
level : r.t,
message : r.m
}))
})
}
if (args.file == "-") {
sprint(_r, "")
} else {
io.writeFileJSON(args.file, _r, "")
}
# JUnit results
#
# -----------------------------
- name: oJob Generate JUnit XML
typeArgs:
noLog: false
shortcut:
name : testGenJUnit
keyArg: suitesId
args :
suitesName : suitesName
resultsFile: resultsFile
help:
text : "This job will generate a JUnit XML with the current test results."
expects:
- name: suitesId
desc: (String) The JUnit suites id
- name: suitesName
desc: (String) The JUnit suites name
- name: resultsFile
desc: (String) The filename and path where to store the JUnit results.
exec: |
if (isUnDef(args.suitesId)) args.suitesId = "test";
if (isUnDef(args.suitesName)) args.suitesName = "test";
if (isUnDef(args.resultsFile)) args.resultsFile = "testResults.xml";
io.writeFileString(args.resultsFile, ow.loadTest().toJUnitXML(args.suitesId, args.suitesName));