1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var spawn = require('child_process').spawn;
|
5 | var concat = require('concat-stream');
|
6 | var yaml = require('js-yaml');
|
7 |
|
8 | module.exports.getDiag = function (body) {
|
9 | var yamlStart = body.indexOf(' ---');
|
10 | var yamlEnd = body.indexOf(' ...\n');
|
11 | var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
|
12 | return line.slice(2);
|
13 | }).join('\n');
|
14 |
|
15 |
|
16 |
|
17 | var withStack = yaml.safeLoad(diag);
|
18 | delete withStack.stack;
|
19 | delete withStack.at;
|
20 | return withStack;
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | var stripChangingData = function (line) {
|
40 | var withoutTestDir = line.replace(__dirname, '$TEST');
|
41 | var withoutPackageDir = withoutTestDir.replace(path.dirname(__dirname), '$TAPE');
|
42 | var withoutPathSep = withoutPackageDir.replace(new RegExp('\\' + path.sep, 'g'), '/');
|
43 | var withoutLineNumbers = withoutPathSep.replace(/:\d+:\d+/g, ':$LINE:$COL');
|
44 | var withoutNestedLineNumbers = withoutLineNumbers.replace(/, <anonymous>:\$LINE:\$COL\)$/, ')');
|
45 | return withoutNestedLineNumbers;
|
46 | };
|
47 |
|
48 | module.exports.stripFullStack = function (output) {
|
49 | var stripped = ' [... stack stripped ...]';
|
50 | var withDuplicates = output.split(/\r?\n/g).map(stripChangingData).map(function (line) {
|
51 | var m = line.match(/[ ]{8}at .*\((.*)\)/);
|
52 |
|
53 | if (m && m[1].slice(0, 5) !== '$TEST') {
|
54 | return stripped;
|
55 | }
|
56 | return line;
|
57 | });
|
58 |
|
59 | var withoutInternals = withDuplicates.filter(function (line) {
|
60 | return !line.match(/ \(node:[^)]+\)$/);
|
61 | });
|
62 |
|
63 | var deduped = withoutInternals.filter(function (line, ix) {
|
64 | var hasPrior = line === stripped && withDuplicates[ix - 1] === stripped;
|
65 | return !hasPrior;
|
66 | });
|
67 |
|
68 | return deduped.join('\n').replace(
|
69 |
|
70 | /at(:?) Test\.(?:module\.exports|tap\.test\.err\.code)/g,
|
71 | 'at$1 Test.<anonymous>'
|
72 | ).replace(
|
73 |
|
74 | /at(:?) (Test\.)?tap\.test\.test\.skip/g,
|
75 | 'at$1 $2<anonymous>'
|
76 | ).replace(
|
77 |
|
78 | /(\[\.\.\. stack stripped \.\.\.\]\r?\n *at) <anonymous> \(([^)]+)\)/g,
|
79 | '$1 $2'
|
80 | ).split(/\r?\n/g);
|
81 | };
|
82 |
|
83 | module.exports.runProgram = function (folderName, fileName, cb) {
|
84 | var result = {
|
85 | stdout: null,
|
86 | stderr: null,
|
87 | exitCode: 0
|
88 | };
|
89 | var ps = spawn(process.execPath, [
|
90 | path.join(__dirname, folderName, fileName)
|
91 | ]);
|
92 |
|
93 | ps.stdout.pipe(concat(function (stdoutRows) {
|
94 | result.stdout = stdoutRows;
|
95 | }));
|
96 | ps.stderr.pipe(concat(function (stderrRows) {
|
97 | result.stderr = stderrRows;
|
98 | }));
|
99 |
|
100 | ps.on('exit', function (code) {
|
101 | result.exitCode = code;
|
102 | cb(result);
|
103 | });
|
104 | };
|