mirror of https://github.com/lukechilds/ava.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.2 KiB
163 lines
4.2 KiB
9 years ago
|
'use strict';
|
||
9 years ago
|
var path = require('path');
|
||
9 years ago
|
var debug = require('debug')('ava');
|
||
9 years ago
|
var pkgDir = require('pkg-dir').sync;
|
||
9 years ago
|
var hasha = require('hasha');
|
||
|
var cacha = require('cacha');
|
||
9 years ago
|
var sourceMapSupport = require('source-map-support');
|
||
9 years ago
|
|
||
9 years ago
|
var opts = JSON.parse(process.argv[2]);
|
||
9 years ago
|
var testPath = opts.file;
|
||
|
|
||
|
var cache = cacha(path.join(pkgDir(path.dirname(testPath)), 'node_modules', '.cache', 'ava'));
|
||
9 years ago
|
|
||
9 years ago
|
if (debug.enabled) {
|
||
9 years ago
|
// Forward the `time-require` `--sorted` flag.
|
||
|
// Intended for internal optimization tests only.
|
||
|
if (opts._sorted) {
|
||
|
process.argv.push('--sorted');
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
require('time-require');
|
||
|
}
|
||
|
|
||
9 years ago
|
// bind globals first before anything has a chance to interfere
|
||
9 years ago
|
var globals = require('./globals');
|
||
|
|
||
9 years ago
|
(opts.require || []).forEach(require);
|
||
9 years ago
|
|
||
9 years ago
|
var sourceMapCache = Object.create(null);
|
||
|
|
||
9 years ago
|
sourceMapSupport.install({
|
||
9 years ago
|
handleUncaughtExceptions: false,
|
||
9 years ago
|
retrieveSourceMap: function (source) {
|
||
|
if (sourceMapCache[source]) {
|
||
|
return {
|
||
|
url: source,
|
||
|
map: sourceMapCache[source]
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
9 years ago
|
var requireFromString = require('require-from-string');
|
||
9 years ago
|
var loudRejection = require('loud-rejection/api')(process);
|
||
9 years ago
|
var serializeError = require('serialize-error');
|
||
9 years ago
|
var send = require('./send');
|
||
9 years ago
|
|
||
9 years ago
|
// if generators are not supported, use regenerator
|
||
9 years ago
|
var options = {
|
||
9 years ago
|
sourceMaps: true
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
// check if test files required ava and show error, when they didn't
|
||
|
exports.avaRequired = false;
|
||
9 years ago
|
|
||
9 years ago
|
// try to load an input source map for the test file, in case the file was
|
||
|
// already compiled once by the user
|
||
|
var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath);
|
||
|
if (inputSourceMap) {
|
||
|
// source-map-support returns the source map as a json-encoded string, but
|
||
|
// babel requires an actual object
|
||
|
options.inputSourceMap = JSON.parse(inputSourceMap.map);
|
||
|
}
|
||
|
|
||
9 years ago
|
// include test file
|
||
9 years ago
|
var cachePath = hasha(cacheKey(testPath));
|
||
|
var hashPath = cachePath + '_hash';
|
||
|
|
||
|
var prevHash = cache.getSync(hashPath, {encoding: 'utf8'});
|
||
|
var currHash = hasha.fromFileSync(testPath);
|
||
|
|
||
|
if (prevHash === currHash) {
|
||
|
var cached = JSON.parse(cache.getSync(cachePath));
|
||
|
|
||
|
sourceMapCache[testPath] = cached.map;
|
||
|
requireFromString(cached.code, testPath, {
|
||
|
appendPaths: module.paths
|
||
|
});
|
||
|
} else {
|
||
|
var createEspowerPlugin = require('babel-plugin-espower/create');
|
||
|
var babel = require('babel-core');
|
||
|
|
||
|
// initialize power-assert
|
||
|
var powerAssert = createEspowerPlugin(babel, {
|
||
|
patterns: require('./enhance-assert').PATTERNS
|
||
|
});
|
||
|
|
||
|
options.presets = [require('babel-preset-stage-2'), require('babel-preset-es2015')];
|
||
|
options.plugins = [powerAssert, require('babel-plugin-transform-runtime')];
|
||
|
|
||
|
var transpiled = babel.transformFileSync(testPath, options);
|
||
|
|
||
|
cache.setSync(hashPath, currHash);
|
||
|
cache.setSync(cachePath, JSON.stringify({
|
||
|
code: transpiled.code,
|
||
|
map: transpiled.map
|
||
|
}));
|
||
|
|
||
|
sourceMapCache[testPath] = transpiled.map;
|
||
|
requireFromString(transpiled.code, testPath, {
|
||
|
appendPaths: module.paths
|
||
|
});
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
process.on('uncaughtException', function (exception) {
|
||
|
send('uncaughtException', {exception: serializeError(exception)});
|
||
|
});
|
||
|
|
||
9 years ago
|
// if ava was not required, show an error
|
||
|
if (!exports.avaRequired) {
|
||
9 years ago
|
send('no-tests', {avaRequired: false});
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// parse and re-emit ava messages
|
||
9 years ago
|
process.on('message', function (message) {
|
||
9 years ago
|
if (!message.ava) {
|
||
|
return;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
process.emit(message.name, message.data);
|
||
9 years ago
|
});
|
||
9 years ago
|
|
||
9 years ago
|
process.on('ava-exit', function () {
|
||
|
// use a little delay when running on AppVeyor (because it's shit)
|
||
|
var delay = process.env.AVA_APPVEYOR ? 100 : 0;
|
||
|
|
||
9 years ago
|
globals.setTimeout(function () {
|
||
9 years ago
|
process.exit(0);
|
||
9 years ago
|
}, delay);
|
||
9 years ago
|
});
|
||
|
|
||
9 years ago
|
process.on('ava-teardown', function () {
|
||
|
var rejections = loudRejection.currentlyUnhandled();
|
||
|
|
||
|
if (rejections.length === 0) {
|
||
|
return exit();
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
rejections = rejections.map(function (rejection) {
|
||
9 years ago
|
return serializeError(rejection.reason);
|
||
9 years ago
|
});
|
||
|
|
||
|
send('unhandledRejections', {rejections: rejections});
|
||
9 years ago
|
globals.setTimeout(exit, 100);
|
||
9 years ago
|
});
|
||
9 years ago
|
|
||
|
function exit() {
|
||
|
send('teardown');
|
||
|
}
|
||
9 years ago
|
|
||
|
function cacheKey(path) {
|
||
|
var key = path;
|
||
|
|
||
|
key += require('../package.json').version;
|
||
|
key += require('babel-core/package.json').version;
|
||
|
key += require('babel-plugin-espower/package.json').version;
|
||
|
key += require('babel-plugin-transform-runtime/package.json').version;
|
||
|
key += require('babel-preset-stage-2/package.json').version;
|
||
|
key += require('babel-preset-es2015/package.json').version;
|
||
|
|
||
|
return hasha(key);
|
||
|
}
|