Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 7ccdcd7

Browse files
authored
drop test dependency on proxyquire (#303)
1 parent 0679ec7 commit 7ccdcd7

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"merge2": "^1.0.3",
4646
"mocha": "^3.0.0",
4747
"nock": "^9.0.0",
48-
"proxyquire": "^1.7.11",
4948
"request": "^2.81.0",
5049
"source-map-support": "^0.4.15",
5150
"tslint": "^5.4.3",

test/fixtures/fib.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ function fib(n) {
2121
* limitations under the License.
2222
*/
2323

24-
var proxyquire = require('proxyquire');
25-
proxyquire('gcp-metadata', {
26-
'retry-request': require('request')
27-
});
24+
const nocks = require('../nocks.js');
25+
nocks.projectId('fake-project-id');
2826

2927
var debuglet = require('../..').start({
3028
debug: {

test/test-debuglet.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
*/
1616
'use strict';
1717

18-
var proxyquire = require('proxyquire');
19-
proxyquire('gcp-metadata', {
20-
'retry-request': require('request')
21-
});
22-
2318
var _ = require('lodash');
2419
var assert = require('assert');
2520
var DEFAULT_CONFIG = require('../build/src/agent/config.js').default;
@@ -61,6 +56,12 @@ function verifyBreakpointRejection(re, body) {
6156
return status.isError && hasCorrectDescription;
6257
}
6358

59+
function mockedGetProjectId(cb) {
60+
setImmediate(() => {
61+
cb(new Error('network unavailable'));
62+
});
63+
};
64+
6465
describe('Debuglet', function() {
6566
describe('setup', function() {
6667
before(function() { oldGP = process.env.GCLOUD_PROJECT; });
@@ -87,10 +88,10 @@ describe('Debuglet', function() {
8788
});
8889

8990
it('should not start when projectId is not available', function(done) {
90-
this.timeout(8000);
9191
var debug = require('../build/src/debug.js').Debug();
9292
var debuglet = new Debuglet(debug, defaultConfig);
9393

94+
debuglet.getProjectId_ = mockedGetProjectId;
9495
debuglet.once('initError', function(err) {
9596
assert.ok(err);
9697
// no need to stop the debuggee.
@@ -101,10 +102,10 @@ describe('Debuglet', function() {
101102
});
102103

103104
it('should not crash without project num', function(done) {
104-
this.timeout(8000);
105105
var debug = require('../build/src/debug.js').Debug();
106106
var debuglet = new Debuglet(debug, defaultConfig);
107107

108+
debuglet.getProjectId_ = mockedGetProjectId;
108109
debuglet.once('started', function() { assert.fail(); });
109110
debuglet.once('initError', function() {
110111
done();
@@ -118,6 +119,7 @@ describe('Debuglet', function() {
118119
{projectId: projectId, credentials: fakeCredentials});
119120
var debuglet = new Debuglet(debug, defaultConfig);
120121

122+
nocks.projectId('project-via-metadata');
121123
var scope = nock(API)
122124
.post(REGISTER_PATH)
123125
.reply(200, {debuggee: {id: DEBUGGEE_ID}});
@@ -144,6 +146,7 @@ describe('Debuglet', function() {
144146
var debug = require('../build/src/debug.js').Debug({credentials: fakeCredentials});
145147
var debuglet = new Debuglet(debug, defaultConfig);
146148

149+
nocks.projectId('project-via-metadata');
147150
var scope = nock(API)
148151
.post(REGISTER_PATH)
149152
.reply(200, {debuggee: {id: DEBUGGEE_ID}});
@@ -168,8 +171,7 @@ describe('Debuglet', function() {
168171
});
169172
var debuglet = new Debuglet(debug, defaultConfig);
170173

171-
// TODO: also make sure we don't request the project from metadata
172-
// service.
174+
nocks.projectId('project-via-metadata');
173175
var scope = nock(API)
174176
.post(REGISTER_PATH)
175177
.reply(200, {debuggee: {id: DEBUGGEE_ID}});
@@ -191,6 +193,7 @@ describe('Debuglet', function() {
191193
var debug = require('../build/src/debug.js').Debug({credentials: fakeCredentials});
192194
var debuglet = new Debuglet(debug, defaultConfig);
193195

196+
nocks.projectId('project-via-metadata');
194197
var scope = nock(API)
195198
.post(REGISTER_PATH)
196199
.reply(200, {debuggee: {id: DEBUGGEE_ID}});
@@ -311,6 +314,7 @@ describe('Debuglet', function() {
311314
{projectId: 'fake-project', credentials: fakeCredentials});
312315
var debuglet = new Debuglet(debug, defaultConfig);
313316

317+
nocks.projectId('project-via-metadata');
314318
var scope =
315319
nock(API).post(REGISTER_PATH, function(body) {
316320
assert.ok(
@@ -334,6 +338,7 @@ describe('Debuglet', function() {
334338
var debuglet = new Debuglet(debug, defaultConfig);
335339

336340
nocks.oauth2();
341+
nocks.projectId('project-via-metadata');
337342
var scope =
338343
nock(API).post(REGISTER_PATH, function(body) {
339344
assert.ok(_.isString(body.debuggee.labels.minorversion));
@@ -357,6 +362,7 @@ describe('Debuglet', function() {
357362
{projectId: '11020304f2934', credentials: fakeCredentials});
358363
var debuglet = new Debuglet(debug, defaultConfig);
359364

365+
nocks.projectId('project-via-metadata');
360366
var scope = nock(API)
361367
.post(REGISTER_PATH)
362368
.reply(404)
@@ -396,6 +402,7 @@ describe('Debuglet', function() {
396402
var debuglet = new Debuglet(debug, defaultConfig);
397403

398404
nocks.oauth2();
405+
nocks.projectId('project-via-metadata');
399406
var scope = nock(API)
400407
.post(REGISTER_PATH)
401408
.reply(200, {debuggee: {id: DEBUGGEE_ID}});
@@ -422,6 +429,7 @@ describe('Debuglet', function() {
422429
};
423430
var debuglet = new Debuglet(debug, defaultConfig);
424431

432+
nocks.projectId('project-via-metadata');
425433
var scope = nock(API).post(REGISTER_PATH, function(body) {
426434
return body.debuggee.sourceContexts[0] &&
427435
body.debuggee.sourceContexts[0].a === 5;
@@ -445,6 +453,7 @@ describe('Debuglet', function() {
445453
{projectId: 'fake-project', credentials: fakeCredentials});
446454
var debuglet = new Debuglet(debug, defaultConfig);
447455

456+
nocks.projectId('project-via-metadata');
448457
var scope =
449458
nock(API)
450459
.post(REGISTER_PATH)
@@ -466,6 +475,7 @@ describe('Debuglet', function() {
466475
{projectId: 'fake-project', credentials: fakeCredentials});
467476
var debuglet = new Debuglet(debug, defaultConfig);
468477

478+
nocks.projectId('project-via-metadata');
469479
var scope =
470480
nock(API)
471481
.post(REGISTER_PATH)
@@ -495,6 +505,7 @@ describe('Debuglet', function() {
495505
{projectId: 'fake-project', credentials: fakeCredentials});
496506
var debuglet = new Debuglet(debug, defaultConfig);
497507

508+
nocks.projectId('project-via-metadata');
498509
var scope = nock(API)
499510
.post(REGISTER_PATH)
500511
.reply(200, {debuggee: {id: DEBUGGEE_ID}})
@@ -522,6 +533,7 @@ describe('Debuglet', function() {
522533
{projectId: 'fake-project', credentials: fakeCredentials});
523534
var debuglet = new Debuglet(debug, defaultConfig);
524535

536+
nocks.projectId('project-via-metadata');
525537
var scope = nock(API)
526538
.post(REGISTER_PATH)
527539
.reply(200, {debuggee: {id: DEBUGGEE_ID}})
@@ -549,6 +561,7 @@ describe('Debuglet', function() {
549561
var debuglet = new Debuglet(debug, defaultConfig);
550562
debuglet.config_.allowExpressions = false;
551563

564+
nocks.projectId('project-via-metadata');
552565
var scope = nock(API)
553566
.post(REGISTER_PATH)
554567
.reply(200, { debuggee: { id: DEBUGGEE_ID } })
@@ -587,6 +600,7 @@ describe('Debuglet', function() {
587600
var debuglet = new Debuglet(debug, defaultConfig);
588601
debuglet.config_.allowExpressions = false;
589602

603+
nocks.projectId('project-via-metadata');
590604
var scope = nock(API)
591605
.post(REGISTER_PATH)
592606
.reply(200, { debuggee: { id: DEBUGGEE_ID } })
@@ -624,6 +638,7 @@ describe('Debuglet', function() {
624638
{projectId: 'fake-project', credentials: fakeCredentials});
625639
var debuglet = new Debuglet(debug, defaultConfig);
626640

641+
nocks.projectId('project-via-metadata');
627642
var scope = nock(API)
628643
.post(REGISTER_PATH)
629644
.reply(200, {debuggee: {id: DEBUGGEE_ID}})
@@ -665,6 +680,7 @@ describe('Debuglet', function() {
665680
{breakpointExpirationSec: 1, forceNewAgent_: true});
666681
this.timeout(6000);
667682

683+
nocks.projectId('project-via-metadata');
668684
var scope =
669685
nock(API)
670686
.post(REGISTER_PATH)
@@ -713,6 +729,7 @@ describe('Debuglet', function() {
713729
});
714730
this.timeout(6000);
715731

732+
nocks.projectId('project-via-metadata');
716733
var scope =
717734
nock(API)
718735
.post(REGISTER_PATH)

test/test-module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
var assert = require('assert');
2020
var module = require('..');
2121
var nock = require('nock');
22+
var nocks = require('./nocks.js');
2223

2324
nock.disableNetConnect();
2425

2526
describe('Debug module', function() {
2627
before(function(done) {
28+
nocks.projectId('project-via-metadata');
2729
var debuglet = module.start(
2830
{projectId: '0', debug: {forceNewAgent_: true, testMode_: true}});
2931
debuglet.on('started', function() {

test/test-options-credentials.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('test-options-credentials', function() {
6161
setImmediate(done);
6262
return true;
6363
});
64+
nocks.projectId('project-via-metadata');
6465
debuglet = new Debuglet(debug, config);
6566
debuglet.start();
6667
});
@@ -83,6 +84,7 @@ describe('test-options-credentials', function() {
8384
setImmediate(done);
8485
return true;
8586
});
87+
nocks.projectId('project-via-metadata');
8688
debuglet = new Debuglet(debug, config);
8789
debuglet.start();
8890
});
@@ -113,6 +115,7 @@ describe('test-options-credentials', function() {
113115
setImmediate(done);
114116
return true;
115117
});
118+
nocks.projectId('project-via-metadata');
116119
['client_id', 'client_secret', 'refresh_token'].forEach(function(field) {
117120
assert(fileCredentials.hasOwnProperty(field));
118121
assert(options.credentials.hasOwnProperty(field));

0 commit comments

Comments
 (0)