Skip to content

Commit

Permalink
testing (docs) preserve stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Apr 18, 2016
1 parent 6a63948 commit 32fd3f1
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions test/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var fs = require('fs');
var gcloud = require('../');
var glob = require('glob');
var mitm = require('mitm');
var prop = require('propprop');
var vm = require('vm');

var util = require('../lib/common/util.js');
Expand All @@ -33,8 +34,23 @@ function runCodeInSandbox(code, sandbox) {
timeout: 1000
});
} catch(err) {
var lineCol = err.stack.match('assert-code\.vm:(.+):(.+)');
lineCol.line = lineCol[1];
lineCol.col = lineCol[2];

var lines = code.split('\n')
.filter(function (line, index) {
if (index < lineCol.line) {
return line;
}
})
.join('\n');

err.message = '\n' + lines + '\n\n' + err.message;

throw err;
// rethrow the error with code for context and resolving issues faster.
throw new Error('\n' + code + '\n\n' + err.message);
// throw new Error('\n' + code + '\n\n' + err.message);
}
}

Expand Down Expand Up @@ -96,20 +112,18 @@ describe('documentation', function() {
global: global
};

var examples = fileDocBlocks.methods.map(function(method) {
return method.examples.map(function(example) {
return example.code;
}).join('\n');
});
fileDocBlocks.methods.forEach(function(method) {
var code = method.examples.map(prop('code')).join('\n')
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
.replace(/require\(\'gcloud/g, 'require(\'..');

var code = examples
.join('\n')
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
.replace(/require\(\'gcloud/g, 'require(\'..');
var displayName = filename
.replace('docs/json/master/', '')
.replace('.json', '.js');

it('should run tests for ' + filename + ' without errors', function() {
this.timeout(5000);
assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
it('should run tests for ' + displayName + '#' + method.id + ' without errors', function() {
assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
});
});
});
});

0 comments on commit 32fd3f1

Please sign in to comment.