mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
test: consolidate triggerReport() tests
PR-URL: https://github.com/nodejs/node/pull/26268 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
8e2cc5e440
commit
a382b52fd8
4 files changed
+54
-93
No files matched your search
@@ -5,6 +5,8 @@
|
||||
const common = require('../common');
|
||||
common.skipIfReportDisabled();
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const helper = require('../common/report');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
|
||||
@@ -13,7 +15,55 @@ common.expectWarning('ExperimentalWarning',
|
||||
'change at any time');
|
||||
tmpdir.refresh();
|
||||
process.report.setOptions({ path: tmpdir.path });
|
||||
process.report.triggerReport();
|
||||
const reports = helper.findReports(process.pid, tmpdir.path);
|
||||
assert.strictEqual(reports.length, 1);
|
||||
helper.validate(reports[0]);
|
||||
|
||||
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.
|
||||
process.report.triggerReport();
|
||||
validate();
|
||||
}
|
||||
|
||||
{
|
||||
// Test with an error argument.
|
||||
process.report.triggerReport(new Error('test error'));
|
||||
validate();
|
||||
}
|
||||
|
||||
{
|
||||
// Test with a file argument.
|
||||
const file = process.report.triggerReport('custom-name-1.json');
|
||||
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.
|
||||
const file = process.report.triggerReport('custom-name-2.json',
|
||||
new Error('test error'));
|
||||
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.
|
||||
const filename = path.join(tmpdir.path, 'custom-name-3.json');
|
||||
process.report.setOptions({ filename });
|
||||
const file = process.report.triggerReport();
|
||||
assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
|
||||
assert.strictEqual(file, filename);
|
||||
helper.validate(filename);
|
||||
fs.unlinkSync(filename);
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// Testcase for passing an error object to the API call.
|
||||
const common = require('../common');
|
||||
common.skipIfReportDisabled();
|
||||
const assert = require('assert');
|
||||
if (process.argv[2] === 'child') {
|
||||
try {
|
||||
throw new Error('Testing error handling');
|
||||
} catch (err) {
|
||||
process.report.triggerReport(err);
|
||||
}
|
||||
} else {
|
||||
const helper = require('../common/report.js');
|
||||
const spawn = require('child_process').spawn;
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
const child = spawn(process.execPath, ['--experimental-report',
|
||||
__filename, 'child'],
|
||||
{ cwd: tmpdir.path });
|
||||
child.on('exit', common.mustCall((code) => {
|
||||
const report_msg = 'No reports found';
|
||||
const process_msg = 'Process exited unexpectedly';
|
||||
assert.strictEqual(code, 0, process_msg);
|
||||
const reports = helper.findReports(child.pid, tmpdir.path);
|
||||
assert.strictEqual(reports.length, 1, report_msg);
|
||||
const report = reports[0];
|
||||
helper.validate(report);
|
||||
}));
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// Tests when a report is triggered with a given filename.
|
||||
const common = require('../common');
|
||||
common.skipIfReportDisabled();
|
||||
const filename = 'myreport.json';
|
||||
if (process.argv[2] === 'child') {
|
||||
process.report.triggerReport(filename);
|
||||
} else {
|
||||
const helper = require('../common/report.js');
|
||||
const spawn = require('child_process').spawn;
|
||||
const assert = require('assert');
|
||||
const { join } = require('path');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
|
||||
const child = spawn(process.execPath,
|
||||
['--experimental-report', __filename, 'child'],
|
||||
{ cwd: tmpdir.path });
|
||||
child.on('exit', common.mustCall((code) => {
|
||||
const process_msg = 'Process exited unexpectedly';
|
||||
assert.strictEqual(code, 0, process_msg + ':' + code);
|
||||
const reports = helper.findReports(child.pid, tmpdir.path);
|
||||
assert.strictEqual(reports.length, 0,
|
||||
`Found unexpected report ${reports[0]}`);
|
||||
const report = join(tmpdir.path, filename);
|
||||
helper.validate(report);
|
||||
}));
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// Tests when a report is triggered with options set.
|
||||
const common = require('../common');
|
||||
common.skipIfReportDisabled();
|
||||
const filename = 'myreport.json';
|
||||
if (process.argv[2] === 'child') {
|
||||
process.report.setOptions({ filename: filename });
|
||||
process.report.triggerReport();
|
||||
} else {
|
||||
const helper = require('../common/report.js');
|
||||
const spawn = require('child_process').spawn;
|
||||
const assert = require('assert');
|
||||
const { join } = require('path');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
|
||||
const child = spawn(process.execPath,
|
||||
['--experimental-report', __filename, 'child'],
|
||||
{ cwd: tmpdir.path });
|
||||
child.on('exit', common.mustCall((code) => {
|
||||
const process_msg = 'Process exited unexpectedly';
|
||||
assert.strictEqual(code, 0, process_msg + ':' + code);
|
||||
const reports = helper.findReports(child.pid, tmpdir.path);
|
||||
assert.strictEqual(reports.length, 0,
|
||||
`Found unexpected report ${reports[0]}`);
|
||||
const report = join(tmpdir.path, filename);
|
||||
helper.validate(report);
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user