Skip to content

Commit

Permalink
refactor: modernize the sample tests (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Dec 19, 2018
1 parent a55c43e commit 69dbf0e
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 228 deletions.
2 changes: 1 addition & 1 deletion samples/logs.js
Expand Up @@ -39,7 +39,7 @@ async function writeLogEntry(logName) {
};

// A text log entry
const entry = log.entry({resource: resource}, 'Hello, world!');
const entry = log.entry({resource}, 'Hello, world!');

// A structured log entry
const secondEntry = log.entry(
Expand Down
19 changes: 8 additions & 11 deletions samples/package.json
@@ -1,35 +1,32 @@
{
"name": "nodejs-docs-samples-logging",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": "googleapis/nodejs-logging",
"files": [
"*.js"
],
"engines": {
"node": ">=8"
},
"scripts": {
"test": "nyc mocha system-test/ test/"
"test": "mocha system-test --timeout 60000"
},
"dependencies": {
"@google-cloud/logging": "^4.1.1",
"@google-cloud/logging-bunyan": "^0.9.0",
"@google-cloud/logging-winston": "^0.10.0",
"@google-cloud/storage": "^2.0.2",
"bunyan": "^1.8.12",
"express": "^4.16.3",
"fluent-logger": "^3.0.0",
"winston": "^2.4.0",
"yargs": "^12.0.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"assert-rejects": "^1.0.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.2.0",
"nyc": "^13.0.0",
"proxyquire": "^2.0.1",
"sinon": "^7.0.0",
"supertest": "^3.1.0",
"proxyquire": "^2.1.0",
"supertest": "^3.3.0",
"uuid": "^3.3.0"
}
}
25 changes: 12 additions & 13 deletions samples/quickstart.js
Expand Up @@ -14,38 +14,37 @@
*/

'use strict';
async function main() {
// [START logging_quickstart]

// [START logging_quickstart]
async function quickstart(
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
logName = 'my-log' // The name of the log to write to
) {
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Creates a client
const logging = new Logging({
projectId: projectId,
});
const logging = new Logging({projectId});

// The name of the log to write to
const logName = 'my-log';
// Selects the log to write to
const log = logging.log(logName);

// The data to write to the log
const text = 'Hello, world!';

// The metadata associated with the entry
const metadata = {
resource: {type: 'global'},
};

// Prepares a log entry
const entry = log.entry(metadata, text);

// Writes the log entry
await log.write(entry);
console.log(`Logged: ${text}`);

// [END logging_quickstart]
}
// [END logging_quickstart]

main().catch(console.error);
const args = process.argv.slice(2);
quickstart(...args).catch(console.error);
4 changes: 0 additions & 4 deletions samples/system-test/.eslintrc.yml
@@ -1,7 +1,3 @@
---
env:
mocha: true
rules:
node/no-unpublished-require: off
node/no-unsupported-features: off
no-empty: off
55 changes: 55 additions & 0 deletions samples/system-test/fluent.test.js
@@ -0,0 +1,55 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const proxyquire = require('proxyquire');
const request = require('supertest');
const {assert} = require('chai');

describe('fluent', () => {
it('should log error', done => {
let loggerCalled = false;

const structuredLogger = {
emit: name => {
loggerCalled = true;
assert.strictEqual(name, 'errors');
},
};

const app = proxyquire('../fluent', {
'fluent-logger': {
createFluentSender: (name, options) => {
assert.strictEqual(name, 'myapp');
assert.deepStrictEqual(options, {
host: 'localhost',
port: 24224,
timeout: 3.0,
});
return structuredLogger;
},
},
});

request(app)
.get('/')
.expect(500)
.expect(() => {
assert(loggerCalled, 'structuredLogger.emit should have been called');
})
.end(done);
});
});
39 changes: 17 additions & 22 deletions samples/system-test/logs.test.js
Expand Up @@ -15,30 +15,25 @@

'use strict';

const path = require(`path`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const uuid = require(`uuid`);

const cwd = path.join(__dirname, `..`);
const cmd = `node logs.js`;
const {assert} = require('chai');
const execa = require('execa');
const uuid = require('uuid');

const cmd = 'node logs';
const exec = async cmd => (await execa.shell(cmd)).stdout;
const logName = `nodejs-logging-logs-test-${uuid.v4()}`;
const message = `Hello world!`;

before(async () => {
await tools.checkCredentials();
});
const message = 'Hello world!';

it(`should write a log entry`, async () => {
const output = await tools.runAsync(
`${cmd} write ${logName} '{"type":"global"}' '{"message":"${message}"}'`,
cwd
);
assert.strictEqual(output, `Wrote to ${logName}`);
});
describe('logs', () => {
it('should write a log entry', async () => {
const output = await exec(
`${cmd} write ${logName} '{"type":"global"}' '{"message":"${message}"}'`
);
assert.strictEqual(output, `Wrote to ${logName}`);
});

it(`should write a simple log entry`, async () => {
const output = await tools.runAsync(`${cmd} write-simple ${logName}`, cwd);
assert.strictEqual(output, `Wrote to ${logName}`);
it('should write a simple log entry', async () => {
const output = await exec(`${cmd} write-simple ${logName}`);
assert.strictEqual(output, `Wrote to ${logName}`);
});
});
72 changes: 17 additions & 55 deletions samples/system-test/quickstart.test.js
Expand Up @@ -15,64 +15,26 @@

'use strict';

const proxyquire = require(`proxyquire`).noPreserveCache();
const sinon = require(`sinon`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const uuid = require(`uuid`);
const {assert} = require('chai');
const uuid = require('uuid');
const {Logging} = require('@google-cloud/logging');
const execa = require('execa');

const {Logging} = proxyquire(`@google-cloud/logging`, {});
const logging = new Logging();

const logName = `nodejs-docs-samples-test-${uuid.v4()}`;
const projectId = process.env.GCLOUD_PROJECT;
const cmd = 'node quickstart';

describe('quickstart', () => {
after(async () => {
await logging
.log(logName)
.delete()
.catch(console.warn);
});

after(async () => {
try {
await logging.log(logName).delete();
} catch (err) {} // ignore error
});

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

it(`should log an entry`, done => {
const expectedlogName = `my-log`;

const logMock = {
entry: sinon.stub().returns({}),
write: _entry => {
assert.deepStrictEqual(_entry, {});

const log = logging.log(logName);
const text = `Hello, world!`;
const entry = log.entry({resource: {type: `global`}}, text);

return log.write(entry).then(results => {
setTimeout(() => {
try {
assert(console.log.calledOnce);
assert.deepStrictEqual(console.log.firstCall.args, [
`Logged: ${text}`,
]);
done();
} catch (err) {
done(err);
}
}, 200);

return results;
});
},
};
const loggingMock = {
log: _logName => {
assert.strictEqual(_logName, expectedlogName);
return logMock;
},
};
proxyquire(`../quickstart`, {
'@google-cloud/logging': {
Logging: sinon.stub().returns(loggingMock),
},
it('should log an entry', async () => {
const {stdout} = await execa.shell(`${cmd} ${projectId} ${logName}`);
assert.match(stdout, /Logged: Hello, world!/);
});
});

0 comments on commit 69dbf0e

Please sign in to comment.