|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// iron-node does not work with forked processes |
| 4 | +// This cli command will run a single file in the current process. |
| 5 | +// Intended to be used with iron-node for profiling purposes. |
| 6 | + |
| 7 | +var path = require('path'); |
| 8 | +var meow = require('meow'); |
| 9 | +var Promise = require('bluebird'); |
| 10 | +var pkgConf = require('pkg-conf'); |
| 11 | +var arrify = require('arrify'); |
| 12 | +var findCacheDir = require('find-cache-dir'); |
| 13 | +var uniqueTempDir = require('unique-temp-dir'); |
| 14 | +var EventEmitter = require('events').EventEmitter; |
| 15 | +var CachingPrecompiler = require('./lib/caching-precompiler'); |
| 16 | +var globals = require('./lib/globals'); |
| 17 | + |
| 18 | +// copy original console and console.log so tests can't manipulate them. |
| 19 | +var originalConsole = console; |
| 20 | +var consoleLog = originalConsole.log; |
| 21 | + |
| 22 | +// Our usage of globals to store setTimeout/clearTimeout upsets iron-node for some reason. |
| 23 | +globals.setTimeout = function (fn, delay) { |
| 24 | + return setTimeout(fn, delay); |
| 25 | +}; |
| 26 | + |
| 27 | +globals.clearTimeout = function (timeout) { |
| 28 | + return clearTimeout(timeout); |
| 29 | +}; |
| 30 | + |
| 31 | +Promise.longStackTraces(); |
| 32 | +var conf = pkgConf.sync('ava'); |
| 33 | + |
| 34 | +// Define a minimal set of options from the main CLI. |
| 35 | +var cli = meow([ |
| 36 | + 'usage: iron-node node_modules/ava/profile.js [options] TEST_FILE', |
| 37 | + '', |
| 38 | + 'Options', |
| 39 | + ' --fail-fast Stop after first test failure', |
| 40 | + ' --serial, -s Run tests serially', |
| 41 | + ' --require, -r Module to preload (Can be repeated)', |
| 42 | + '' |
| 43 | +], { |
| 44 | + string: [ |
| 45 | + '_', |
| 46 | + 'require' |
| 47 | + ], |
| 48 | + boolean: [ |
| 49 | + 'fail-fast', |
| 50 | + 'verbose', |
| 51 | + 'serial', |
| 52 | + 'tap' |
| 53 | + ], |
| 54 | + default: conf, |
| 55 | + alias: { |
| 56 | + r: 'require', |
| 57 | + s: 'serial' |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +if (cli.input.length !== 1) { |
| 62 | + throw new Error('no file'); |
| 63 | +} |
| 64 | + |
| 65 | +var file = path.resolve(cli.input[0]); |
| 66 | +var cacheDir = findCacheDir({name: 'ava', files: [file]}) || uniqueTempDir(); |
| 67 | +var opts = { |
| 68 | + file: file, |
| 69 | + failFast: cli.flags.failFast, |
| 70 | + serial: cli.flags.serial, |
| 71 | + require: arrify(cli.flags.require), |
| 72 | + tty: false, |
| 73 | + cacheDir: cacheDir, |
| 74 | + precompiled: new CachingPrecompiler(cacheDir).generateHashForFile(file) |
| 75 | +}; |
| 76 | + |
| 77 | +var events = new EventEmitter(); |
| 78 | + |
| 79 | +// Mock the behavior of a parent process. |
| 80 | +process.send = function (data) { |
| 81 | + if (data && data.ava) { |
| 82 | + var name = data.name.replace(/^ava-/, ''); |
| 83 | + if (events.listenerCount(name)) { |
| 84 | + events.emit(name, data.data); |
| 85 | + } else { |
| 86 | + consoleLog.call(originalConsole, 'UNHANDLED AVA EVENT: ', name, data.data); |
| 87 | + } |
| 88 | + return; |
| 89 | + } |
| 90 | + consoleLog.call(originalConsole, 'NON AVA EVENT: ', data); |
| 91 | +}; |
| 92 | + |
| 93 | +events.on('test', function (data) { |
| 94 | + consoleLog.call(originalConsole, 'TEST:', data.title, data.error); |
| 95 | +}); |
| 96 | + |
| 97 | +events.on('results', function (data) { |
| 98 | + consoleLog.call(originalConsole, 'RESULTS: ', data.stats); |
| 99 | +}); |
| 100 | + |
| 101 | +events.on('stats', function () { |
| 102 | + setImmediate(function () { |
| 103 | + process.emit('ava-run'); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +// test-worker will read process.argv[2] for options |
| 108 | +process.argv[2] = JSON.stringify(opts); |
| 109 | +process.argv.length = 3; |
| 110 | + |
| 111 | +require('./lib/test-worker'); |
0 commit comments