node/test/abort/test-abort-backtrace.js
James M Snell 8caa1dcee6 test: rely less on duplicative common test harness utilities
There are several cleanups here that are not just style nits...

1. The `common.isMainThread` was just a passthrough to the
   `isMainThread` export on the worker_thread module. It's
   use was inconsistent and just obfuscated the fact that
   the test file depend on the `worker_threads` built-in.
   By eliminating it we simplify the test harness a bit and
   make it clearer which tests depend on the worker_threads
   check.
2. The `common.isDumbTerminal` is fairly unnecesary since
   that just wraps a public API check.
3. Several of the `common.skipIf....` checks were inconsistently
   used and really don't need to be separate utility functions.

A key part of the motivation here is to work towards making more
of the tests more self-contained and less reliant on the common
test harness where possible.

PR-URL: https://github.com/nodejs/node/pull/56712
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-01-25 07:23:09 +00:00

71 lines
1.9 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const cp = require('child_process');
function getPrintedStackTrace(stderr) {
const lines = stderr.split('\n');
let state = 'initial';
const result = {
message: [],
nativeStack: [],
jsStack: [],
};
for (let i = 0; i < lines.length; ++i) {
const line = lines[i].trim();
if (line.length === 0) {
continue; // Skip empty lines.
}
switch (state) {
case 'initial':
result.message.push(line);
if (line.includes('Native stack trace')) {
state = 'native-stack';
} else {
result.message.push(line);
}
break;
case 'native-stack':
if (line.includes('JavaScript stack trace')) {
state = 'js-stack';
} else {
result.nativeStack.push(line);
}
break;
case 'js-stack':
result.jsStack.push(line);
break;
}
}
return result;
}
if (process.argv[2] === 'child') {
process.abort();
} else {
const child = cp.spawnSync(`${process.execPath}`, [`${__filename}`, 'child']);
const stderr = child.stderr.toString();
assert.strictEqual(child.stdout.toString(), '');
const { nativeStack, jsStack } = getPrintedStackTrace(stderr);
if (!nativeStack.every((frame, index) => frame.startsWith(`${index + 1}:`))) {
assert.fail(`Each frame should start with a frame number:\n${stderr}`);
}
// For systems that don't support backtraces, the native stack is
// going to be empty.
if (process.platform !== 'win32' && nativeStack.length > 0) {
const { getBinaryPath } = require('../common/shared-lib-util');
if (!nativeStack.some((frame) => frame.includes(`[${getBinaryPath()}]`))) {
assert.fail(`Some native stack frame include the binary name:\n${stderr}`);
}
}
if (jsStack.length > 0) {
assert(jsStack.some((frame) => frame.includes(__filename)));
}
}