node/test/parallel/test-vm-debug-context.js
James M Snell 1fdbaed2f2
test: begin normalizing fixtures use
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

Backport-PR-URL: https://github.com/nodejs/node/pull/16265
PR-URL: https://github.com/nodejs/node/pull/14332
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2017-10-26 00:21:30 -04:00

97 lines
3 KiB
JavaScript

/* eslint-disable no-debugger */
'use strict';
const common = require('../common');
const assert = require('assert');
const vm = require('vm');
const { spawn } = require('child_process');
const fixtures = require('../common/fixtures');
assert.throws(function() {
vm.runInDebugContext('*');
}, /SyntaxError/);
assert.throws(function() {
vm.runInDebugContext({ toString: assert.fail });
}, /AssertionError/);
assert.throws(function() {
vm.runInDebugContext('throw URIError("BAM")');
}, /URIError/);
assert.throws(function() {
vm.runInDebugContext('(function(f) { f(f) })(function(f) { f(f) })');
}, /RangeError/);
assert.strictEqual(typeof vm.runInDebugContext('this'), 'object');
assert.strictEqual(typeof vm.runInDebugContext('Debug'), 'object');
assert.strictEqual(vm.runInDebugContext(), undefined);
assert.strictEqual(vm.runInDebugContext(0), 0);
assert.strictEqual(vm.runInDebugContext(null), null);
assert.strictEqual(vm.runInDebugContext(undefined), undefined);
// See https://github.com/nodejs/node/issues/1190, accessing named interceptors
// and accessors inside a debug event listener should not crash.
{
const Debug = vm.runInDebugContext('Debug');
let breaks = 0;
function ondebugevent(evt, exc) {
if (evt !== Debug.DebugEvent.Break) return;
exc.frame(0).evaluate('process.env').properties(); // Named interceptor.
exc.frame(0).evaluate('process.title').getTruncatedValue(); // Accessor.
breaks += 1;
}
function breakpoint() {
debugger;
}
assert.strictEqual(breaks, 0);
Debug.setListener(ondebugevent);
assert.strictEqual(breaks, 0);
breakpoint();
assert.strictEqual(breaks, 1);
}
// Can set listeners and breakpoints on a single line file
{
const Debug = vm.runInDebugContext('Debug');
const fn = require(fixtures.path('exports-function-with-param'));
let called = false;
Debug.setListener(function(event, state, data) {
if (data.constructor.name === 'BreakEvent') {
called = true;
}
});
Debug.setBreakPoint(fn);
fn('foo');
assert.strictEqual(Debug.showBreakPoints(fn), '(arg) { [B0]return arg; }');
assert.strictEqual(called, true);
}
// See https://github.com/nodejs/node/issues/1190, fatal errors should not
// crash the process.
const script = fixtures.path('vm-run-in-debug-context.js');
let proc = spawn(process.execPath, [script]);
const data = [];
proc.stdout.on('data', common.mustNotCall());
proc.stderr.on('data', data.push.bind(data));
proc.stderr.once('end', common.mustCall(function() {
const haystack = Buffer.concat(data).toString('utf8');
assert(/SyntaxError: Unexpected token \*/.test(haystack));
}));
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
assert.strictEqual(exitCode, 1);
assert.strictEqual(signalCode, null);
}));
proc = spawn(process.execPath, [script, 'handle-fatal-exception']);
proc.stdout.on('data', common.mustNotCall());
proc.stderr.on('data', common.mustNotCall());
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
assert.strictEqual(exitCode, 42);
assert.strictEqual(signalCode, null);
}));