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

Commit

Permalink
drop test dependency on proxyquire (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots authored Jul 6, 2017
1 parent 0679ec7 commit 7ccdcd7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"merge2": "^1.0.3",
"mocha": "^3.0.0",
"nock": "^9.0.0",
"proxyquire": "^1.7.11",
"request": "^2.81.0",
"source-map-support": "^0.4.15",
"tslint": "^5.4.3",
Expand Down
6 changes: 2 additions & 4 deletions test/fixtures/fib.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ function fib(n) {
* limitations under the License.
*/

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

var debuglet = require('../..').start({
debug: {
Expand Down
35 changes: 26 additions & 9 deletions test/test-debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
*/
'use strict';

var proxyquire = require('proxyquire');
proxyquire('gcp-metadata', {
'retry-request': require('request')
});

var _ = require('lodash');
var assert = require('assert');
var DEFAULT_CONFIG = require('../build/src/agent/config.js').default;
Expand Down Expand Up @@ -61,6 +56,12 @@ function verifyBreakpointRejection(re, body) {
return status.isError && hasCorrectDescription;
}

function mockedGetProjectId(cb) {
setImmediate(() => {
cb(new Error('network unavailable'));
});
};

describe('Debuglet', function() {
describe('setup', function() {
before(function() { oldGP = process.env.GCLOUD_PROJECT; });
Expand All @@ -87,10 +88,10 @@ describe('Debuglet', function() {
});

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

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

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

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

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

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

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

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

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

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

nocks.projectId('project-via-metadata');
var scope = nock(API)
.post(REGISTER_PATH)
.reply(404)
Expand Down Expand Up @@ -396,6 +402,7 @@ describe('Debuglet', function() {
var debuglet = new Debuglet(debug, defaultConfig);

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

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

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

nocks.projectId('project-via-metadata');
var scope =
nock(API)
.post(REGISTER_PATH)
Expand Down Expand Up @@ -495,6 +505,7 @@ describe('Debuglet', function() {
{projectId: 'fake-project', credentials: fakeCredentials});
var debuglet = new Debuglet(debug, defaultConfig);

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

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

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

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

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

nocks.projectId('project-via-metadata');
var scope =
nock(API)
.post(REGISTER_PATH)
Expand Down Expand Up @@ -713,6 +729,7 @@ describe('Debuglet', function() {
});
this.timeout(6000);

nocks.projectId('project-via-metadata');
var scope =
nock(API)
.post(REGISTER_PATH)
Expand Down
2 changes: 2 additions & 0 deletions test/test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
var assert = require('assert');
var module = require('..');
var nock = require('nock');
var nocks = require('./nocks.js');

nock.disableNetConnect();

describe('Debug module', function() {
before(function(done) {
nocks.projectId('project-via-metadata');
var debuglet = module.start(
{projectId: '0', debug: {forceNewAgent_: true, testMode_: true}});
debuglet.on('started', function() {
Expand Down
3 changes: 3 additions & 0 deletions test/test-options-credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('test-options-credentials', function() {
setImmediate(done);
return true;
});
nocks.projectId('project-via-metadata');
debuglet = new Debuglet(debug, config);
debuglet.start();
});
Expand All @@ -83,6 +84,7 @@ describe('test-options-credentials', function() {
setImmediate(done);
return true;
});
nocks.projectId('project-via-metadata');
debuglet = new Debuglet(debug, config);
debuglet.start();
});
Expand Down Expand Up @@ -113,6 +115,7 @@ describe('test-options-credentials', function() {
setImmediate(done);
return true;
});
nocks.projectId('project-via-metadata');
['client_id', 'client_secret', 'refresh_token'].forEach(function(field) {
assert(fileCredentials.hasOwnProperty(field));
assert(options.credentials.hasOwnProperty(field));
Expand Down

0 comments on commit 7ccdcd7

Please sign in to comment.