Skip to content

Commit

Permalink
tests: fix snippet tests in sandbox env
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Apr 10, 2017
1 parent 021a97d commit 253f7e5
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/logging/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ Logging.prototype.getEntries = function(options, callback) {
Logging.prototype.getEntriesStream = function(options) {
var self = this;

options = options || {};

var requestStream;

var userStream = streamEvents(pumpify.obj());
Expand Down Expand Up @@ -616,6 +618,10 @@ Logging.prototype.request = function(config, callback) {
}

function makeRequestCallback() {
if (global.GCLOUD_SANDBOX_ENV) {
return;
}

prepareGaxRequest(function(err, requestFn) {
if (err) {
callback(err);
Expand All @@ -627,6 +633,10 @@ Logging.prototype.request = function(config, callback) {
}

function makeRequestStream() {
if (global.GCLOUD_SANDBOX_ENV) {
return through.obj();
}

prepareGaxRequest(function(err, requestFn) {
if (err) {
stream.destroy(err);
Expand Down
7 changes: 7 additions & 0 deletions packages/logging/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ describe('Logging', function() {

stream.abort();
});

it('should not require an options object', function() {
assert.doesNotThrow(function() {
var stream = logging.getEntriesStream();
stream.emit('reading');
});
});
});

describe('getSinks', function() {
Expand Down
8 changes: 8 additions & 0 deletions packages/spanner/src/session-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ SessionPool.prototype.release = function(session) {
* Make an API request, first assuring an active session is used.
*/
SessionPool.prototype.request = function(config, callback) {
if (global.GCLOUD_SANDBOX_ENV) {
return;
}

var self = this;

this.getSession(function(err, session) {
Expand All @@ -316,6 +320,10 @@ SessionPool.prototype.request = function(config, callback) {
* Make an API request as a stream, first assuring an active session is used.
*/
SessionPool.prototype.requestStream = function(config) {
if (global.GCLOUD_SANDBOX_ENV) {
return through.obj();
}

var self = this;

var requestStream;
Expand Down
30 changes: 30 additions & 0 deletions packages/spanner/test/session-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,20 @@ describe('SessionPool', function() {
sessionPool.release = util.noop;
});

it('should return if in the snippet sandbox', function(done) {
sessionPool.getSession = function() {
done(new Error('Should not get a session in the sandbox.'));
};

global.GCLOUD_SANDBOX_ENV = true;
var returnValue = sessionPool.request(CONFIG, assert.ifError);
delete global.GCLOUD_SANDBOX_ENV;

assert.strictEqual(returnValue, undefined);

done();
});

it('should get a session', function(done) {
sessionPool.getSession = function() {
done();
Expand Down Expand Up @@ -838,6 +852,22 @@ describe('SessionPool', function() {
sessionPool.release = util.noop;
});

it('should return if in the snippet sandbox', function(done) {
sessionPool.getSession = function() {
done(new Error('Should not get a session in the sandbox.'));
};

global.GCLOUD_SANDBOX_ENV = true;
var returnValue = sessionPool.requestStream(CONFIG);
delete global.GCLOUD_SANDBOX_ENV;

assert(returnValue instanceof require('stream'));

returnValue.emit('reading');

done();
});

it('should get a session when stream opens', function(done) {
sessionPool.getSession = function() {
done();
Expand Down
7 changes: 7 additions & 0 deletions packages/speech/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ Speech.detectEncoding_ = function(filename) {
* @private
*/
Speech.findFile_ = function(file, callback) {
if (global.GCLOUD_SANDBOX_ENV) {
callback(null, {
content: new Buffer('')
});
return;
}

if (common.util.isCustomType(file, 'storage/file')) {
// File is an instance of module:storage/file.
callback(null, {
Expand Down
15 changes: 15 additions & 0 deletions packages/speech/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ describe('Speech', function() {
});

describe('findFile_', function() {
it('should return buffer for snippet sandbox', function(done) {
global.GCLOUD_SANDBOX_ENV = true;

Speech.findFile_({}, function(err, foundFile) {
delete global.GCLOUD_SANDBOX_ENV;
assert.ifError(err);

assert.deepEqual(foundFile, {
content: new Buffer('')
});

done();
});
});

it('should convert a File object', function(done) {
var file = {
bucket: {
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@ Bucket.prototype.makePublic = function(options, callback) {
* });
*/
Bucket.prototype.upload = function(localPath, options, callback) {
if (global.GCLOUD_SANDBOX_ENV) {
return;
}

if (is.fn(options)) {
callback = options;
options = {};
Expand Down
7 changes: 7 additions & 0 deletions packages/storage/test/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,13 @@ describe('Bucket', function() {
};
});

it('should return early in snippet sandbox', function() {
global.GCLOUD_SANDBOX_ENV = true;
var returnValue = bucket.upload(filepath, assert.ifError);
delete global.GCLOUD_SANDBOX_ENV;
assert.strictEqual(returnValue, undefined);
});

it('should accept a path & cb', function(done) {
bucket.upload(filepath, function(err, file) {
assert.ifError(err);
Expand Down
9 changes: 9 additions & 0 deletions packages/vision/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,15 @@ Vision.prototype.readDocument = function(images, options, callback) {
* @private
*/
Vision.findImages_ = function(images, callback) {
if (global.GCLOUD_SANDBOX_ENV) {
callback(null, [
{
content: new Buffer('')
}
]);
return;
};

var MAX_PARALLEL_LIMIT = 5;
images = arrify(images);

Expand Down
17 changes: 17 additions & 0 deletions packages/vision/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,23 @@ describe('Vision', function() {
});

describe('findImages_', function() {
it('should return buffer for snippet sandbox', function(done) {
global.GCLOUD_SANDBOX_ENV = true;

Vision.findImages_({}, function(err, images) {
delete global.GCLOUD_SANDBOX_ENV;
assert.ifError(err);

assert.deepEqual(images, [
{
content: new Buffer('')
}
]);

done();
});
});

it('should convert a File object', function(done) {
var file = {
name: 'file-name',
Expand Down

0 comments on commit 253f7e5

Please sign in to comment.