1 | var check = require('check-types');
|
2 | var verify = check.verify;
|
3 | var spawn = require('child_process').spawn;
|
4 | var q = require('q');
|
5 | var npmPath = require('./npm-test').npmPath;
|
6 |
|
7 |
|
8 | function test(options, testCommand) {
|
9 | options = options || {};
|
10 | var log = options.tldr ? _.noop : console.log.bind(console);
|
11 |
|
12 | verify.unemptyString(testCommand, 'missing test command string');
|
13 | log(' ', testCommand);
|
14 |
|
15 | var testParts = testCommand.split(' ');
|
16 | console.assert(testParts.length > 0, 'missing any test words in ' + testCommand);
|
17 | var testExecutable = testParts.shift();
|
18 | verify.unemptyString(testExecutable, 'missing test executable for command ' + testCommand);
|
19 | if (testExecutable === 'npm') {
|
20 | testExecutable = npmPath;
|
21 | }
|
22 | var testProcess = spawn(testExecutable, testParts);
|
23 | var testOutput = '';
|
24 | var testErrors = '';
|
25 |
|
26 | testProcess.stdout.setEncoding('utf-8');
|
27 | testProcess.stderr.setEncoding('utf-8');
|
28 |
|
29 | testProcess.stdout.on('data', function (data) {
|
30 | testOutput += data;
|
31 | });
|
32 |
|
33 | testProcess.stderr.on('data', function (data) {
|
34 | testErrors += data;
|
35 | });
|
36 |
|
37 | var deferred = q.defer();
|
38 | testProcess.on('error', function (err) {
|
39 | console.error('test command: "' + testCommand + '"');
|
40 | console.error(err);
|
41 | testErrors += err.toString();
|
42 | deferred.reject({
|
43 | code: err.code,
|
44 | errors: testErrors
|
45 | });
|
46 | });
|
47 |
|
48 | testProcess.on('exit', function (code) {
|
49 | if (code) {
|
50 | console.error('testProcess test returned', code);
|
51 | console.error('test errors:\n' + testErrors);
|
52 | console.error(testOutput);
|
53 | deferred.reject({
|
54 | code: code,
|
55 | errors: testErrors
|
56 | });
|
57 | }
|
58 | deferred.resolve();
|
59 | });
|
60 | return deferred.promise;
|
61 | }
|
62 |
|
63 | module.exports = test;
|