Skip to content

Commit 97336e4

Browse files
tests: fix snippet tests in sandbox env (#2199)
1 parent 8d9b607 commit 97336e4

File tree

10 files changed

+141
-0
lines changed

10 files changed

+141
-0
lines changed

packages/logging/src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ Logging.prototype.getEntries = function(options, callback) {
346346
Logging.prototype.getEntriesStream = function(options) {
347347
var self = this;
348348

349+
options = options || {};
350+
349351
var requestStream;
350352

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

618620
function makeRequestCallback() {
621+
if (global.GCLOUD_SANDBOX_ENV) {
622+
return;
623+
}
624+
619625
prepareGaxRequest(function(err, requestFn) {
620626
if (err) {
621627
callback(err);
@@ -627,6 +633,10 @@ Logging.prototype.request = function(config, callback) {
627633
}
628634

629635
function makeRequestStream() {
636+
if (global.GCLOUD_SANDBOX_ENV) {
637+
return through.obj();
638+
}
639+
630640
prepareGaxRequest(function(err, requestFn) {
631641
if (err) {
632642
stream.destroy(err);

packages/logging/test/index.js

+34
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,13 @@ describe('Logging', function() {
591591

592592
stream.abort();
593593
});
594+
595+
it('should not require an options object', function() {
596+
assert.doesNotThrow(function() {
597+
var stream = logging.getEntriesStream();
598+
stream.emit('reading');
599+
});
600+
});
594601
});
595602

596603
describe('getSinks', function() {
@@ -889,6 +896,19 @@ describe('Logging', function() {
889896
});
890897

891898
describe('makeRequestCallback', function() {
899+
it('should return if in snippet sandbox', function(done) {
900+
logging.auth.getProjectId = function() {
901+
done(new Error('Should not have gotten project ID.'));
902+
};
903+
904+
global.GCLOUD_SANDBOX_ENV = true;
905+
var returnValue = logging.request(CONFIG, assert.ifError);
906+
delete global.GCLOUD_SANDBOX_ENV;
907+
908+
assert.strictEqual(returnValue, undefined);
909+
done();
910+
});
911+
892912
it('should prepare the request', function(done) {
893913
logging.api[CONFIG.client][CONFIG.method] = {
894914
bind: function(gaxClient, reqOpts, gaxOpts) {
@@ -944,6 +964,20 @@ describe('Logging', function() {
944964
};
945965
});
946966

967+
it('should return if in snippet sandbox', function(done) {
968+
logging.auth.getProjectId = function() {
969+
done(new Error('Should not have gotten project ID.'));
970+
};
971+
972+
global.GCLOUD_SANDBOX_ENV = true;
973+
var returnValue = logging.request(CONFIG);
974+
returnValue.emit('reading');
975+
delete global.GCLOUD_SANDBOX_ENV;
976+
977+
assert(returnValue instanceof require('stream'));
978+
done();
979+
});
980+
947981
it('should expose an abort function', function(done) {
948982
GAX_STREAM.cancel = done;
949983

packages/spanner/src/session-pool.js

+8
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ SessionPool.prototype.release = function(session) {
295295
* Make an API request, first assuring an active session is used.
296296
*/
297297
SessionPool.prototype.request = function(config, callback) {
298+
if (global.GCLOUD_SANDBOX_ENV) {
299+
return;
300+
}
301+
298302
var self = this;
299303

300304
this.getSession(function(err, session) {
@@ -316,6 +320,10 @@ SessionPool.prototype.request = function(config, callback) {
316320
* Make an API request as a stream, first assuring an active session is used.
317321
*/
318322
SessionPool.prototype.requestStream = function(config) {
323+
if (global.GCLOUD_SANDBOX_ENV) {
324+
return through.obj();
325+
}
326+
319327
var self = this;
320328

321329
var requestStream;

packages/spanner/test/session-pool.js

+30
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,20 @@ describe('SessionPool', function() {
752752
sessionPool.release = util.noop;
753753
});
754754

755+
it('should return if in the snippet sandbox', function(done) {
756+
sessionPool.getSession = function() {
757+
done(new Error('Should not get a session in the sandbox.'));
758+
};
759+
760+
global.GCLOUD_SANDBOX_ENV = true;
761+
var returnValue = sessionPool.request(CONFIG, assert.ifError);
762+
delete global.GCLOUD_SANDBOX_ENV;
763+
764+
assert.strictEqual(returnValue, undefined);
765+
766+
done();
767+
});
768+
755769
it('should get a session', function(done) {
756770
sessionPool.getSession = function() {
757771
done();
@@ -838,6 +852,22 @@ describe('SessionPool', function() {
838852
sessionPool.release = util.noop;
839853
});
840854

855+
it('should return if in the snippet sandbox', function(done) {
856+
sessionPool.getSession = function() {
857+
done(new Error('Should not get a session in the sandbox.'));
858+
};
859+
860+
global.GCLOUD_SANDBOX_ENV = true;
861+
var returnValue = sessionPool.requestStream(CONFIG);
862+
delete global.GCLOUD_SANDBOX_ENV;
863+
864+
assert(returnValue instanceof require('stream'));
865+
866+
returnValue.emit('reading');
867+
868+
done();
869+
});
870+
841871
it('should get a session when stream opens', function(done) {
842872
sessionPool.getSession = function() {
843873
done();

packages/speech/src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ Speech.detectEncoding_ = function(filename) {
161161
* @private
162162
*/
163163
Speech.findFile_ = function(file, callback) {
164+
if (global.GCLOUD_SANDBOX_ENV) {
165+
callback(null, {
166+
content: new Buffer('')
167+
});
168+
return;
169+
}
170+
164171
if (common.util.isCustomType(file, 'storage/file')) {
165172
// File is an instance of module:storage/file.
166173
callback(null, {

packages/speech/test/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ describe('Speech', function() {
211211
});
212212

213213
describe('findFile_', function() {
214+
it('should return buffer for snippet sandbox', function(done) {
215+
global.GCLOUD_SANDBOX_ENV = true;
216+
217+
Speech.findFile_({}, function(err, foundFile) {
218+
delete global.GCLOUD_SANDBOX_ENV;
219+
assert.ifError(err);
220+
221+
assert.deepEqual(foundFile, {
222+
content: new Buffer('')
223+
});
224+
225+
done();
226+
});
227+
});
228+
214229
it('should convert a File object', function(done) {
215230
var file = {
216231
bucket: {

packages/storage/src/bucket.js

+4
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,10 @@ Bucket.prototype.makePublic = function(options, callback) {
11671167
* });
11681168
*/
11691169
Bucket.prototype.upload = function(localPath, options, callback) {
1170+
if (global.GCLOUD_SANDBOX_ENV) {
1171+
return;
1172+
}
1173+
11701174
if (is.fn(options)) {
11711175
callback = options;
11721176
options = {};

packages/storage/test/bucket.js

+7
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,13 @@ describe('Bucket', function() {
913913
};
914914
});
915915

916+
it('should return early in snippet sandbox', function() {
917+
global.GCLOUD_SANDBOX_ENV = true;
918+
var returnValue = bucket.upload(filepath, assert.ifError);
919+
delete global.GCLOUD_SANDBOX_ENV;
920+
assert.strictEqual(returnValue, undefined);
921+
});
922+
916923
it('should accept a path & cb', function(done) {
917924
bucket.upload(filepath, function(err, file) {
918925
assert.ifError(err);

packages/vision/src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,15 @@ Vision.prototype.readDocument = function(images, options, callback) {
17121712
* @private
17131713
*/
17141714
Vision.findImages_ = function(images, callback) {
1715+
if (global.GCLOUD_SANDBOX_ENV) {
1716+
callback(null, [
1717+
{
1718+
content: new Buffer('')
1719+
}
1720+
]);
1721+
return;
1722+
}
1723+
17151724
var MAX_PARALLEL_LIMIT = 5;
17161725
images = arrify(images);
17171726

packages/vision/test/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,23 @@ describe('Vision', function() {
10081008
});
10091009

10101010
describe('findImages_', function() {
1011+
it('should return buffer for snippet sandbox', function(done) {
1012+
global.GCLOUD_SANDBOX_ENV = true;
1013+
1014+
Vision.findImages_({}, function(err, images) {
1015+
delete global.GCLOUD_SANDBOX_ENV;
1016+
assert.ifError(err);
1017+
1018+
assert.deepEqual(images, [
1019+
{
1020+
content: new Buffer('')
1021+
}
1022+
]);
1023+
1024+
done();
1025+
});
1026+
});
1027+
10111028
it('should convert a File object', function(done) {
10121029
var file = {
10131030
name: 'file-name',

0 commit comments

Comments
 (0)