Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run eslint on most samples #884

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
appengine/loopback/*
functions/**
iot/*
appengine/*
19 changes: 19 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
extends:
- 'eslint:recommended'
- 'plugin:node/recommended'
- prettier
env:
mocha: true
plugins:
- node
- prettier
rules:
prettier/prettier: error
block-scoped-var: error
eqeqeq: error
no-warning-comments: warn
no-console: off
node/no-missing-require: off
node/no-unpublished-require: off

3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/*
samples/node_modules/*
src/**/doc/*
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bracketSpacing: false
printWidth: 80
semi: true
singleQuote: true
tabWidth: 2
trailingComma: es5
useTabs: false
9 changes: 6 additions & 3 deletions appengine/analytics/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ app.enable('trust proxy');
// Engine, but will need to be set manually when running locally. See README.md.
const GA_TRACKING_ID = process.env.GA_TRACKING_ID;

function trackEvent (category, action, label, value) {
function trackEvent(category, action, label, value) {
const data = {
// API Version.
v: '1',
Expand All @@ -44,7 +44,7 @@ function trackEvent (category, action, label, value) {
// Event label.
el: label,
// Event value.
ev: value
ev: value,
};

return got.post('http://www.google-analytics.com/collect', data);
Expand All @@ -54,7 +54,10 @@ app.get('/', (req, res, next) => {
// Event value must be numeric.
trackEvent('Example category', 'Example action', 'Example label', '100')
.then(() => {
res.status(200).send('Event tracked.').end();
res
.status(200)
.send('Event tracked.')
.end();
})
// This sample treats an event tracking error as a fatal error. Depending
// on your application's needs, failing to track an event may not be
Expand Down
4 changes: 2 additions & 2 deletions appengine/building-an-app/update/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const path = require(`path`);
const app = express();

// [START enable_parser]
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({extended: true}));
// [END enable_parser]

app.get('/', (req, res) => {
Expand All @@ -40,7 +40,7 @@ app.get('/submit', (req, res) => {
app.post('/submit', (req, res) => {
console.log({
name: req.body.name,
message: req.body.message
message: req.body.message,
});
res.send('Thanks for your message!');
});
Expand Down
21 changes: 12 additions & 9 deletions appengine/building-an-app/update/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,42 @@ const utils = require(`@google-cloud/nodejs-repo-tools`);
const cwd = path.join(__dirname, `../`);
const requestObj = utils.getRequest({
cwd: cwd,
cmd: `server` });
cmd: `server`,
});

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

test.cb.serial(`should send greetings`, (t) => {
test.cb.serial(`should send greetings`, t => {
requestObj
.get(`/`)
.expect(200)
.expect((response) => {
.expect(response => {
t.is(response.text, `Hello from App Engine!`);
})
.end(t.end);
});

test.cb.serial(`should display form`, (t) => {
test.cb.serial(`should display form`, t => {
requestObj
.get(`/submit`)
.expect(200)
.expect((response) => {
t.true(response.text.includes('textarea name="message" placeholder="Message"'));
.expect(response => {
t.true(
response.text.includes('textarea name="message" placeholder="Message"')
);
})
.end(t.end);
});

test.cb.serial(`should record message`, (t) => {
test.cb.serial(`should record message`, t => {
requestObj
.post(`/submit`, {
name: `sample-user`,
message: `sample-message`
message: `sample-message`,
})
.expect(200)
.expect((response) => {
.expect(response => {
t.is(response.text, `Thanks for your message!`);
})
.end(t.end);
Expand Down
8 changes: 4 additions & 4 deletions appengine/cloudsql/createTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ prompt.get(FIELDS, (err, config) => {
}

// Connect to the database
const knex = Knex({ client: 'mysql', connection: config });
const knex = Knex({client: 'mysql', connection: config});

// Create the "visits" table
knex.schema.createTable('visits',
(table) => {
knex.schema
.createTable('visits', table => {
table.increments();
table.timestamp('timestamp');
table.string('userIp');
Expand All @@ -44,7 +44,7 @@ prompt.get(FIELDS, (err, config) => {
console.log(`Successfully created 'visits' table.`);
return knex.destroy();
})
.catch((err) => {
.catch(err => {
console.error(`Failed to create 'visits' table:`, err);
if (knex) {
knex.destroy();
Expand Down
34 changes: 22 additions & 12 deletions appengine/cloudsql/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ app.enable('trust proxy');

const knex = connect();

function connect () {
function connect() {
// [START gae_flex_mysql_connect]
const config = {
user: process.env.SQL_USER,
password: process.env.SQL_PASSWORD,
database: process.env.SQL_DATABASE
database: process.env.SQL_DATABASE,
};

if (process.env.INSTANCE_CONNECTION_NAME && process.env.NODE_ENV === 'production') {
if (
process.env.INSTANCE_CONNECTION_NAME &&
process.env.NODE_ENV === 'production'
) {
config.socketPath = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`;
}

// Connect to the database
const knex = Knex({
client: 'mysql',
connection: config
connection: config,
});
// [END gae_flex_mysql_connect]

Expand All @@ -57,7 +60,7 @@ function connect () {
* @param {object} visit The visit record to insert.
* @returns {Promise}
*/
function insertVisit (knex, visit) {
function insertVisit(knex, visit) {
return knex('visits').insert(visit);
}

Expand All @@ -67,13 +70,16 @@ function insertVisit (knex, visit) {
* @param {object} knex The Knex connection object.
* @returns {Promise}
*/
function getVisits (knex) {
return knex.select('timestamp', 'userIp')
function getVisits(knex) {
return knex
.select('timestamp', 'userIp')
.from('visits')
.orderBy('timestamp', 'desc')
.limit(10)
.then((results) => {
return results.map((visit) => `Time: ${visit.timestamp}, AddrHash: ${visit.userIp}`);
.then(results => {
return results.map(
visit => `Time: ${visit.timestamp}, AddrHash: ${visit.userIp}`
);
});
}

Expand All @@ -82,20 +88,24 @@ app.get('/', (req, res, next) => {
const visit = {
timestamp: new Date(),
// Store a hash of the visitor's ip address
userIp: crypto.createHash('sha256').update(req.ip).digest('hex').substr(0, 7)
userIp: crypto
.createHash('sha256')
.update(req.ip)
.digest('hex')
.substr(0, 7),
};

insertVisit(knex, visit)
// Query the last 10 visits from the database.
.then(() => getVisits(knex))
.then((visits) => {
.then(visits => {
res
.status(200)
.set('Content-Type', 'text/plain')
.send(`Last 10 visits:\n${visits.join('\n')}`)
.end();
})
.catch((err) => {
.catch(err => {
next(err);
});
});
Expand Down
50 changes: 26 additions & 24 deletions appengine/cloudsql/test/createTables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,51 @@ const tools = require(`@google-cloud/nodejs-repo-tools`);

const SAMPLE_PATH = path.join(__dirname, `../createTables.js`);

const exampleConfig = [
`user`,
`password`,
`database`
];
const exampleConfig = [`user`, `password`, `database`];

function getSample () {
function getSample() {
const configMock = exampleConfig;
const promptMock = {
start: sinon.stub(),
get: sinon.stub().yields(null, configMock)
get: sinon.stub().yields(null, configMock),
};
const tableMock = {
increments: sinon.stub(),
timestamp: sinon.stub(),
string: sinon.stub()
string: sinon.stub(),
};
const knexMock = {
schema: {
createTable: sinon.stub()
createTable: sinon.stub(),
},
destroy: sinon.stub().returns(Promise.resolve())
destroy: sinon.stub().returns(Promise.resolve()),
};

knexMock.schema.createTable.returns(Promise.resolve(knexMock)).yields(tableMock);
knexMock.schema.createTable
.returns(Promise.resolve(knexMock))
.yields(tableMock);
const KnexMock = sinon.stub().returns(knexMock);

return {
mocks: {
Knex: KnexMock,
knex: knexMock,
config: configMock,
prompt: promptMock
}
prompt: promptMock,
},
};
}

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

test.cb.serial(`should create a table`, (t) => {
test.cb.serial(`should create a table`, t => {
const sample = getSample();
const expectedResult = `Successfully created 'visits' table.`;

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt
prompt: sample.mocks.prompt,
});

t.true(sample.mocks.prompt.start.calledOnce);
Expand All @@ -78,10 +76,12 @@ test.cb.serial(`should create a table`, (t) => {

setTimeout(() => {
t.true(sample.mocks.Knex.calledOnce);
t.deepEqual(sample.mocks.Knex.firstCall.args, [{
client: 'mysql',
connection: exampleConfig
}]);
t.deepEqual(sample.mocks.Knex.firstCall.args, [
{
client: 'mysql',
connection: exampleConfig,
},
]);

t.true(sample.mocks.knex.schema.createTable.calledOnce);
t.is(sample.mocks.knex.schema.createTable.firstCall.args[0], 'visits');
Expand All @@ -92,14 +92,14 @@ test.cb.serial(`should create a table`, (t) => {
}, 10);
});

test.cb.serial(`should handle prompt error`, (t) => {
test.cb.serial(`should handle prompt error`, t => {
const error = new Error(`error`);
const sample = getSample();
sample.mocks.prompt.get = sinon.stub().yields(error);

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt
prompt: sample.mocks.prompt,
});

setTimeout(() => {
Expand All @@ -110,14 +110,16 @@ test.cb.serial(`should handle prompt error`, (t) => {
}, 10);
});

test.cb.serial(`should handle knex creation error`, (t) => {
test.cb.serial(`should handle knex creation error`, t => {
const error = new Error(`error`);
const sample = getSample();
sample.mocks.knex.schema.createTable = sinon.stub().returns(Promise.reject(error));
sample.mocks.knex.schema.createTable = sinon
.stub()
.returns(Promise.reject(error));

proxyquire(SAMPLE_PATH, {
knex: sample.mocks.Knex,
prompt: sample.mocks.prompt
prompt: sample.mocks.prompt,
});

setTimeout(() => {
Expand Down
Loading