-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
common.js
92 lines (80 loc) · 3.38 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
var path = require('path');
var spawn = require('child_process').spawn;
var concat = require('concat-stream');
var yaml = require('js-yaml');
module.exports.getDiag = function (body) {
var yamlStart = body.indexOf(' ---');
var yamlEnd = body.indexOf(' ...\n');
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
return line.slice(2);
}).join('\n');
// The stack trace and at variable will vary depending on where the code
// is run, so just strip it out.
var withStack = yaml.safeLoad(diag);
delete withStack.stack;
delete withStack.at;
return withStack;
};
// There are three challenges associated with checking the stack traces included
// in errors:
// 1) The base checkout directory of tape might change. Because stack traces
// include absolute paths, the stack traces will change depending on the
// checkout path. We handle this by replacing the base test directory with a
// placeholder $TEST variable and the package root with a placeholder
// $TAPE variable.
// 2) Line positions within the file might change. We handle this by replacing
// line and column markers with placeholder $LINE and $COL "variables"
// a) node 0.8 does not provide nested eval line numbers, so we remove them
// 3) Stacks themselves change frequently with refactoring. We've even run into
// issues with node library refactorings "breaking" stack traces. Most of
// these changes are irrelevant to the tests themselves. To counter this, we
// strip out all stack frames that aren't directly under our test directory,
// and replace them with placeholders.
var stripChangingData = function (line) {
var withoutTestDir = line.replace(__dirname, '$TEST');
var withoutPackageDir = withoutTestDir.replace(path.dirname(__dirname), '$TAPE');
var withoutPathSep = withoutPackageDir.replace(new RegExp('\\' + path.sep, 'g'), '/');
var withoutLineNumbers = withoutPathSep.replace(/:\d+:\d+/g, ':$LINE:$COL');
var withoutNestedLineNumbers = withoutLineNumbers.replace(/, <anonymous>:\$LINE:\$COL\)$/, ')');
return withoutNestedLineNumbers;
};
module.exports.stripFullStack = function (output) {
var stripped = ' [... stack stripped ...]';
var withDuplicates = output.split('\n').map(stripChangingData).map(function (line) {
var m = line.match(/[ ]{8}at .*\((.*)\)/);
if (m && m[1].slice(0, 5) !== '$TEST') {
return stripped;
}
return line;
});
var deduped = withDuplicates.filter(function (line, ix) {
var hasPrior = line === stripped && withDuplicates[ix - 1] === stripped;
return !hasPrior;
});
return deduped.join('\n').replace(
// Handle stack trace variation in Node v0.8
/at(:?) Test\.(?:module\.exports|tap\.test\.err\.code)/g,
'at$1 Test.<anonymous>'
);
};
module.exports.runProgram = function (folderName, fileName, cb) {
var result = {
stdout: null,
stderr: null,
exitCode: 0
};
var ps = spawn(process.execPath, [
path.join(__dirname, folderName, fileName)
]);
ps.stdout.pipe(concat(function (stdoutRows) {
result.stdout = stdoutRows;
}));
ps.stderr.pipe(concat(function (stderrRows) {
result.stderr = stderrRows;
}));
ps.on('exit', function (code) {
result.exitCode = code;
cb(result);
});
};