1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var path = require('path');
|
5 | var passthrough = require('stream').PassThrough;
|
6 | var tapMerge = require('tap-merge');
|
7 | var merge = require('merge2');
|
8 | var multistream = require('multistream');
|
9 | var parallel = require('run-parallel');
|
10 |
|
11 | var runNode = require('./lib/run-node');
|
12 | var runElectron = require('./lib/run-electron');
|
13 | var reportCoverage = require('./lib/report-coverage');
|
14 |
|
15 | function run(opts, cb) {
|
16 | if (!opts.node && !opts.browser) {
|
17 | throw Error('No browser or node test entry specified');
|
18 | }
|
19 |
|
20 | var tests = [];
|
21 | var coverageObjects = [];
|
22 | var outputs = [];
|
23 |
|
24 | function finish(code) {
|
25 | if (opts.report) {
|
26 | reportCoverage(coverageObjects, opts.report, opts.reportDir);
|
27 | }
|
28 | cb(code);
|
29 | }
|
30 |
|
31 | function runTest(runner, entry) {
|
32 | tests.push(function(done) {
|
33 | var out = passthrough();
|
34 | var err = passthrough();
|
35 | var entryPath = path.resolve(process.cwd(), entry);
|
36 | var sub = runner(entryPath, function(coverage, exitCode) {
|
37 | if (coverage) {
|
38 | coverageObjects.push(coverage);
|
39 | }
|
40 | done(null, exitCode);
|
41 | });
|
42 | sub.stdout.pipe(out);
|
43 | sub.stderr.pipe(err);
|
44 | outputs.push(merge([out, err]));
|
45 | });
|
46 | }
|
47 |
|
48 | if (opts.node) {
|
49 | runTest(runNode, opts.node);
|
50 | }
|
51 |
|
52 | if (opts.browser) {
|
53 | runTest(runElectron, opts.browser);
|
54 | }
|
55 |
|
56 | parallel(tests, function(err, results) {
|
57 | var finalCode = results.reduce(function(acc, code) {
|
58 | return acc || code;
|
59 | }, 0);
|
60 | finish(finalCode);
|
61 | });
|
62 |
|
63 | var allOutput = multistream(outputs);
|
64 | var merged = tapMerge();
|
65 | allOutput.pipe(merged);
|
66 | return merged;
|
67 | }
|
68 |
|
69 | module.exports = run;
|