diff options
author | Matteo Collina <hello@matteocollina.com> | 2018-07-04 19:55:12 +0200 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-07-07 00:04:52 +0200 |
commit | 19795d83833de7489afec583f8773ee9c0452f70 (patch) | |
tree | 0e495abfe821e92a158b757fcb0a24715fc7d863 /test | |
parent | dd023df135207086c86129aa1683ea4688f97f53 (diff) | |
download | android-node-v8-19795d83833de7489afec583f8773ee9c0452f70.tar.gz android-node-v8-19795d83833de7489afec583f8773ee9c0452f70.tar.bz2 android-node-v8-19795d83833de7489afec583f8773ee9c0452f70.zip |
inspector: expose original console
Adds require('inspector').console, mapping it to the original
global.console of V8. This enables applications to send messages to
the inspector console programmatically.
Fixes: https://github.com/nodejs/node/issues/21651
PR-URL: https://github.com/nodejs/node/pull/21659
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/common/inspector-helper.js | 10 | ||||
-rw-r--r-- | test/sequential/test-inspector-console.js | 39 |
2 files changed, 47 insertions, 2 deletions
diff --git a/test/common/inspector-helper.js b/test/common/inspector-helper.js index e590349f9c..1372604979 100644 --- a/test/common/inspector-helper.js +++ b/test/common/inspector-helper.js @@ -7,6 +7,7 @@ const fixtures = require('../common/fixtures'); const { spawn } = require('child_process'); const { parse: parseURL } = require('url'); const { getURLFromFilePath } = require('internal/url'); +const { EventEmitter } = require('events'); const _MAINSCRIPT = fixtures.path('loop.js'); const DEBUG = false; @@ -311,10 +312,12 @@ class InspectorSession { } } -class NodeInstance { +class NodeInstance extends EventEmitter { constructor(inspectorFlags = ['--inspect-brk=0'], scriptContents = '', scriptFile = _MAINSCRIPT) { + super(); + this._scriptPath = scriptFile; this._script = scriptFile ? null : scriptContents; this._portCallback = null; @@ -326,7 +329,10 @@ class NodeInstance { this._unprocessedStderrLines = []; this._process.stdout.on('data', makeBufferingDataCallback( - (line) => console.log('[out]', line))); + (line) => { + this.emit('stdout', line); + console.log('[out]', line); + })); this._process.stderr.on('data', makeBufferingDataCallback( (message) => this.onStderrLine(message))); diff --git a/test/sequential/test-inspector-console.js b/test/sequential/test-inspector-console.js new file mode 100644 index 0000000000..6a06c79888 --- /dev/null +++ b/test/sequential/test-inspector-console.js @@ -0,0 +1,39 @@ +// Flags: --expose-internals +'use strict'; + +const common = require('../common'); +common.skipIfInspectorDisabled(); + +const { NodeInstance } = require('../common/inspector-helper.js'); +const assert = require('assert'); + +async function runTest() { + const script = 'require(\'inspector\').console.log(\'hello world\');'; + const child = new NodeInstance('--inspect-brk=0', script, ''); + + let out = ''; + child.on('stdout', (line) => out += line); + + const session = await child.connectInspectorSession(); + + const commands = [ + { 'method': 'Runtime.enable' }, + { 'method': 'Runtime.runIfWaitingForDebugger' } + ]; + + session.send(commands); + + const msg = await session.waitForNotification('Runtime.consoleAPICalled'); + + assert.strictEqual(msg.params.type, 'log'); + assert.deepStrictEqual(msg.params.args, [{ + type: 'string', + value: 'hello world' + }]); + assert.strictEqual(out, ''); + + session.disconnect(); +} + +common.crashOnUnhandledRejection(); +runTest(); |