UNPKG

2.72 kBJavaScriptView Raw
1'use strict';
2
3/*global __dirname, process, require, module */
4var shell = require('shelljs'),
5 shellescape = require('shell-escape'),
6 path = require('path'),
7 whichLocal = require('npm-which')(path.resolve(__dirname, '..')),
8 whichCwd = require('npm-which')(process.cwd()),
9 directories = {};
10
11shell.config.fatal = false;
12
13/**
14 * Get the file location of this node_module binary
15 * @method getBin
16 * @param {string} filename Bin you are looking for
17 * @return {string} Full path to the file
18 */
19function getBin(filename) {
20 try {
21 return escape(whichLocal.sync(filename));
22 } catch (unused) {
23 return escape(whichCwd.sync(filename));
24 }
25}
26
27/**
28 * Escapes directory path, single argument version for 'shell-escape'.
29 * @method escape
30 * @param {string} directory Directory you wish to escape
31 * @return {string} Escaped directory
32 */
33function escape(directory){
34 return shellescape([directory]);
35}
36
37/**
38 * Executes istanbul and mocha and sends all the output to the right location
39 * @param {Object} args Mocha arguments
40 */
41module.exports = function (args) {
42 directories.artifacts = shell.env.ARTIFACTS_DIR || path.join(process.cwd(), 'artifacts');
43 directories.coverage = shell.env.COVERAGE_DIR || path.join(directories.artifacts, 'coverage');
44 directories.tests = shell.env.TEST_DIR || path.join(directories.artifacts, 'test');
45
46 // Make sure directories exist
47 Object.keys(directories).forEach(function (name) {
48 shell.mkdir('-p', directories[name]);
49 });
50
51 // Set Xunit file
52 shell.env.XUNIT_FILE = path.join(directories.tests, 'xunit.xml');
53
54 if (args.indexOf('--no-colors') === -1) {
55 args.unshift('--colors');
56 }
57
58 var command = [];
59
60 var noCoverageIndex = args.indexOf('--no-coverage');
61 if (noCoverageIndex === -1) {
62 // we want coverage
63 command.push(getBin('istanbul'));
64 command.push('cover');
65
66 // .. but do we want cobertura?
67 var coberturaIndex = args.indexOf('--cobertura');
68 if (coberturaIndex !== -1) {
69 // remove the --cobertura option from args array
70 args.splice(coberturaIndex, 1);
71 command.push('--report cobertura');
72 }
73
74 command.push('--dir ' + escape(directories.coverage) + ' --');
75 command.push(getBin('_mocha'));
76 } else {
77 // remove the --no-coverage option from args array
78 args.splice(noCoverageIndex, 1);
79 command.push(getBin('mocha'));
80 }
81
82 command.push('--reporter ' + escape(require.resolve('spec-xunit-file')));
83 command.push(shellescape(args));
84
85 // Trigger istanbul and mocha
86 shell.exit(shell.exec(command.join(' ')).code);
87};