|
1 | 1 | /* |
2 | | - * Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
|
42 | 42 | var Mocha = require('mocha'); |
43 | 43 | var fs = require('fs'); |
44 | 44 |
|
| 45 | +const { |
| 46 | + EVENT_TEST_FAIL, |
| 47 | + EVENT_TEST_PASS, |
| 48 | + EVENT_TEST_PENDING |
| 49 | +} = Mocha.Runner.constants; |
| 50 | + |
45 | 51 | var limitToSuites = []; |
| 52 | +var jsonResultsFile = null; |
46 | 53 | for (var i = 2; i < process.argv.length; i++) { |
47 | 54 | var arg = process.argv[i]; |
48 | 55 | if (arg.length > 0) { |
| 56 | + if (arg.startsWith('--')) { |
| 57 | + if (arg == '--help') { |
| 58 | + console.log(`node ${process.argv[1]} [--json-results=file.json.gz] [testsuite.js...]`) |
| 59 | + process.exit(0); |
| 60 | + } else if (arg.startsWith('--json-results=')) { |
| 61 | + jsonResultsFile = arg.split('=', 2)[1]; |
| 62 | + } else { |
| 63 | + console.error(`Unknown option: ${arg}`); |
| 64 | + } |
| 65 | + continue; |
| 66 | + } |
49 | 67 | suite = process.argv[i]; |
50 | 68 | console.log("limiting to suite:", suite); |
51 | 69 | limitToSuites.push(suite); |
@@ -104,8 +122,40 @@ for (var i = 0; i < filesLength; i++) { |
104 | 122 | } |
105 | 123 | } |
106 | 124 |
|
| 125 | +const JsonStatus = { |
| 126 | + PASSED: "PASSED", |
| 127 | + FAILED: "FAILED", |
| 128 | + IGNORED: "IGNORED" |
| 129 | +}; |
| 130 | + |
| 131 | +let results = []; |
| 132 | +function onTestEnd(test, status) { |
| 133 | + results.push({ |
| 134 | + name: test.fullTitle(), |
| 135 | + status: status, |
| 136 | + duration: String(test.duration || 0) |
| 137 | + }); |
| 138 | +} |
| 139 | +function onRunEnd() { |
| 140 | + if (jsonResultsFile !== null) { |
| 141 | + let output = JSON.stringify(results); |
| 142 | + if (jsonResultsFile.endsWith('.gz')) { |
| 143 | + const {gzipSync} = require('zlib'); |
| 144 | + output = gzipSync(output); |
| 145 | + } |
| 146 | + fs.writeFileSync(jsonResultsFile, output); |
| 147 | + } |
| 148 | +} |
| 149 | + |
107 | 150 | mocha.run(function (failures) { |
| 151 | + onRunEnd(); |
108 | 152 | process.on('exit', function () { |
109 | | - process.exit(failures); // exit with non-zero status if there were failures |
| 153 | + process.exit(failures ? 1 : 0); // exit with non-zero status if there were failures |
110 | 154 | }); |
| 155 | +}).on(EVENT_TEST_PASS, (test) => { |
| 156 | + onTestEnd(test, JsonStatus.PASSED); |
| 157 | +}).on(EVENT_TEST_FAIL, (test) => { |
| 158 | + onTestEnd(test, JsonStatus.FAILED); |
| 159 | +}).on(EVENT_TEST_PENDING, (test) => { |
| 160 | + onTestEnd(test, JsonStatus.IGNORED); |
111 | 161 | }); |
0 commit comments