UNPKG

2.44 kBJavaScriptView Raw
1var yaml = require('js-yaml');
2
3module.exports.getDiag = function (body) {
4 var yamlStart = body.indexOf(' ---');
5 var yamlEnd = body.indexOf(' ...\n');
6 var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
7 return line.slice(2);
8 }).join('\n');
9
10 // The stack trace will vary depending on where the code is run, so just
11 // strip it out.
12 var withStack = yaml.safeLoad(diag);
13 delete withStack.stack;
14 return withStack;
15}
16
17// There are three challenges associated with checking the stack traces included
18// in errors:
19// 1) The base checkout directory of tape might change. Because stack traces
20// include absolute paths, the stack traces will change depending on the
21// checkout path. We handle this by replacing the base test directory with a
22// placeholder $TEST variable.
23// 2) Line positions within the file might change. We handle this by replacing
24// line and column markers with placeholder $LINE and $COL "variables"
25// a) node 0.8 does not provide nested eval line numbers, so we remove them
26// 3) Stacks themselves change frequently with refactoring. We've even run into
27// issues with node library refactorings "breaking" stack traces. Most of
28// these changes are irrelevant to the tests themselves. To counter this, we
29// strip out all stack frames that aren't directly under our test directory,
30// and replace them with placeholders.
31module.exports.stripFullStack = function (output) {
32 var stripped = ' [... stack stripped ...]';
33 var withDuplicates = output.split('\n').map(function (line) {
34 var m = line.match(/[ ]{8}at .*\((.*)\)/);
35
36 var stripChangingData = function (line) {
37 var withoutDirectory = line.replace(__dirname, '$TEST');
38 var withoutLineNumbers = withoutDirectory.replace(/:\d+:\d+/g, ':$LINE:$COL');
39 var withoutNestedLineNumbers = withoutLineNumbers.replace(/, \<anonymous\>:\$LINE:\$COL\)$/, ')');
40 return withoutNestedLineNumbers;
41 }
42
43 if (m) {
44 if (m[1].slice(0, __dirname.length) === __dirname) {
45 return stripChangingData(line);
46 }
47 return stripped;
48 }
49 return stripChangingData(line);
50 })
51
52 var deduped = withDuplicates.filter(function (line, ix) {
53 var hasPrior = line === stripped && withDuplicates[ix - 1] === stripped;
54 return !hasPrior;
55 });
56
57 return deduped.join('\n');
58}