Skip to content

Commit

Permalink
test: adding markdown and shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Sep 7, 2023
1 parent 98ff451 commit 1aaa07d
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 83 deletions.
6 changes: 3 additions & 3 deletions .package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ name: oJob-common
main: ''
mainJob: ''
license: Apache 2.0
version: '20230905'
version: '20230906'
dependencies:
openaf: '>=20211229'
files:
Expand Down Expand Up @@ -69,7 +69,7 @@ files:
- ojob.yaml
filesHash:
LICENSE: 92170cdc034b2ff819323ff670d3b7266c8bffcd
README.md: a568bcd30c0268fc25eef6c9d510363710248220
README.md: 390af07744301413415a75ef85464fced704935c
oJobBasics.yaml: 3178f72c3826bd9514cbd5c15bc3399821191f3c
oJobCh.yaml: 4290a3d5dc035a6d7b6659aeb0e370d0bb55be58
oJobDebug.yaml: bfb5debec954e3b524ae4ce103d6ea8e780448e9
Expand All @@ -89,7 +89,7 @@ filesHash:
oJobSQL.yaml: c7d0ab718f87470f83c07c85b6c4074a810ea3ef
oJobSSH.yaml: 6dbefb7028a6be855ee4d3cc93e971eeee563d5b
oJobSVN.yaml: 10920575ac4c337fe99448b8a04a0689c2bde77f
oJobTest.yaml: fb1cb5c06120b5e4774b620fd8466b7574f7d39b
oJobTest.yaml: caebc8302482279d936b47210ef579022bc3b167
oJobWatchDog.yaml: d1dbeab36784bd6722daa27e572425c84ebbc36b
oJobXLS.yaml: 00def6b195d7487989d7aa2c774b584a0e0b01a0
oJobs.yaml: 83d8406c43b4a5325686467ed80911a9c1e26d49
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,37 @@ todo:

## Shortcuts examples:

### oJobTest.yaml

```yaml
include:
- oJobTest.yaml
todo:
- (test ): oJob::a
((job )): a
- (test ): oJob::b
((job )): b
- (test ): Script::Test
((func )): |
sleep(1500, true)
ow.test.assert(1, 1, "Problem with assert in script test")
- (testAssert): Problem with a and b
((a )): 123
((b )): 124
- (testGenMD ): __
((file )): results.md
jobs:
- name: a
exec: |
sleep(1500, true)
- name: b
exec: |
throw "MY error!"
```

### oJobHTTPd.yaml

```yaml
Expand Down
291 changes: 211 additions & 80 deletions oJobTest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,223 @@
# Copyright 2023 Nuno Aguiar

jobs:
# Test assert
#
- name: oJob Assert
help: >
This job will assert that args.a and args.b provided are equal. Otherwise it will
display the args.msg message.
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";
# 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(args.a, args.b, args.msg);
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
help: >
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.
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)
if (isDef(args.func)) {
if (!(isFunction(args.func))) args.func = new Function(args.func)
for(var c = 0; c < count; c++) ow.loadTest().test(job.name, args.func)
} else {
if (isDef(args.job)) {
for(var c = 0; c < count; c++) ow.loadTest().test(job.name, () => $job(args.job) )
}
}
# Test functions
#
# ---------------
- name: oJob Test
typeArgs:
noLog: false
shortcut:
name : test
keyArg: name
args :
func : func
job : job
count: count
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).
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)
# test outside commands
#
- name: oJob Test sh
help: >
This job will test a args.cmd (shell command) provided. The test will use the current
oJob job name.
exec: >
if (isUnDef(args.cmd)) throw "Please define args.cmd.";
ow.loadTest().test(job.name, args.cmd);
if (isDef(args.func)) {
if (!(isFunction(args.func))) args.func = new Function(args.func)
for(var c = 0; c < count; c++) ow.loadTest().test(jname, args.func)
} else {
if (isDef(args.job)) {
for(var c = 0; c < count; c++) ow.loadTest().test(jname, () => {
id = "|" + genUUID()
var _r = $job(args.job, __, id)
# Test results jobs
#
if (!isMap(_r)) throw $from( ow.oJob.getLogCh().get({ ojobId: ow.oJob.getID() + id, name: args.job }).log ).last().error
})
}
}
- name: oJob Test Results
help: |
Prints or outputs (if args.quiet = true) a args.results with: count of tests, tests passed, tests failed and all profile information.
exec: |
var prof = ow.loadTest().__profile
args.results = {
count: ow.loadTest().getCountTest(),
pass: ow.loadTest().getCountPass(),
fail: ow.loadTest().getCountFail(),
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 ((isDef(args.quiet) && !args.quiet) || isUnDef(args.quiet)) ow.oJob.output(args.results)
# 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)
- name: oJob Test Todo results
help: |
Prints or outputs (if args.quiet = true) a args.results with: count of tests, tests passed and tests failed.
exec: >
args.results = {
count: $ch("oJob::logs").size(),
pass: $from($ch("oJob::logs").getAll()).equals("success", true).count(),
fail: $from($ch("oJob::logs").getAll()).equals("error", true).count()
};
if ((isDef(args.quiet) && !args.quiet) || isUnDef(args.quiet)) ow.oJob.output(args.results)
if (isUnDef(args.cmd)) throw "Please define args.cmd."
ow.loadTest().test(jname, args.cmd)
# JUnit results
# Test results jobs
#
# ------------------------
- name : oJob Test Results
typeArgs:
noLog: false
shortcut:
name : testResults
keyArg: quiet
args :
noprofile: noprofile
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
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.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::logs").size(),
pass: $from($ch("oJob::logs").getAll()).equals("success", true).count(),
fail: $from($ch("oJob::logs").getAll()).equals("error", true).count()
};
if ((isDef(args.quiet) && !args.quiet) || isUnDef(args.quiet)) ow.oJob.output(args.results)
- name: oJob Generate JUnit XML
help: >
This job will generate a JUnit XML with the current test results. You can provide:
- suitesId (String) The JUnit suites id
- suitesName (String) The JUnit suites name
- resultsFile (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));
# -----------------------------
- name : oJob Generate Markdown
typeArgs:
noLog: false
shortcut:
name : testGenMD
keyArg: key
args :
path: path
file: file
help :
text : "This job will generate a Markdown with the current test results."
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.
check:
in:
key : isString.default(__)
path: isString.default(__)
file: isString.default(__)
exec : |
var _r = ow.loadTest().toMarkdown()
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)
}
}
# 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));

0 comments on commit 1aaa07d

Please sign in to comment.