Skip to content

Commit 08744d2

Browse files
woessossama-ismaili
authored andcommitted
Add support for dumping JSON status file from Node.js unit tests.
(cherry picked from commit 2735871)
1 parent b2f5806 commit 08744d2

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

graal-nodejs/test/graal/index.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -42,10 +42,28 @@
4242
var Mocha = require('mocha');
4343
var fs = require('fs');
4444

45+
const {
46+
EVENT_TEST_FAIL,
47+
EVENT_TEST_PASS,
48+
EVENT_TEST_PENDING
49+
} = Mocha.Runner.constants;
50+
4551
var limitToSuites = [];
52+
var jsonResultsFile = null;
4653
for (var i = 2; i < process.argv.length; i++) {
4754
var arg = process.argv[i];
4855
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+
}
4967
suite = process.argv[i];
5068
console.log("limiting to suite:", suite);
5169
limitToSuites.push(suite);
@@ -104,8 +122,40 @@ for (var i = 0; i < filesLength; i++) {
104122
}
105123
}
106124

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+
107150
mocha.run(function (failures) {
151+
onRunEnd();
108152
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
110154
});
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);
111161
});

0 commit comments

Comments
 (0)