Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / build-scripts / test-runner.js
blobbfb046854eaa8893fbbdc417991e4404f93bc862
1 'use strict';
3 var childProcessPromise = require('./child-process-promise');
4 var path = require('path');
5 var es = require('event-stream');
6 var gutil = require('gulp-util');
8 function resolveBinary(moduleName) {
9 var moduleLocation = require.resolve(moduleName);
10 var moduleDirectory = path.dirname(moduleLocation);
11 return path.resolve(moduleDirectory, '../.bin/' + moduleName);
14 function serializeCompilers(compilers) {
15 if (!compilers) return;
16 return Object.keys(compilers)
17 .map(function(extension) {
18 var m = compilers[extension];
19 return extension + ':' + m;
21 .join(',');
24 function testRunner(options, files) {
25 var executable;
26 var args;
27 var mochaBinary = resolveBinary('mocha');
29 function pushBooleanArg(argName, bool) {
30 if (bool) {
31 args.push(argName);
35 function pushArg(argName, value) {
36 if (value) {
37 args.push(argName);
38 args.push(value);
42 function addMultiArgs(argName, extensions) {
43 if (!extensions) return;
44 extensions.forEach(function(ext) {
45 args.push(argName);
46 args.push(ext);
47 });
50 function addOptional(options, argName, keyName) {
51 if (options.hasOwnProperty(keyName)) {
52 args.push(argName);
53 args.push(options[keyName]);
57 if (options.nyc) {
58 var nyc = options.nyc;
59 executable = resolveBinary('nyc');
60 args = [];
62 pushBooleanArg('--cache', nyc.cache);
63 pushArg('--report-dir', nyc.reportDir);
64 pushArg('--reporter', nyc.reporter);
65 addMultiArgs('--extension', nyc.extensions);
66 addMultiArgs('--require', nyc.requires);
67 addOptional(nyc, '--instrument', 'instrument');
68 addOptional(nyc, '--source-map', 'sourceMap');
69 args.push(mochaBinary);
70 } else {
71 executable = mochaBinary;
72 args = [];
75 pushArg('-R', options.reporter);
76 pushBooleanArg('--recursive', options.recursive);
77 pushArg('--grep', options.grep);
78 pushBooleanArg('--invert', options.invert);
79 pushBooleanArg('--bail', options.invert);
80 pushArg('--timeout', options.timeout);
81 pushArg('--compilers', serializeCompilers(options.compilers));
83 args = args.concat(files);
85 gutil.log('Spawning ', executable, args.join(' '));
86 return childProcessPromise.spawn(executable, args, options.env);
89 function pipe(options) {
90 var files = [];
91 return es.through(
92 function(data) {
93 files.push(data.path);
94 // console.log(data);
95 this.emit('data', data);
97 function() {
98 testRunner(options, files)
99 .bind(this)
100 .then(function() {
101 this.emit('end');
103 .catch(function(err) {
104 this.emit('error', err);
110 module.exports = pipe;