-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allow Node arguments to be configured #2272
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
Changes from 9 commits
103a243
0cc0abd
3f6633c
ae70f22
420c9b7
8e51406
d4acaeb
adfc212
26c4448
5b99832
9f00c05
5cf8dd7
28a2c06
1ae69c4
c1a99d4
8e649b9
e00e80f
03fca0a
9fcdc7b
ae3836b
be272c7
b5d2796
18b7074
037cc33
518c44d
7aad65a
0e21309
c40194a
f01a038
438148c
f670481
7ec7f34
40e7716
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
|
@@ -15,6 +16,8 @@ const makeDir = require('make-dir'); | |
const ms = require('ms'); | ||
const chunkd = require('chunkd'); | ||
const Emittery = require('emittery'); | ||
const minimist = require('minimist'); | ||
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. Could we use https://www.npmjs.com/package/yargs-parser instead? It's already in our dependency tree as part of (Though it looks like so is 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. I choose minimist, because it has reverse package, maybe there is another package for it? 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. I reckon |
||
const dargs = require('dargs'); | ||
const globs = require('./globs'); | ||
const RunStatus = require('./run-status'); | ||
const fork = require('./fork'); | ||
|
@@ -320,28 +323,28 @@ class Api extends Emittery { | |
} | ||
|
||
async _computeForkExecArgv() { | ||
const execArgv = this.options.testOnlyExecArgv || process.execArgv; | ||
if (execArgv.length === 0) { | ||
return Promise.resolve(execArgv); | ||
} | ||
const {nodeArguments: configExecArgs} = this.options; | ||
|
||
// --inspect-brk is used in addition to --inspect to break on first line and wait | ||
const inspectArgIndex = execArgv.findIndex(arg => /^--inspect(-brk)?($|=)/.test(arg)); | ||
if (inspectArgIndex === -1) { | ||
return Promise.resolve(execArgv); | ||
} | ||
const mainProcessArgs = minimist(process.execArgv); | ||
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. Could we cache this? This function runs for every test file. 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. Not sure, what I correctly understand you, check please |
||
|
||
const port = await getPort(); | ||
const forkExecArgv = execArgv.slice(); | ||
let flagName = '--inspect'; | ||
const oldValue = forkExecArgv[inspectArgIndex]; | ||
if (oldValue.includes('brk')) { | ||
flagName += '-brk'; | ||
} | ||
const args = { | ||
...mainProcessArgs, | ||
...configExecArgs | ||
}; | ||
|
||
forkExecArgv[inspectArgIndex] = `${flagName}=${port}`; | ||
const hasInspect = args.inspect || args['inspect-brk']; | ||
if (hasInspect) { | ||
if (args.inspect && args['inspect-brk']) { | ||
throw new Error('Flags inspect and inspect-brk provided simultaneously'); | ||
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. Note to self: check how this ends up being reported. |
||
} | ||
|
||
const port = await getPort(); | ||
|
||
const flagName = args.inspect ? 'inspect' : 'inspect-brk'; | ||
args[flagName] = port; | ||
} | ||
|
||
return forkExecArgv; | ||
return dargs(args); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ exports.run = async () => { // eslint-disable-line complexity | |
--color Force color output | ||
--no-color Disable color output | ||
--reset-cache Reset AVA's compilation cache and exit | ||
--node-arguments Configure Node.js arguments used to launch worker processes (For example --node-arguments="--frozen-intrinsics --experimental-modules") | ||
--config JavaScript file for AVA to read its config from, instead of using package.json | ||
or ava.config.js files | ||
|
||
|
@@ -115,6 +116,10 @@ exports.run = async () => { // eslint-disable-line complexity | |
type: 'boolean', | ||
default: false | ||
}, | ||
'node-arguments': { | ||
type: 'string', | ||
default: '' | ||
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. Can we remove this default? See my comment for |
||
}, | ||
'--': { | ||
type: 'string' | ||
} | ||
|
@@ -180,6 +185,7 @@ exports.run = async () => { // eslint-disable-line complexity | |
const Watcher = require('./watcher'); | ||
const babelManager = require('./babel-manager'); | ||
const normalizeExtensions = require('./extensions'); | ||
const normalizeNodeArguments = require('./node-arguments'); | ||
const {normalizeGlobs} = require('./globs'); | ||
const validateEnvironmentVariables = require('./environment-variables'); | ||
|
||
|
@@ -214,6 +220,13 @@ exports.run = async () => { // eslint-disable-line complexity | |
exit(error.message); | ||
} | ||
|
||
let nodeArguments; | ||
try { | ||
nodeArguments = normalizeNodeArguments(conf.nodeArguments, cli.flags.nodeArguments); | ||
} catch (error) { | ||
exit(error.message); | ||
} | ||
|
||
// Copy resultant cli.flags into conf for use with Api and elsewhere | ||
Object.assign(conf, cli.flags); | ||
|
||
|
@@ -265,6 +278,7 @@ exports.run = async () => { // eslint-disable-line complexity | |
snapshotDir: conf.snapshotDir ? path.resolve(projectDir, conf.snapshotDir) : null, | ||
timeout: conf.timeout, | ||
updateSnapshots: conf.updateSnapshots, | ||
nodeArguments, | ||
workerArgv: cli.flags['--'] | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
'use strict'; | ||||||
|
||||||
const minimist = require('minimist'); | ||||||
const omit = require('lodash/omit'); | ||||||
|
||||||
/** | ||||||
* @param {string[]} confParams | ||||||
* @param {string} cliParams | ||||||
* @return {object} | ||||||
*/ | ||||||
function normalizeNodeArguments(confParams, cliParams) { | ||||||
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. If we can assume the arguments may be
Suggested change
And we can get rid of the JSDoc comment too. We do need to enforce |
||||||
return omit({ | ||||||
...minimist(confParams || []), | ||||||
...minimist(cliParams.split(' ')) | ||||||
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. Are there Node.js arguments that take strings? Cause then splitting on the empty string won't work. |
||||||
}, '_'); | ||||||
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. Is the const { _, ...normalized } = { ...minimist(), ...minimist() };
return normalized; |
||||||
} | ||||||
|
||||||
module.exports = normalizeNodeArguments; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import test from '../..'; | ||
|
||
test('exec arguments includes --throw-deprecation and --zero-fill-buffers', t => { | ||
t.plan(2); | ||
t.truthy(process.execArgv.includes('--throw-deprecation')); | ||
t.truthy(process.execArgv.includes('--zero-fill-buffers')); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"ava": { | ||
"nodeArguments": [ | ||
"--require", | ||
"./setup.js" | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global.SETUP_CALLED = true; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import test from '../../..'; | ||
|
||
test('works', t => { | ||
t.plan(2); | ||
t.truthy(global.SETUP_CALLED); | ||
t.truthy(process.execArgv.includes('--require')); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
const {test} = require('tap'); | ||
const {execCli} = require('../helper/cli'); | ||
|
||
test('passes node arguments to workers', t => { | ||
t.plan(1); | ||
execCli(['--node-arguments="--throw-deprecation --zero-fill-buffers"', 'node-arguments.js'], err => t.ifError(err)); | ||
}); | ||
|
||
test('reads node arguments from config', t => { | ||
t.plan(1); | ||
execCli(['test.js'], { | ||
dirname: 'fixture/node-arguments' | ||
}, err => t.ifError(err)); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const {test} = require('tap'); | ||
const normalizeNodeArguments = require('../lib/node-arguments'); | ||
|
||
test('normalizes multiple node arguments from cli', t => { | ||
t.deepEqual(normalizeNodeArguments([], '--a --b --c'), {a: true, b: true, c: true}); | ||
t.end(); | ||
}); | ||
|
||
test('normalizes multiple node arguments from config', t => { | ||
t.deepEqual(normalizeNodeArguments(['--arg1', '--b'], ''), {arg1: true, b: true}); | ||
t.end(); | ||
}); | ||
|
||
test('normalizes node arguments from config and cli', t => { | ||
t.deepEqual( | ||
normalizeNodeArguments(['--arg1', '--b=2'], '--arg2 --b=12'), | ||
{arg1: true, arg2: true, b: 12} | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('normalizes single node arguments from cli', t => { | ||
t.deepEqual(normalizeNodeArguments([], '--test-flag'), {'test-flag': true}); | ||
t.end(); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.