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.
|
2020-03-13 03:24:01 +00:00
|
|
|
require('../common');
|
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
|
|
|
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);
|
2019-06-25 16:09:14 +00:00
|
|
|
helper.validate(reports[0], arguments[0]);
|
2019-02-22 20:04:18 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:46:36 +00:00
|
|
|
{
|
|
|
|
// Test with an error with one line stack
|
|
|
|
const error = new Error();
|
|
|
|
error.stack = 'only one line';
|
|
|
|
process.report.writeReport(error);
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:09:14 +00:00
|
|
|
{
|
|
|
|
const error = new Error();
|
|
|
|
error.foo = 'goo';
|
|
|
|
process.report.writeReport(error);
|
|
|
|
validate([['javascriptStack.errorProperties.foo', 'goo']]);
|
|
|
|
}
|
|
|
|
|
2019-02-22 20:04:18 +00:00
|
|
|
{
|
|
|
|
// Test with a file argument.
|
2019-03-08 16:59:08 +00:00
|
|
|
const file = process.report.writeReport('custom-name-1.json');
|
2023-08-21 16:41:53 +00:00
|
|
|
const absolutePath = tmpdir.resolve(file);
|
2019-02-22 20:04:18 +00:00
|
|
|
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'));
|
2023-08-21 16:41:53 +00:00
|
|
|
const absolutePath = tmpdir.resolve(file);
|
2019-02-22 20:04:18 +00:00
|
|
|
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) => {
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
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) => {
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
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.
|
2020-03-13 03:24:01 +00:00
|
|
|
const args = ['-e', '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.
|
2020-03-13 03:24:01 +00:00
|
|
|
const args = ['-e', '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.
|
2023-08-21 16:41:53 +00:00
|
|
|
const reportDir = tmpdir.resolve('does', 'not', 'exist');
|
2020-03-13 03:24:01 +00:00
|
|
|
const args = [`--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:'));
|
|
|
|
}
|