diff --git a/lib/fork.js b/lib/fork.js index bf918d391..228fd7bb0 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,15 @@ module.exports = (file, opts, execArgv) => { } : false }, opts); - const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color']; + const args = [JSON.stringify(opts)]; + + 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, 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..dd2456d62 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 = 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/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 9062c7e40..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) { @@ -151,3 +150,12 @@ test('color support is initialized correctly', t => { 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(); + }); +});