Skip to content
Permalink
Browse files

Reorganize worker code

  • Loading branch information...
novemberborn committed Feb 18, 2018
1 parent 7c3b90f commit 8938577ff10459f24838bad099ecae4da8b4be44
@@ -4,5 +4,5 @@
if (process.env.AVA_PATH && process.env.AVA_PATH !== __dirname) {
module.exports = require(process.env.AVA_PATH);
} else {
module.exports = require('./lib/main');
module.exports = require('./lib/worker/main');
}
@@ -6,7 +6,7 @@ const debug = require('debug')('ava');
// Ignore unimportant stack trace lines
let ignoreStackLines = [];

const avaInternals = /\/ava\/(?:lib\/)?[\w-]+\.js:\d+:\d+\)?$/;
const avaInternals = /\/ava\/(?:lib\/|lib\/worker\/)?[\w-]+\.js:\d+:\d+\)?$/;
const avaDependencies = /\/node_modules\/(?:append-transform|bluebird|empower-core|nyc|require-precompiled|(?:ava\/node_modules\/)?(?:babel-runtime|core-js))\//;
const stackFrameLine = /^.+( \(.+:\d+:\d+\)|:\d+:\d+)$/;

@@ -4,7 +4,7 @@ const chalk = require('chalk');
const stripAnsi = require('strip-ansi');
const cloneDeepWith = require('lodash.clonedeepwith');
const reactPlugin = require('@concordance/react');
const options = require('./worker-options').get();
const options = require('./worker/options').get();

// Wrap Concordance's React plugin. Change the name to avoid collisions if in
// the future users can register plugins themselves.
@@ -24,6 +24,8 @@ if (env.NODE_PATH) {
// the presence of this variable allows it to require this one instead
env.AVA_PATH = path.resolve(__dirname, '..');

const workerPath = require.resolve('./worker/subprocess');

module.exports = (file, opts, execArgv) => {
opts = Object.assign({
file,
@@ -36,7 +38,7 @@ module.exports = (file, opts, execArgv) => {

const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color'].concat(opts.workerArgv);

const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
const ps = childProcess.fork(workerPath, args, {
cwd: opts.projectDir,
silent: true,
env,

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,5 @@
'use strict';
require('./options').set(JSON.parse(process.argv[2]));

// Remove arguments received from fork.js and leave those specified by the user.
process.argv.splice(2, 2);
@@ -0,0 +1,32 @@
'use strict';
/* eslint-disable node/no-deprecated-api */

const seenDependencies = new Set();
function getAll() {
return Array.from(seenDependencies);
}
exports.getAll = getAll;

function track(filename) {
if (seenDependencies.has(filename)) {
return;
}

seenDependencies.add(filename);
}
exports.track = track;

function install(testPath) {
Object.keys(require.extensions).forEach(ext => {
const wrappedHandler = require.extensions[ext];

require.extensions[ext] = (module, filename) => {
if (filename !== testPath) {
track(filename);
}

wrappedHandler(module, filename);
};
});
}
exports.install = install;
@@ -0,0 +1,14 @@
'use strict';
const path = require('path');
const chalk = require('chalk'); // Use default Chalk instance.

// Check if the test is being run without AVA cli
const isForked = typeof process.send === 'function';
if (!isForked) {
const fp = path.relative('.', process.argv[1]);

console.log();
console.error(`Test files must be run with the AVA CLI:\n\n ${chalk.grey.dim('$')} ${chalk.cyan('ava ' + fp)}\n`);

process.exit(1); // eslint-disable-line unicorn/no-process-exit
}
@@ -0,0 +1,16 @@
'use strict';
const tty = require('tty');
const options = require('./options').get();

if (options.tty) {
Object.assign(process.stdout, {isTTY: true}, options.tty);

const isatty = tty.isatty;
tty.isatty = function (fd) {
if (fd === 1 || fd === process.stdout) {
return true;
}

return isatty(fd);
};
}
@@ -0,0 +1,34 @@
'use strict';

// `process.channel` was added in Node.js 7.1.0, but the channel was available
// through an undocumented API as `process._channel`.
const channel = process.channel || process._channel;

// Parse and re-emit AVA messages
process.on('message', message => {
if (!message.ava) {
return;
}

process.emit(message.name, message.data);
});

exports.send = (name, data) => {
process.send({
name: `ava-${name}`,
data,
ava: true
});
};

let allowUnref = true;
exports.unrefChannel = () => {
if (allowUnref) {
channel.unref();
}
};

exports.forceRefChannel = () => {
allowUnref = false;
channel.ref();
};
@@ -0,0 +1,3 @@
'use strict';
/* eslint-disable import/no-unassigned-import */
require('chalk'); // This processes the --color/--no-color argument passed by fork.js
@@ -1,7 +1,5 @@
'use strict';
const worker = require('./test-worker');

const runner = worker.getRunner();
const runner = require('./subprocess').getRunner();

const makeCjsExport = () => {
function test() {
File renamed without changes.
@@ -0,0 +1,40 @@
'use strict';
const fs = require('fs');
const path = require('path');
const sourceMapSupport = require('source-map-support');
const installPrecompiler = require('require-precompiled');
const options = require('./options').get();

const sourceMapCache = new Map();
const cacheDir = options.cacheDir;

function installSourceMapSupport() {
sourceMapSupport.install({
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap(source) {
if (sourceMapCache.has(source)) {
return {
url: source,
map: fs.readFileSync(sourceMapCache.get(source), 'utf8')
};
}
}
});
}

function install() {
installSourceMapSupport();

installPrecompiler(filename => {
const precompiled = options.precompiled[filename];

if (precompiled) {
sourceMapCache.set(filename, path.join(cacheDir, `${precompiled}.js.map`));
return fs.readFileSync(path.join(cacheDir, `${precompiled}.js`), 'utf8');
}

return null;
});
}
exports.install = install;
Oops, something went wrong.

0 comments on commit 8938577

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.