Skip to content

Commit

Permalink
Merge pull request googleapis#1195 from callmehiphop/testing--example…
Browse files Browse the repository at this point in the history
…-reporting

docs: improved example test reporting
  • Loading branch information
stephenplusplus committed Apr 1, 2016
2 parents 367f9d4 + 181e07f commit b299aca
Showing 1 changed file with 54 additions and 60 deletions.
114 changes: 54 additions & 60 deletions test/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,14 @@ function runCodeInSandbox(code, sandbox) {

describe('documentation', function() {
var MITM;
var FILES;

before(function(done) {
before(function() {
// Set a global to indicate to any interested function inside of gcloud that
// this is a sandbox environment.
global.GCLOUD_SANDBOX_ENV = true;

// Turn off the network so that API calls aren't actually made.
MITM = mitm();

var jsonGlob = 'docs/json/master/**/*.json';
var ignore = [
'docs/json/master/types.json'
];

glob(jsonGlob, { ignore: ignore }, function(err, files) {
assert.ifError(err);
FILES = files;
done();
});
});

after(function() {
Expand All @@ -69,53 +57,59 @@ describe('documentation', function() {
MITM.disable();
});

it('should run docs examples without errors', function() {
this.timeout(5000);

FILES.forEach(function(filename) {
var fileContents = fs.readFileSync(filename, {
encoding: 'utf8'
});
var fileDocBlocks;

try {
fileDocBlocks = JSON.parse(fileContents);
} catch(e) {
throw new Error([
'Failed to parse one of the doc files (' + e.message + ')',
'Filename: ' + filename,
'File contents: "' + fileContents + '"'
].join('\n'));
}

var mockConsole = Object.keys(console).reduce(function(console, method) {
console[method] = util.noop;
return console;
}, {});

var sandbox = {
gcloud: gcloud,
require: require,
process: process,
console: mockConsole,
Buffer: Buffer,
Date: Date,
Array: Array,
global: global
};

fileDocBlocks.methods.forEach(function(method) {
var examples = method.examples.map(function(example) {
return example.code;
});

var code = examples
.join('\n')
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
.replace(/require\(\'gcloud/g, 'require(\'..');

assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
});
var jsonGlob = 'docs/json/master/**/*.json';
var ignore = [
'docs/json/master/types.json'
];

var FILES = glob.sync(jsonGlob, { ignore: ignore });

FILES.forEach(function(filename) {
var fileContents = fs.readFileSync(filename, {
encoding: 'utf8'
});
var fileDocBlocks;

try {
fileDocBlocks = JSON.parse(fileContents);
} catch(e) {
throw new Error([
'Failed to parse one of the doc files (' + e.message + ')',
'Filename: ' + filename,
'File contents: "' + fileContents + '"'
].join('\n'));
}

var mockConsole = Object.keys(console).reduce(function(console, method) {
console[method] = util.noop;
return console;
}, {});

var sandbox = {
gcloud: gcloud,
require: require,
process: process,
console: mockConsole,
Buffer: Buffer,
Date: Date,
Array: Array,
global: global
};

var examples = fileDocBlocks.methods.map(function(method) {
return method.examples.map(function(example) {
return example.code;
}).join('\n');
});

var code = examples
.join('\n')
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
.replace(/require\(\'gcloud/g, 'require(\'..');

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

0 comments on commit b299aca

Please sign in to comment.