2019-02-20 16:10:18 +00:00
|
|
|
// Flags: --experimental-report
|
2018-09-05 14:39:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-02-20 16:10:18 +00:00
|
|
|
// Test producing a report via API call, using the no-hooks/no-signal interface.
|
2018-09-05 14:39:08 +00:00
|
|
|
const common = require('../common');
|
|
|
|
common.skipIfReportDisabled();
|
2019-02-20 16:10:18 +00:00
|
|
|
const assert = require('assert');
|
2019-03-08 15:30:15 +00:00
|
|
|
const { spawnSync } = require('child_process');
|
2019-02-22 20:04:18 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2019-02-20 16:10:18 +00:00
|
|
|
const helper = require('../common/report');
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
2018-09-05 14:39:08 +00:00
|
|
|
|
2019-02-20 16:10:18 +00:00
|
|
|
common.expectWarning('ExperimentalWarning',
|
|
|
|
'report is an experimental feature. This feature could ' +
|
|
|
|
'change at any time');
|
|
|
|
tmpdir.refresh();
|
2019-03-03 17:05:38 +00:00
|
|
|
process.report.directory = tmpdir.path;
|
2019-02-22 20:04:18 +00:00
|
|
|
|
|
|
|
function validate() {
|
|
|
|
const reports = helper.findReports(process.pid, tmpdir.path);
|
|
|
|
assert.strictEqual(reports.length, 1);
|
|
|
|
helper.validate(reports[0]);
|
|
|
|
fs.unlinkSync(reports[0]);
|
|
|
|
return reports[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test with no arguments.
|
2019-03-08 16:59:08 +00:00
|
|
|
process.report.writeReport();
|
2019-02-22 20:04:18 +00:00
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test with an error argument.
|
2019-03-08 16:59:08 +00:00
|
|
|
process.report.writeReport(new Error('test error'));
|
2019-02-22 20:04:18 +00:00
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test with a file argument.
|
2019-03-08 16:59:08 +00:00
|
|
|
const file = process.report.writeReport('custom-name-1.json');
|
2019-02-22 20:04:18 +00:00
|
|
|
const absolutePath = path.join(tmpdir.path, file);
|
|
|
|
assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
|
|
|
|
assert.strictEqual(file, 'custom-name-1.json');
|
|
|
|
helper.validate(absolutePath);
|
|
|
|
fs.unlinkSync(absolutePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test with file and error arguments.
|
2019-03-08 16:59:08 +00:00
|
|
|
const file = process.report.writeReport('custom-name-2.json',
|
|
|
|
new Error('test error'));
|
2019-02-22 20:04:18 +00:00
|
|
|
const absolutePath = path.join(tmpdir.path, file);
|
|
|
|
assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
|
|
|
|
assert.strictEqual(file, 'custom-name-2.json');
|
|
|
|
helper.validate(absolutePath);
|
|
|
|
fs.unlinkSync(absolutePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test with a filename option.
|
2019-03-03 17:05:38 +00:00
|
|
|
process.report.filename = 'custom-name-3.json';
|
2019-03-08 16:59:08 +00:00
|
|
|
const file = process.report.writeReport();
|
2019-02-22 20:04:18 +00:00
|
|
|
assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
|
2019-03-03 17:05:38 +00:00
|
|
|
const filename = path.join(process.report.directory, 'custom-name-3.json');
|
|
|
|
assert.strictEqual(file, process.report.filename);
|
2019-02-22 20:04:18 +00:00
|
|
|
helper.validate(filename);
|
|
|
|
fs.unlinkSync(filename);
|
|
|
|
}
|
2019-02-23 14:27:36 +00:00
|
|
|
|
|
|
|
// Test with an invalid file argument.
|
|
|
|
[null, 1, Symbol(), function() {}].forEach((file) => {
|
|
|
|
common.expectsError(() => {
|
2019-03-08 16:59:08 +00:00
|
|
|
process.report.writeReport(file);
|
2019-02-23 14:27:36 +00:00
|
|
|
}, { code: 'ERR_INVALID_ARG_TYPE' });
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test with an invalid error argument.
|
|
|
|
[null, 1, Symbol(), function() {}, 'foo'].forEach((error) => {
|
|
|
|
common.expectsError(() => {
|
2019-03-08 16:59:08 +00:00
|
|
|
process.report.writeReport('file', error);
|
2019-02-23 14:27:36 +00:00
|
|
|
}, { code: 'ERR_INVALID_ARG_TYPE' });
|
|
|
|
});
|
2019-03-08 15:30:15 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test the special "stdout" filename.
|
|
|
|
const args = ['--experimental-report', '-e',
|
2019-03-08 16:59:08 +00:00
|
|
|
'process.report.writeReport("stdout")'];
|
2019-03-08 15:30:15 +00:00
|
|
|
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
|
|
|
|
assert.strictEqual(child.status, 0);
|
|
|
|
assert.strictEqual(child.signal, null);
|
|
|
|
assert.strictEqual(helper.findReports(child.pid, tmpdir.path).length, 0);
|
|
|
|
helper.validateContent(child.stdout.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test the special "stderr" filename.
|
|
|
|
const args = ['--experimental-report', '-e',
|
2019-03-08 16:59:08 +00:00
|
|
|
'process.report.writeReport("stderr")'];
|
2019-03-08 15:30:15 +00:00
|
|
|
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
|
|
|
|
assert.strictEqual(child.status, 0);
|
|
|
|
assert.strictEqual(child.signal, null);
|
|
|
|
assert.strictEqual(child.stdout.toString().trim(), '');
|
|
|
|
assert.strictEqual(helper.findReports(child.pid, tmpdir.path).length, 0);
|
|
|
|
const report = child.stderr.toString().split('Node.js report completed')[0];
|
|
|
|
helper.validateContent(report);
|
|
|
|
}
|
2019-03-08 16:21:05 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test the case where the report file cannot be opened.
|
|
|
|
const reportDir = path.join(tmpdir.path, 'does', 'not', 'exist');
|
|
|
|
const args = ['--experimental-report',
|
2019-04-19 15:12:28 +00:00
|
|
|
`--report-directory=${reportDir}`,
|
2019-03-08 16:21:05 +00:00
|
|
|
'-e',
|
2019-03-08 16:59:08 +00:00
|
|
|
'process.report.writeReport()'];
|
2019-03-08 16:21:05 +00:00
|
|
|
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
|
|
|
|
|
|
|
|
assert.strictEqual(child.status, 0);
|
|
|
|
assert.strictEqual(child.signal, null);
|
|
|
|
assert.strictEqual(child.stdout.toString().trim(), '');
|
|
|
|
const stderr = child.stderr.toString();
|
|
|
|
assert(stderr.includes('Failed to open Node.js report file:'));
|
|
|
|
}
|