Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: fix snippet tests in sandbox env #2199

Merged
merged 4 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 34 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 Expand Up @@ -889,6 +896,19 @@ describe('Logging', function() {
});

describe('makeRequestCallback', function() {
it('should return if in snippet sandbox', function(done) {
logging.auth.getProjectId = function() {
done(new Error('Should not have gotten project ID.'));
};

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

assert.strictEqual(returnValue, undefined);
done();
});

it('should prepare the request', function(done) {
logging.api[CONFIG.client][CONFIG.method] = {
bind: function(gaxClient, reqOpts, gaxOpts) {
Expand Down Expand Up @@ -944,6 +964,20 @@ describe('Logging', function() {
};
});

it('should return if in snippet sandbox', function(done) {
logging.auth.getProjectId = function() {
done(new Error('Should not have gotten project ID.'));
};

global.GCLOUD_SANDBOX_ENV = true;
var returnValue = logging.request(CONFIG);
returnValue.emit('reading');
delete global.GCLOUD_SANDBOX_ENV;

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

it('should expose an abort function', function(done) {
GAX_STREAM.cancel = done;

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