Skip to content

Commit 2249a3b

Browse files
committed
Only pass color flag to worker and remove internal options
1 parent 8644060 commit 2249a3b

10 files changed

+80
-2
lines changed

lib/fork.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const fs = require('fs');
55
const Promise = require('bluebird');
66
const debug = require('debug')('ava');
7+
const hasFlag = require('has-flag');
78
const AvaError = require('./ava-error');
89

910
if (fs.realpathSync(__filename) !== __filename) {
@@ -36,7 +37,11 @@ module.exports = (file, opts, execArgv) => {
3637
} : false
3738
}, opts);
3839

39-
const args = [JSON.stringify(opts)].concat(process.argv.slice(2));
40+
const args = [JSON.stringify(opts)];
41+
42+
if (hasFlag('--color') || hasFlag('--no-color')) {
43+
args.push(`--color=${opts.color}`);
44+
}
4045

4146
const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
4247
cwd: opts.projectDir,

lib/process-adapter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ exports.ipcChannel = process.channel || process._channel;
2929
const opts = JSON.parse(process.argv[2]);
3030
exports.opts = opts;
3131

32+
// Remove internal `ava` options from arguments
33+
process.argv.splice(2, 1);
34+
3235
// Fake TTY support
3336
if (opts.tty) {
3437
process.stdout.isTTY = true;

lib/test-worker.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
2+
const chalk = require('chalk');
23

34
// Check if the test is being run without AVA cli
45
{
56
const path = require('path');
6-
const chalk = require('chalk');
77

88
const isForked = typeof process.send === 'function';
99
if (!isForked) {
@@ -27,6 +27,9 @@ globals.options = opts;
2727

2828
const serializeError = require('./serialize-error');
2929

30+
// Initialize color support
31+
chalk.enabled = typeof opts.color === 'boolean' && opts.color;
32+
3033
(opts.require || []).forEach(require);
3134

3235
adapter.installSourceMapSupport();

test/cli.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,17 @@ test('--color enables formatting colors', t => {
802802
t.end();
803803
});
804804
});
805+
806+
test('--no-color forwards flag to worker', t => {
807+
execCli(['--no-color', 'no-color-flag.js'], {dirname: 'fixture'}, err => {
808+
t.ok(err);
809+
t.end();
810+
});
811+
});
812+
813+
test('--color forwards flag to worker', t => {
814+
execCli(['--color', 'color-flag.js'], {dirname: 'fixture'}, err => {
815+
t.ok(err);
816+
t.end();
817+
});
818+
});

test/fixture/arguments.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../';
2+
3+
test(t => {
4+
t.is(process.argv[2], undefined);
5+
});

test/fixture/chalk-disabled.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import chalk from 'chalk';
2+
import test from '../../';
3+
4+
test('should not support colors', t => {
5+
t.false(chalk.enabled);
6+
});

test/fixture/chalk-enabled.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import chalk from 'chalk';
2+
import test from '../../';
3+
4+
test('should support colors', t => {
5+
t.true(chalk.enabled);
6+
});

test/fixture/color-flag.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../';
2+
3+
test(t => {
4+
t.is(process.argv[2], '--color');
5+
});

test/fixture/no-color-flag.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../';
2+
3+
test(t => {
4+
t.is(process.argv[2], '--no-color');
5+
});

test/fork.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,29 @@ test('babelrc is ignored', t => {
134134
t.end();
135135
});
136136
});
137+
138+
test('color support is initialized correctly', t => {
139+
t.plan(1);
140+
141+
return Promise.all([
142+
fork(fixture('chalk-enabled.js'), {color: true}).run({}),
143+
fork(fixture('chalk-disabled.js'), {color: false}).run({}),
144+
fork(fixture('chalk-disabled.js'), {}).run({})
145+
]).then(info => {
146+
info.forEach(info => {
147+
if (info.stats.failCount > 0) {
148+
throw new Error(`${info.file} failed`);
149+
}
150+
});
151+
t.is(info.length, 3);
152+
});
153+
});
154+
155+
test('doesn\'t pass internal options to worker', t => {
156+
return fork(fixture('arguments.js'))
157+
.run({})
158+
.then(info => {
159+
t.is(info.stats.passCount, 1);
160+
t.end();
161+
});
162+
});

0 commit comments

Comments
 (0)