Skip to content

Commit

Permalink
Use eslint in functions/ (#1009)
Browse files Browse the repository at this point in the history
Fixes #992
  • Loading branch information
fhinkel authored Dec 7, 2018
1 parent 455989a commit 43cb78c
Show file tree
Hide file tree
Showing 92 changed files with 1,971 additions and 1,491 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
**/node_modules/*
functions/**
appengine/parse-server/cloud/main.js
6 changes: 3 additions & 3 deletions functions/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ const requestPromiseNative = require('request-promise-native');
* @param {object} event.data The event data.
* @returns {Promise}
*/
exports.helloPromise = (event) => {
exports.helloPromise = event => {
return requestPromiseNative({
uri: event.data.endpoint
uri: event.data.endpoint,
});
};
// [END functions_background_promise]
Expand All @@ -61,7 +61,7 @@ exports.helloPromise = (event) => {
* @param {object} event The Cloud Functions event.
* @param {object} event.data The event data.
*/
exports.helloSynchronous = (event) => {
exports.helloSynchronous = event => {
// This function returns synchronously
if (event.data.something === true) {
return 'Something is true!';
Expand Down
5 changes: 1 addition & 4 deletions functions/background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=4.3.2"
"node": ">=8"
},
"scripts": {
"lint": "semistandard '**/*.js'",
"pretest": "npm run lint",
"test": "ava -T 20s --verbose test/*.test.js"
},
"dependencies": {
Expand All @@ -24,7 +22,6 @@
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"ava": "0.25.0",
"proxyquire": "2.1.0",
"semistandard": "^12.0.1",
"sinon": "4.4.2"
},
"cloud-repo-tools": {
Expand Down
70 changes: 39 additions & 31 deletions functions/background/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

function getSample () {
function getSample() {
const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`));

return {
program: proxyquire(`../`, {
'request-promise-native': requestPromiseNative
'request-promise-native': requestPromiseNative,
}),
mocks: {
requestPromiseNative: requestPromiseNative
}
requestPromiseNative: requestPromiseNative,
},
};
}

test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);

test.serial(`should echo message`, (t) => {
test.serial(`should echo message`, t => {
const event = {
data: {
myMessage: `hi`
}
myMessage: `hi`,
},
};
const sample = getSample();
const callback = sinon.stub();
Expand All @@ -53,45 +53,53 @@ test.serial(`should echo message`, (t) => {
t.deepEqual(callback.firstCall.args, []);
});

test.serial(`should say no message was provided`, (t) => {
test.serial(`should say no message was provided`, t => {
const error = new Error(`No message defined!`);
const callback = sinon.stub();
const sample = getSample();
sample.program.helloWorld({ data: {} }, callback);
sample.program.helloWorld({data: {}}, callback);

t.is(callback.callCount, 1);
t.deepEqual(callback.firstCall.args, [error]);
});

test.serial(`should make a promise request`, (t) => {
test.serial(`should make a promise request`, t => {
const sample = getSample();
const event = {
data: {
endpoint: `foo.com`
}
endpoint: `foo.com`,
},
};

return sample.program.helloPromise(event)
.then((result) => {
t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]);
t.is(result, `test`);
});
return sample.program.helloPromise(event).then(result => {
t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [
{uri: `foo.com`},
]);
t.is(result, `test`);
});
});

test.serial(`should return synchronously`, (t) => {
t.is(getSample().program.helloSynchronous({
data: {
something: true
}
}), `Something is true!`);
});

test.serial(`should throw an error`, (t) => {
t.throws(() => {
test.serial(`should return synchronously`, t => {
t.is(
getSample().program.helloSynchronous({
data: {
something: false
}
});
}, Error, `Something was not true!`);
something: true,
},
}),
`Something is true!`
);
});

test.serial(`should throw an error`, t => {
t.throws(
() => {
getSample().program.helloSynchronous({
data: {
something: false,
},
});
},
Error,
`Something was not true!`
);
});
4 changes: 4 additions & 0 deletions functions/billing/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
rules:
no-unused-vars: off

44 changes: 24 additions & 20 deletions functions/billing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

// [START functions_billing_limit]
// [START functions_billing_stop]
const { google } = require('googleapis');
const { auth } = require('google-auth-library');
const {google} = require('googleapis');
const {auth} = require('google-auth-library');

const PROJECT_ID = process.env.GCP_PROJECT;
const PROJECT_NAME = `projects/${PROJECT_ID}`;
Expand All @@ -38,7 +38,7 @@ exports.notifySlack = async (data, context) => {
const res = await slack.chat.postMessage({
token: BOT_ACCESS_TOKEN,
channel: CHANNEL,
text: budgetNotificationText
text: budgetNotificationText,
});
console.log(res);
};
Expand Down Expand Up @@ -71,13 +71,13 @@ const _setAuthCredential = async () => {
if (client.hasScopes && !client.hasScopes()) {
client = client.createScoped([
'https://www.googleapis.com/auth/cloud-billing',
'https://www.googleapis.com/auth/cloud-platform'
'https://www.googleapis.com/auth/cloud-platform',
]);
}

// Set credential globally for all requests
google.options({
auth: client
auth: client,
});
};

Expand All @@ -86,8 +86,8 @@ const _setAuthCredential = async () => {
* @param {string} projectName Name of project to check if billing is enabled
* @return {bool} Whether project has billing enabled or not
*/
const _isBillingEnabled = async (projectName) => {
const res = await billing.getBillingInfo({ name: projectName });
const _isBillingEnabled = async projectName => {
const res = await billing.getBillingInfo({name: projectName});
return res.data.billingEnabled;
};

Expand All @@ -96,10 +96,10 @@ const _isBillingEnabled = async (projectName) => {
* @param {string} projectName Name of project disable billing on
* @return {string} Text containing response from disabling billing
*/
const _disableBillingForProject = async (projectName) => {
const _disableBillingForProject = async projectName => {
const res = await billing.updateBillingInfo({
name: projectName,
resource: { 'billingAccountName': '' } // Disable billing
resource: {billingAccountName: ''}, // Disable billing
});
return `Billing disabled: ${JSON.stringify(res.data)}`;
};
Expand All @@ -126,7 +126,7 @@ exports.limitUse = async (data, context) => {
const _listRunningInstances = async (projectId, zone) => {
const res = await compute.instances.list({
project: projectId,
zone: zone
zone: zone,
});

const instances = res.data.items || [];
Expand All @@ -142,15 +142,19 @@ const _stopInstances = async (projectId, zone, instanceNames) => {
if (!instanceNames.length) {
return 'No running instances were found.';
}
await Promise.all(instanceNames.map(instanceName => {
return compute.instances.stop({
project: projectId,
zone: zone,
instance: instanceName
}).then((res) => {
console.log('Instance stopped successfully: ' + instanceName);
return res.data;
});
}));
await Promise.all(
instanceNames.map(instanceName => {
return compute.instances
.stop({
project: projectId,
zone: zone,
instance: instanceName,
})
.then(res => {
console.log('Instance stopped successfully: ' + instanceName);
return res.data;
});
})
);
};
// [END functions_billing_limit]
6 changes: 3 additions & 3 deletions functions/billing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "0.0.1",
"description": "Examples of integrating Cloud Functions with billing",
"main": "index.js",
"engines": {
"node": ">=8"
},
"scripts": {
"lint": "semistandard '**/*.js'",
"pretest": "npm run lint",
"test": "ava test/*"
},
"author": "Ace Nassri <[email protected]>",
Expand All @@ -19,7 +20,6 @@
"@google-cloud/nodejs-repo-tools": "^2.2.5",
"ava": "^0.25.0",
"proxyquire": "^2.1.0",
"semistandard": "^13.0.0",
"sinon": "^6.3.4"
}
}
Loading

0 comments on commit 43cb78c

Please sign in to comment.