-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
test_runner: support using --inspect
with --test
#44520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae799bd
test_runner: support using `--inspect` with `--test`
MoLow 9414d24
CR & tests
MoLow 2e344c3
CR
MoLow d1fdf1c
CR
MoLow 572ad0d
fixes
MoLow dea4784
Apply suggestions from code review
MoLow 85b5716
Update lib/internal/cluster/primary.js
MoLow a762de7
attampt fixing windows build
MoLow 1da1d5e
try fully sequential
MoLow c1a956c
lint
MoLow e137527
try debugging this
MoLow 3346b00
try
MoLow 92ea9bb
fix
MoLow b07d574
primordials
MoLow 2e1c14a
optimize
MoLow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
'use strict'; | ||
const { | ||
ArrayFrom, | ||
ArrayPrototypeConcat, | ||
ArrayPrototypeFilter, | ||
ArrayPrototypeIncludes, | ||
ArrayPrototypeJoin, | ||
ArrayPrototypePush, | ||
ArrayPrototypeSlice, | ||
ArrayPrototypeSome, | ||
ArrayPrototypeSort, | ||
ObjectAssign, | ||
PromisePrototypeThen, | ||
RegExpPrototypeExec, | ||
SafePromiseAll, | ||
SafeSet, | ||
} = primordials; | ||
|
@@ -21,7 +23,7 @@ const { | |
ERR_TEST_FAILURE, | ||
}, | ||
} = require('internal/errors'); | ||
const { validateArray } = require('internal/validators'); | ||
const { validateArray, validatePort } = require('internal/validators'); | ||
const { kEmptyObject } = require('internal/util'); | ||
const { createTestTree } = require('internal/test_runner/harness'); | ||
const { kSubtestsFailed, Test } = require('internal/test_runner/test'); | ||
|
@@ -32,6 +34,10 @@ const { | |
const { basename, join, resolve } = require('path'); | ||
const { once } = require('events'); | ||
|
||
const kMinPort = 1024; | ||
ljharb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const kMaxPort = 65535; | ||
const kInspectArgRegex = /--inspect(?:-brk|-port)?/; | ||
MoLow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const kInspectMsgRegex = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\/|Debugger attached|Waiting for the debugger to disconnect\.\.\./; | ||
const kFilterArgs = ['--test']; | ||
|
||
// TODO(cjihrig): Replace this with recursive readdir once it lands. | ||
|
@@ -96,29 +102,59 @@ function createTestFileList() { | |
return ArrayPrototypeSort(ArrayFrom(testFiles)); | ||
} | ||
|
||
function isUsingInspector() { | ||
return ArrayPrototypeSome(process.execArgv, (arg) => RegExpPrototypeExec(kInspectArgRegex, arg) !== null) || | ||
RegExpPrototypeExec(kInspectArgRegex, process.env.NODE_OPTIONS) !== null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this kind of seems like it should just be a boolean property available on |
||
|
||
function filterExecArgv(arg) { | ||
return !ArrayPrototypeIncludes(kFilterArgs, arg); | ||
} | ||
|
||
function runTestFile(path, root) { | ||
let debugPortOffset = 1; | ||
function getRunArgs({ path, inspectPort, usingInspector }) { | ||
const argv = ArrayPrototypeFilter(process.execArgv, filterExecArgv); | ||
if (usingInspector) { | ||
if (typeof inspectPort === 'function') { | ||
inspectPort = inspectPort(); | ||
} else if (inspectPort == null) { | ||
inspectPort = process.debugPort + debugPortOffset; | ||
if (inspectPort > kMaxPort) | ||
inspectPort = inspectPort - kMaxPort + kMinPort - 1; | ||
debugPortOffset++; | ||
} | ||
validatePort(inspectPort); | ||
|
||
ArrayPrototypePush(argv, `--inspect-port=${inspectPort}`); | ||
} | ||
ArrayPrototypePush(argv, path); | ||
return argv; | ||
} | ||
|
||
function runTestFile(path, root, usingInspector, inspectPort) { | ||
const subtest = root.createSubtest(Test, path, async (t) => { | ||
const args = ArrayPrototypeConcat( | ||
ArrayPrototypeFilter(process.execArgv, filterExecArgv), | ||
path); | ||
const args = getRunArgs({ path, inspectPort, usingInspector }); | ||
|
||
const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8' }); | ||
// TODO(cjihrig): Implement a TAP parser to read the child's stdout | ||
// instead of just displaying it all if the child fails. | ||
let err; | ||
let stderr = ''; | ||
|
||
child.on('error', (error) => { | ||
err = error; | ||
}); | ||
|
||
const { 0: { 0: code, 1: signal }, 1: stdout, 2: stderr } = await SafePromiseAll([ | ||
child.stderr.on('data', (data) => { | ||
if (usingInspector && RegExpPrototypeExec(kInspectMsgRegex, data) !== null) { | ||
MoLow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
process.stderr.write(data); | ||
} | ||
stderr += data; | ||
}); | ||
|
||
const { 0: { 0: code, 1: signal }, 1: stdout } = await SafePromiseAll([ | ||
once(child, 'exit', { signal: t.signal }), | ||
child.stdout.toArray({ signal: t.signal }), | ||
child.stderr.toArray({ signal: t.signal }), | ||
]); | ||
|
||
if (code !== 0 || signal !== null) { | ||
|
@@ -128,7 +164,7 @@ function runTestFile(path, root) { | |
exitCode: code, | ||
signal: signal, | ||
stdout: ArrayPrototypeJoin(stdout, ''), | ||
stderr: ArrayPrototypeJoin(stderr, ''), | ||
stderr, | ||
// The stack will not be useful since the failures came from tests | ||
// in a child process. | ||
stack: undefined, | ||
|
@@ -145,19 +181,20 @@ function run(options) { | |
if (options === null || typeof options !== 'object') { | ||
options = kEmptyObject; | ||
} | ||
const { concurrency, timeout, signal, files } = options; | ||
const { concurrency, timeout, signal, files, inspectPort } = options; | ||
|
||
if (files != null) { | ||
validateArray(files, 'options.files'); | ||
} | ||
|
||
const root = createTestTree({ concurrency, timeout, signal }); | ||
const testFiles = files ?? createTestFileList(); | ||
const usingInspector = isUsingInspector(); | ||
|
||
PromisePrototypeThen(SafePromiseAll(testFiles, (path) => runTestFile(path, root)), | ||
PromisePrototypeThen(SafePromiseAll(testFiles, (path) => runTestFile(path, root, usingInspector, inspectPort)), | ||
() => root.postRun()); | ||
|
||
return root.reporter; | ||
} | ||
|
||
module.exports = { run }; | ||
module.exports = { run, isUsingInspector }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir.js'); | ||
const { NodeInstance } = require('../common/inspector-helper.js'); | ||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
const { join } = require('path'); | ||
const fixtures = require('../common/fixtures'); | ||
const testFixtures = fixtures.path('test-runner'); | ||
|
||
common.skipIfInspectorDisabled(); | ||
tmpdir.refresh(); | ||
|
||
async function debugTest() { | ||
const child = new NodeInstance(['--test', '--inspect-brk=0'], undefined, join(testFixtures, 'index.test.js')); | ||
|
||
let stdout = ''; | ||
let stderr = ''; | ||
child.on('stdout', (line) => stdout += line); | ||
child.on('stderr', (line) => stderr += line); | ||
|
||
const session = await child.connectInspectorSession(); | ||
|
||
await session.send([ | ||
{ method: 'Runtime.enable' }, | ||
{ method: 'NodeRuntime.notifyWhenWaitingForDisconnect', | ||
params: { enabled: true } }, | ||
{ method: 'Runtime.runIfWaitingForDebugger' }]); | ||
|
||
|
||
await session.waitForNotification((notification) => { | ||
return notification.method === 'NodeRuntime.waitingForDisconnect'; | ||
}); | ||
|
||
session.disconnect(); | ||
assert.match(stderr, | ||
/Warning: Using the inspector with --test forces running at a concurrency of 1\. Use the inspectPort option to run with concurrency/); | ||
} | ||
|
||
debugTest().then(common.mustCall()); | ||
|
||
|
||
{ | ||
const args = ['--test', '--inspect=0', join(testFixtures, 'index.js')]; | ||
const child = spawnSync(process.execPath, args); | ||
|
||
assert.match(child.stderr.toString(), | ||
/Warning: Using the inspector with --test forces running at a concurrency of 1\. Use the inspectPort option to run with concurrency/); | ||
const stdout = child.stdout.toString(); | ||
assert.match(stdout, /not ok 1 - .+index\.js/); | ||
assert.match(stdout, /stderr: \|-\r?\n\s+Debugger listening on/); | ||
assert.strictEqual(child.status, 1); | ||
assert.strictEqual(child.signal, null); | ||
} | ||
|
||
|
||
{ | ||
// File not found. | ||
const args = ['--test', '--inspect=0', 'a-random-file-that-does-not-exist.js']; | ||
const child = spawnSync(process.execPath, args); | ||
|
||
const stderr = child.stderr.toString(); | ||
assert.strictEqual(child.stdout.toString(), ''); | ||
assert.match(stderr, /^Could not find/); | ||
assert.doesNotMatch(stderr, /Warning: Using the inspector with --test forces running at a concurrency of 1\. Use the inspectPort option to run with concurrency/); | ||
assert.strictEqual(child.status, 1); | ||
assert.strictEqual(child.signal, null); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.