From 7bfc5aabd472702bacbfbc06125ad0e71f99bb46 Mon Sep 17 00:00:00 2001 From: Kevin Martensson Date: Sun, 4 Jun 2017 19:21:15 +0200 Subject: [PATCH 1/4] Run workers with user supplied command line arguments Fixes #1393. --- lib/fork.js | 2 +- test/fixture/chalk-disabled.js | 6 ------ test/fixture/chalk-enabled.js | 6 ------ test/fork.js | 17 ----------------- 4 files changed, 1 insertion(+), 30 deletions(-) delete mode 100644 test/fixture/chalk-disabled.js delete mode 100644 test/fixture/chalk-enabled.js diff --git a/lib/fork.js b/lib/fork.js index bf918d391..a0c98ae30 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -36,7 +36,7 @@ module.exports = (file, opts, execArgv) => { } : false }, opts); - const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color']; + const args = [JSON.stringify(opts)].concat(process.argv.slice(2)); const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, { cwd: opts.projectDir, diff --git a/test/fixture/chalk-disabled.js b/test/fixture/chalk-disabled.js deleted file mode 100644 index 21caccf3e..000000000 --- a/test/fixture/chalk-disabled.js +++ /dev/null @@ -1,6 +0,0 @@ -import chalk from 'chalk'; -import test from '../../'; - -test('should not support colors', t => { - t.false(chalk.enabled); -}); diff --git a/test/fixture/chalk-enabled.js b/test/fixture/chalk-enabled.js deleted file mode 100644 index a7e0ff640..000000000 --- a/test/fixture/chalk-enabled.js +++ /dev/null @@ -1,6 +0,0 @@ -import chalk from 'chalk'; -import test from '../../'; - -test('should support colors', t => { - t.true(chalk.enabled); -}); diff --git a/test/fork.js b/test/fork.js index 9062c7e40..214e0d3b8 100644 --- a/test/fork.js +++ b/test/fork.js @@ -134,20 +134,3 @@ test('babelrc is ignored', t => { t.end(); }); }); - -test('color support is initialized correctly', t => { - t.plan(1); - - return Promise.all([ - fork(fixture('chalk-enabled.js'), {color: true}).run({}), - fork(fixture('chalk-disabled.js'), {color: false}).run({}), - fork(fixture('chalk-disabled.js'), {}).run({}) - ]).then(info => { - info.forEach(info => { - if (info.stats.failCount > 0) { - throw new Error(`${info.file} failed`); - } - }); - t.is(info.length, 3); - }); -}); From 6be4470d1ec202015703b58420390efd098fdabb Mon Sep 17 00:00:00 2001 From: Kevin Martensson Date: Wed, 13 Sep 2017 21:18:06 +0200 Subject: [PATCH 2/4] Only pass color flag to worker and remove internal options --- lib/fork.js | 7 ++++++- lib/process-adapter.js | 3 +++ lib/test-worker.js | 5 ++++- test/cli.js | 14 ++++++++++++++ test/fixture/arguments.js | 5 +++++ test/fixture/chalk-disabled.js | 6 ++++++ test/fixture/chalk-enabled.js | 6 ++++++ test/fixture/color-flag.js | 5 +++++ test/fixture/no-color-flag.js | 5 +++++ test/fork.js | 26 ++++++++++++++++++++++++++ 10 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 test/fixture/arguments.js create mode 100644 test/fixture/chalk-disabled.js create mode 100644 test/fixture/chalk-enabled.js create mode 100644 test/fixture/color-flag.js create mode 100644 test/fixture/no-color-flag.js diff --git a/lib/fork.js b/lib/fork.js index a0c98ae30..6f77ea29b 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -4,6 +4,7 @@ const path = require('path'); const fs = require('fs'); const Promise = require('bluebird'); const debug = require('debug')('ava'); +const hasFlag = require('has-flag'); const AvaError = require('./ava-error'); if (fs.realpathSync(__filename) !== __filename) { @@ -36,7 +37,11 @@ module.exports = (file, opts, execArgv) => { } : false }, opts); - const args = [JSON.stringify(opts)].concat(process.argv.slice(2)); + const args = [JSON.stringify(opts)]; + + if (hasFlag('--color') || hasFlag('--no-color')) { + args.push(`--color=${opts.color}`); + } const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, { cwd: opts.projectDir, diff --git a/lib/process-adapter.js b/lib/process-adapter.js index 5f9c0d79d..07aad3f21 100644 --- a/lib/process-adapter.js +++ b/lib/process-adapter.js @@ -29,6 +29,9 @@ exports.ipcChannel = process.channel || process._channel; const opts = JSON.parse(process.argv[2]); exports.opts = opts; +// Remove internal `ava` options from arguments +process.argv.splice(2, 1); + // Fake TTY support if (opts.tty) { process.stdout.isTTY = true; diff --git a/lib/test-worker.js b/lib/test-worker.js index d70403c89..bf0648c28 100644 --- a/lib/test-worker.js +++ b/lib/test-worker.js @@ -1,9 +1,9 @@ 'use strict'; +const chalk = require('chalk'); // Check if the test is being run without AVA cli { const path = require('path'); - const chalk = require('chalk'); const isForked = typeof process.send === 'function'; if (!isForked) { @@ -27,6 +27,9 @@ globals.options = opts; const serializeError = require('./serialize-error'); +// Initialize color support +chalk.enabled = typeof opts.color === 'boolean' && opts.color; + (opts.require || []).forEach(require); adapter.installSourceMapSupport(); diff --git a/test/cli.js b/test/cli.js index aa69d04aa..0adc8347b 100644 --- a/test/cli.js +++ b/test/cli.js @@ -802,3 +802,17 @@ test('--color enables formatting colors', t => { t.end(); }); }); + +test('--no-color forwards flag to worker', t => { + execCli(['--no-color', 'no-color-flag.js'], {dirname: 'fixture'}, err => { + t.ok(err); + t.end(); + }); +}); + +test('--color forwards flag to worker', t => { + execCli(['--color', 'color-flag.js'], {dirname: 'fixture'}, err => { + t.ok(err); + t.end(); + }); +}); diff --git a/test/fixture/arguments.js b/test/fixture/arguments.js new file mode 100644 index 000000000..d7768de6d --- /dev/null +++ b/test/fixture/arguments.js @@ -0,0 +1,5 @@ +import test from '../../'; + +test(t => { + t.is(process.argv[2], undefined); +}); diff --git a/test/fixture/chalk-disabled.js b/test/fixture/chalk-disabled.js new file mode 100644 index 000000000..21caccf3e --- /dev/null +++ b/test/fixture/chalk-disabled.js @@ -0,0 +1,6 @@ +import chalk from 'chalk'; +import test from '../../'; + +test('should not support colors', t => { + t.false(chalk.enabled); +}); diff --git a/test/fixture/chalk-enabled.js b/test/fixture/chalk-enabled.js new file mode 100644 index 000000000..a7e0ff640 --- /dev/null +++ b/test/fixture/chalk-enabled.js @@ -0,0 +1,6 @@ +import chalk from 'chalk'; +import test from '../../'; + +test('should support colors', t => { + t.true(chalk.enabled); +}); diff --git a/test/fixture/color-flag.js b/test/fixture/color-flag.js new file mode 100644 index 000000000..ec17e8692 --- /dev/null +++ b/test/fixture/color-flag.js @@ -0,0 +1,5 @@ +import test from '../../'; + +test(t => { + t.is(process.argv[2], '--color'); +}); diff --git a/test/fixture/no-color-flag.js b/test/fixture/no-color-flag.js new file mode 100644 index 000000000..86a0bb4ce --- /dev/null +++ b/test/fixture/no-color-flag.js @@ -0,0 +1,5 @@ +import test from '../../'; + +test(t => { + t.is(process.argv[2], '--no-color'); +}); diff --git a/test/fork.js b/test/fork.js index 214e0d3b8..4eec3f4f7 100644 --- a/test/fork.js +++ b/test/fork.js @@ -134,3 +134,29 @@ test('babelrc is ignored', t => { t.end(); }); }); + +test('color support is initialized correctly', t => { + t.plan(1); + + return Promise.all([ + fork(fixture('chalk-enabled.js'), {color: true}).run({}), + fork(fixture('chalk-disabled.js'), {color: false}).run({}), + fork(fixture('chalk-disabled.js'), {}).run({}) + ]).then(info => { + info.forEach(info => { + if (info.stats.failCount > 0) { + throw new Error(`${info.file} failed`); + } + }); + t.is(info.length, 3); + }); +}); + +test('doesn\'t pass internal options to worker', t => { + return fork(fixture('arguments.js')) + .run({}) + .then(info => { + t.is(info.stats.passCount, 1); + t.end(); + }); +}); From 889d87f2ea154d7b78aab1bf8a5535ecacb865a6 Mon Sep 17 00:00:00 2001 From: Kevin Martensson Date: Mon, 18 Sep 2017 21:07:11 +0200 Subject: [PATCH 3/4] Pass `--no-color` as is --- lib/fork.js | 6 +++++- lib/test-worker.js | 2 +- test/fork.js | 3 +-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/fork.js b/lib/fork.js index 6f77ea29b..d76a628e4 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -39,10 +39,14 @@ module.exports = (file, opts, execArgv) => { const args = [JSON.stringify(opts)]; - if (hasFlag('--color') || hasFlag('--no-color')) { + if (hasFlag('--color')) { args.push(`--color=${opts.color}`); } + if (hasFlag('--no-color')) { + args.push(`--no-color`); + } + const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, { cwd: opts.projectDir, silent: true, diff --git a/lib/test-worker.js b/lib/test-worker.js index bf0648c28..dd2456d62 100644 --- a/lib/test-worker.js +++ b/lib/test-worker.js @@ -28,7 +28,7 @@ globals.options = opts; const serializeError = require('./serialize-error'); // Initialize color support -chalk.enabled = typeof opts.color === 'boolean' && opts.color; +chalk.enabled = opts.color; (opts.require || []).forEach(require); diff --git a/test/fork.js b/test/fork.js index 4eec3f4f7..4b8700ee8 100644 --- a/test/fork.js +++ b/test/fork.js @@ -140,8 +140,7 @@ test('color support is initialized correctly', t => { return Promise.all([ fork(fixture('chalk-enabled.js'), {color: true}).run({}), - fork(fixture('chalk-disabled.js'), {color: false}).run({}), - fork(fixture('chalk-disabled.js'), {}).run({}) + fork(fixture('chalk-disabled.js'), {color: false}).run({}) ]).then(info => { info.forEach(info => { if (info.stats.failCount > 0) { From 9296ddf39f5c54981047c5b4e71c868497c44cdf Mon Sep 17 00:00:00 2001 From: Kevin Martensson Date: Mon, 25 Sep 2017 00:38:03 +0200 Subject: [PATCH 4/4] Don't unnecessarily use template literals --- lib/fork.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fork.js b/lib/fork.js index d76a628e4..228fd7bb0 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -44,7 +44,7 @@ module.exports = (file, opts, execArgv) => { } if (hasFlag('--no-color')) { - args.push(`--no-color`); + args.push('--no-color'); } const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {