Skip to content

Commit 55aeb1f

Browse files
committed
Merge pull request #1244 from stephenplusplus/spp--doc-test-stacktrace
testing (docs) preserve stack trace
2 parents 84df6f6 + 7d386a1 commit 55aeb1f

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

test/docs.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var fs = require('fs');
2121
var gcloud = require('../');
2222
var glob = require('glob');
2323
var mitm = require('mitm');
24+
var prop = require('propprop');
2425
var vm = require('vm');
2526

2627
var util = require('../lib/common/util.js');
@@ -34,7 +35,21 @@ function runCodeInSandbox(code, sandbox) {
3435
});
3536
} catch(err) {
3637
// rethrow the error with code for context and resolving issues faster.
37-
throw new Error('\n' + code + '\n\n' + err.message);
38+
var lineCol = err.stack.match('assert-code\.vm:(.+):(.+)');
39+
lineCol.line = lineCol[1];
40+
lineCol.col = lineCol[2];
41+
42+
var lines = code.split('\n')
43+
.filter(function(line, index) {
44+
if (index < lineCol.line) {
45+
return line;
46+
}
47+
})
48+
.join('\n');
49+
50+
err.message = '\n' + lines + '\n\n' + err.message;
51+
52+
throw err;
3853
}
3954
}
4055

@@ -96,20 +111,20 @@ describe('documentation', function() {
96111
global: global
97112
};
98113

99-
var examples = fileDocBlocks.methods.map(function(method) {
100-
return method.examples.map(function(example) {
101-
return example.code;
102-
}).join('\n');
103-
});
114+
fileDocBlocks.methods.forEach(function(method) {
115+
var code = method.examples.map(prop('code')).join('\n')
116+
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
117+
.replace(/require\(\'gcloud/g, 'require(\'..');
118+
119+
var displayName = filename
120+
.replace('docs/json/master/', '')
121+
.replace('.json', '.js');
104122

105-
var code = examples
106-
.join('\n')
107-
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
108-
.replace(/require\(\'gcloud/g, 'require(\'..');
123+
displayName += '#' + method.id;
109124

110-
it('should run tests for ' + filename + ' without errors', function() {
111-
this.timeout(5000);
112-
assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
125+
it('should run ' + displayName + ' example without errors', function() {
126+
assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
127+
});
113128
});
114129
});
115130
});

0 commit comments

Comments
 (0)