Skip to content

Commit

Permalink
Fixed linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Dec 14, 2016
1 parent df9a1cf commit b05773b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
10 changes: 5 additions & 5 deletions packages/common/src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

'use strict';

var request = require('request');
var util = require('./util.js');

var METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1';
Expand All @@ -42,7 +41,7 @@ metadata.getMetadataValue = getMetadataValue;
/**
* Attempts to retreive the project id for the current active project from the
* metadata service (See https://cloud.google.com/compute/docs/metadata).
*
*
* @param {Object} [headers] - An optional set of headers to include in the http
* request. This function may mutate the given headers object.
* @param {function(?, number):?} callback an (err, result) style callback
Expand All @@ -53,15 +52,16 @@ function getProjectId(headers, callback) {
headers = {};
}
getMetadataValue('/project/project-id', headers,
function (err, projectId, response) {
function(err, projectId, response) {
if (!err && response.statusCode === 200) {
return callback(null, projectId);
} else if (err && err.code === 'ENOTFOUND') {
}
if (err && err.code === 'ENOTFOUND') {
return callback(new Error('Could not auto-discover project-id.' +
'Please export GCLOUD_PROJECT with your project name'), null);
}
return callback(err || new Error('Error discovering project id'), null);
});
});
}

metadata.getProjectId = getProjectId;
Expand Down
42 changes: 19 additions & 23 deletions packages/common/test/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'use strict';
var assert = require('assert');
var nock = require('nock');
var path = require('path');
var proxyquire = require('proxyquire');

describe('common/metadata', function() {
Expand Down Expand Up @@ -62,7 +61,7 @@ describe('common/metadata', function() {
done();
});
});

it('should be able handle 500\'s from the service',
function(done) {
var scope = nock('http://metadata.google.internal')
Expand All @@ -83,26 +82,26 @@ describe('common/metadata', function() {
it('should accept an optional headers parameter', function(done) {
var scope =
nock('http://metadata.google.internal', {
reqheaders: {'Flux': 'Capacitor'}
reqheaders: { Flux: 'Capacitor' }
})
.get('/computeMetadata/v1/project/project-id')
.reply(200, 'a-stub-project-id');
metadata.getProjectId({'Flux': 'Capacitor'}, function(err, project) {
metadata.getProjectId({ Flux: 'Capacitor' }, function(err, project) {
assert.ok(!err);
assert.strictEqual(project, 'a-stub-project-id');
scope.done();
done();
});
});

it('Should callback with ENOTFOUND', function (done) {
it('should callback with ENOTFOUND', function(done) {
var oldEnv = process.env.GCLOUD_PROJECT;
process.env.GCLOUD_PROJECT = './this-should-not-exist.json';
var scope = nock('http://metadata.google.internal')
.get('/computeMetadata/v1/project/project-id')
.once()
.replyWithError({'message': 'Not Found', code: 'ENOTFOUND'});
metadata.getProjectId(function (e, result) {
.replyWithError({ message: 'Not Found', code: 'ENOTFOUND' });
metadata.getProjectId(function(e, result) {
assert.ok(e instanceof Error, 'e should be an instance of Error');
assert.deepEqual(result, null);
process.env.GCLOUD_PROJECT = oldEnv;
Expand All @@ -114,30 +113,29 @@ describe('common/metadata', function() {

describe('getInstanceId - valid cases', function() {
var STUB_ID = 'a-stub-instance-id';
it(
'Should be able to get the instance id without additional headers supplied',
function (done) {
it('should be able to get the instance id without additional headers ' +
'supplied',
function(done) {
var mock = nock('http://metadata.google.internal/computeMetadata/v1')
.get('/instance/id')
.once()
.reply(200, STUB_ID);
metadata.getInstanceId(function (err, id) {
metadata.getInstanceId(function(err, id) {
assert.deepEqual(err, null, 'Error should be null');
assert.deepEqual(STUB_ID, id, 'The id should be the stub id');
mock.done();
done();
});
}
);
it(
'Should be able to get the instance id with additional headers supplied',
function (done) {
it('should be able to get the instance id with additional headers supplied',
function(done) {
var mock = nock('http://metadata.google.internal/computeMetadata/v1',
{reqHeaders: {'x-custom-header': 'true'}})
.get('/instance/id')
.once()
.reply(200, STUB_ID);
metadata.getInstanceId({'x-custom-header': 'true'}, function (err, id) {
metadata.getInstanceId({'x-custom-header': 'true'}, function(err, id) {
assert.deepEqual(err, null, 'Error should be null');
assert.deepEqual(STUB_ID, id, 'The id should be the stub id');
mock.done();
Expand All @@ -149,14 +147,13 @@ describe('common/metadata', function() {

describe('getHostname - valid cases', function() {
var STUB_HOSTNAME = 'a-stub-hostname';
it(
'Should be able to get the hostname without additional headers supplied',
function (done) {
it('should be able to get the hostname without additional headers supplied',
function(done) {
var mock = nock('http://metadata.google.internal/computeMetadata/v1')
.get('/instance/hostname')
.once()
.reply(200, STUB_HOSTNAME);
metadata.getHostname(function (err, id) {
metadata.getHostname(function(err, id) {
assert.deepEqual(err, null, 'Error should be null');
assert.deepEqual(STUB_HOSTNAME, id,
'The hostname should be the stub hostname');
Expand All @@ -165,15 +162,14 @@ describe('common/metadata', function() {
});
}
);
it(
'Should be able to get the hostname with additional headers supplied',
function (done) {
it('should be able to get the hostname with additional headers supplied',
function(done) {
var mock = nock('http://metadata.google.internal/computeMetadata/v1',
{reqHeaders: {'x-custom-header': 'true'}})
.get('/instance/hostname')
.once()
.reply(200, STUB_HOSTNAME);
metadata.getHostname({'x-custom-header': 'true'}, function (err, id) {
metadata.getHostname({'x-custom-header': 'true'}, function(err, id) {
assert.deepEqual(err, null, 'Error should be null');
assert.deepEqual(STUB_HOSTNAME, id,
'The hostname should be the stub hostname');
Expand Down
6 changes: 3 additions & 3 deletions packages/common/test/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ function override(name, object) {
return cachedObject[methodName].apply(this, args);
};

object[methodName].unoverride_ = function () {
object[methodName].unoverride_ = function() {
object[methodName] = cachedObject[methodName];
};
});
}

// Reverses the override function.
function unoverride(object) {
Object.keys(object).forEach(function (methodName) {
Object.keys(object).forEach(function(methodName) {
if (object[methodName].unoverride_) {
object[methodName].unoverride_();
}
});
}

// Resets all overridden functions.
// Resets all overridden functions.
function resetOverrides() {
overrides = Object.keys(overrides).reduce(function(acc, name) {
acc[name] = {};
Expand Down
4 changes: 2 additions & 2 deletions packages/common/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ describe('common/util', function() {

after(function() {
// Set each function in util to its original value
Object.keys(util).forEach(function (utilMethod) {
Object.keys(util).forEach(function(utilMethod) {
if (util[utilMethod].unoverride_) {
util[utilMethod].unoverride_();
}
})
});
});

it('should have set correct defaults on Request', function() {
Expand Down

0 comments on commit b05773b

Please sign in to comment.