Skip to content

Commit 983fb6e

Browse files
authored
Merge pull request #83 from bocoup/json-reporter
Add CLI option: `--reporter-keys`
2 parents b484d18 + 8790e87 commit 983fb6e

File tree

6 files changed

+72
-29
lines changed

6 files changed

+72
-29
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Run `test262-harness --help` for details on the various configuration options.
3030
| `--hostPath` | Path to the host executable.
3131
| `--hostArgs` | Any additional arguments to pass to the host when invoking it (eg. `--harmony`, `--es6all`, etc).
3232
| `-t`, `--threads` | Run this many tests in parallel. Note that the browser runners don't work great with t > 1.
33-
| `-r`, `--reporter` | Selects test case result format. Currently either `json` or `simple`. Default `simple`.
33+
| `-r`, `--reporter` | Format of data written to standard output. Currently either `json` or `simple`. Default `simple`.
3434
|`--test262Dir` | Optional. Root test262 directory and is used to locate the includes directory.
3535
|`--includesDir` | Includes directory. By default inferred from test262Dir or else detected by walking upward from the first test found.
3636
|`--prelude` | Path to a file to include before every test (useful for testing polyfills for example)
37-
37+
|`--reporter-keys` | Comma-separated list of keys to include in output of `json` reporter.
3838
3939

bin/run.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,29 @@ let includesDir = argv.includesDir;
2626
// print version of test262-harness
2727
if (argv.version) {
2828
printVersion();
29-
process.exit(0);
29+
return;
3030
}
3131

3232
// initialize reporter by attempting to load lib/reporters/${reporter}
3333
// defaults to 'simple'
3434
let reporter;
35+
let reporterOpts = {};
3536
if (fs.existsSync(Path.join(__dirname, '../lib/reporters', `${argv.reporter}.js`))) {
3637
reporter = require(`../lib/reporters/${argv.reporter}.js`);
3738
} else {
3839
console.error(`Reporter ${argv.reporter} not found.`);
39-
process.exit(1);
40+
process.exitCode = 1;
41+
return;
42+
}
43+
44+
if (argv.reporterKeys) {
45+
if (argv.reporter !== 'json') {
46+
console.error('`--reporter-keys` option applies only to the `json` reporter.');
47+
process.exitCode = 1;
48+
return;
49+
}
50+
51+
reporterOpts.reporterKeys = argv.reporterKeys.split(',');
4052
}
4153

4254
// load preload contents
@@ -57,7 +69,8 @@ if (argv.hostType) {
5769

5870
if (!argv.hostPath) {
5971
console.error('Missing host path. Pass --hostPath with a path to the host executable you want to test.');
60-
process.exit(1);
72+
process.exitCode = 1;
73+
return;
6174
}
6275

6376
hostPath = argv.hostPath;
@@ -76,7 +89,8 @@ argv.timeout = argv.timeout || DEFAULT_TEST_TIMEOUT;
7689
// Show help if no arguments provided
7790
if (!argv._.length) {
7891
cli.showHelp();
79-
process.exit(1);
92+
process.exitCode = 1;
93+
return;
8094
}
8195

8296
// Test Pipeline
@@ -95,7 +109,7 @@ const results = rawResults.map(test => {
95109
return test;
96110
});
97111
const resultEmitter = resultsEmitter(results);
98-
reporter(resultEmitter);
112+
reporter(resultEmitter, reporterOpts);
99113

100114
function printVersion() {
101115
const p = require(Path.resolve(__dirname, "..", "package.json"));

lib/cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ const yargv = yargs
1515
.nargs('threads', 1)
1616
.default('threads', 1)
1717
.alias('threads', 't')
18-
.describe('reporter', 'select a reporter to use (simple or json)')
18+
.describe('reporter', 'format of data written to standard output')
19+
.choices('reporter', ['simple', 'json'])
1920
.nargs('reporter', 1)
2021
.alias('reporter', 'r')
2122
.default('reporter', 'simple')
23+
.describe('reporter-keys', 'comma-separated list of keys to include in JSON ouput')
2224
.help('help')
2325
.alias('help', 'h')
2426
.describe('timeout', 'test timeout (in ms, default 10000)')

lib/pick.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = function(obj, keys) {
4+
let picked = {};
5+
6+
keys.forEach(function(key) {
7+
picked[key] = obj[key];
8+
});
9+
10+
return picked;
11+
};

lib/reporters/json.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
2+
const pick = require('../pick');
23

3-
function jsonReporter(results) {
4+
function jsonReporter(results, opts) {
45
let started = false;
56

67
results.on('start', function () {
@@ -12,13 +13,21 @@ function jsonReporter(results) {
1213
});
1314

1415
results.on('test end', function (test) {
16+
let toEmit;
17+
1518
if (started) {
1619
process.stdout.write(',');
1720
} else {
1821
started = true;
1922
}
2023

21-
console.log(JSON.stringify(test));
24+
if (opts.reporterKeys) {
25+
toEmit = pick(test, opts.reporterKeys);
26+
} else {
27+
toEmit = test;
28+
}
29+
30+
console.log(JSON.stringify(toEmit));
2231
});
2332
}
2433

test/test.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ const path = require('path');
44
const cp = require('child_process');
55

66
Promise.all([
7-
run(),
8-
run({ prelude: true })
7+
run(['test/collateral/**/*.js']),
8+
run(['--prelude', './test/test-prelude.js', 'test/collateral/bothStrict.js']),
9+
run(['--reporter-keys', 'attrs,result', 'test/collateral/bothStrict.js']),
10+
run(['--reporter-keys', 'rawResult,attrs,result', 'test/collateral/bothStrict.js']),
11+
run(['--reporter-keys', 'attrs,rawResult,result', 'test/collateral/bothStrict.js']),
12+
run(['--reporter-keys', 'attrs,result,rawResult', 'test/collateral/bothStrict.js'])
913
])
1014
.then(validate)
1115
.catch(reportRunError);
@@ -16,26 +20,16 @@ function reportRunError(error) {
1620
process.exit(1);
1721
}
1822

19-
function run(options = { prelude: false }) {
23+
function run(extraArgs) {
2024
return new Promise((resolve, reject) => {
2125
let stdout = '';
2226
let stderr = '';
2327
let args = [
24-
'--hostType', 'node',
25-
'--hostPath', process.execPath,
26-
'-r', 'json',
27-
'--includesDir', './test/test-includes',
28-
];
29-
30-
if (options.prelude) {
31-
args.push(
32-
'--prelude',
33-
'./test/test-prelude.js',
34-
'test/collateral/bothStrict.js'
35-
);
36-
} else {
37-
args.push('test/collateral/**/*.js');
38-
}
28+
'--hostType', 'node',
29+
'--hostPath', process.execPath,
30+
'-r', 'json',
31+
'--includesDir', './test/test-includes',
32+
].concat(extraArgs);
3933

4034
const child = cp.fork('bin/run.js', args, { silent: true });
4135

@@ -56,9 +50,16 @@ function run(options = { prelude: false }) {
5650
}
5751

5852
function validate(records) {
59-
const [normal, prelude] = records;
53+
const [
54+
normal, prelude, withoutRawResult, withRawResult1, withRawResult2,
55+
withRawResult3
56+
] = records;
6057
validateResultRecords(normal);
6158
validateResultRecords(prelude, { prelude: true });
59+
validateResultRecords(withoutRawResult, { noRawResult: true });
60+
validateResultRecords(withRawResult1);
61+
validateResultRecords(withRawResult2);
62+
validateResultRecords(withRawResult3);
6263
}
6364

6465
function validateResultRecords(records, options = { prelude: false }) {
@@ -86,6 +87,12 @@ function validateResultRecords(records, options = { prelude: false }) {
8687
test.assert(record.rawResult.stdout.indexOf("prelude!") > -1, 'Has prelude content');
8788
}
8889

90+
if (options.noRawResult) {
91+
test.equal(record.rawResult, undefined);
92+
} else {
93+
test.equal(typeof record.rawResult, 'object');
94+
}
95+
8996
test.end();
9097
});
9198
});

0 commit comments

Comments
 (0)