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

refactor: use execSync for tests #445

Merged
merged 1 commit into from Apr 9, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -94,6 +94,7 @@
"@types/uuid": "^3.4.4",
"assert-rejects": "^1.0.0",
"bignumber.js": "^8.0.0",
"chai": "^4.2.0",
"codecov": "^3.0.4",
"eslint": "^5.4.0",
"eslint-config-prettier": "^4.0.0",
Expand Down
6 changes: 2 additions & 4 deletions samples/package.json
Expand Up @@ -8,10 +8,10 @@
"*.js"
],
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "mocha system-test --timeout 60000"
"test": "mocha --timeout 60000"
},
"dependencies": {
"@google-cloud/logging": "^4.5.1",
Expand All @@ -21,9 +21,7 @@
"yargs": "^13.0.0"
},
"devDependencies": {
"assert-rejects": "^1.0.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0",
"proxyquire": "^2.1.0",
"supertest": "^4.0.0",
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions samples/system-test/logs.test.js → samples/test/logs.test.js
Expand Up @@ -16,24 +16,24 @@
'use strict';

const {assert} = require('chai');
const execa = require('execa');
const cp = require('child_process');
const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
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!';

describe('logs', () => {
it('should write a log entry', async () => {
const output = await exec(
it('should write a log entry', () => {
const output = execSync(
`${cmd} write ${logName} '{"type":"global"}' '{"message":"${message}"}'`
);
assert.strictEqual(output, `Wrote to ${logName}`);
assert.include(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}`);
it('should write a simple log entry', () => {
const output = execSync(`${cmd} write-simple ${logName}`);
assert.include(output, `Wrote to ${logName}`);
});
});
Expand Up @@ -18,7 +18,9 @@
const {assert} = require('chai');
const uuid = require('uuid');
const {Logging} = require('@google-cloud/logging');
const execa = require('execa');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const logging = new Logging();
const logName = `nodejs-docs-samples-test-${uuid.v4()}`;
Expand All @@ -33,8 +35,8 @@ describe('quickstart', () => {
.catch(console.warn);
});

it('should log an entry', async () => {
const {stdout} = await execa.shell(`${cmd} ${projectId} ${logName}`);
assert.match(stdout, /Logged: Hello, world!/);
it('should log an entry', () => {
const stdout = execSync(`${cmd} ${projectId} ${logName}`);
assert.include(stdout, 'Logged: Hello, world!');
});
});
47 changes: 24 additions & 23 deletions samples/system-test/sinks.test.js → samples/test/sinks.test.js
Expand Up @@ -18,11 +18,12 @@
const {Logging} = new require('@google-cloud/logging');
const {Storage} = new require('@google-cloud/storage');
const {assert} = require('chai');
const execa = require('execa');
assert.rejects = require('assert').rejects;
const cp = require('child_process');
const uuid = require(`uuid`);
const assertRejects = require('assert-rejects');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const cmd = 'node sinks.js';
const bucketName = `nodejs-logging-sinks-test-${uuid.v4()}`;
const sinkName = `nodejs-logging-sinks-test-${uuid.v4()}`;
Expand All @@ -47,40 +48,40 @@ describe('sinks', () => {
});

it(`should create a sink`, async () => {
const output = await exec(
const output = execSync(
`${cmd} create ${sinkName} ${bucketName} "${filter}"`
);
assert.strictEqual(output, `Created sink ${sinkName} to ${bucketName}`);
assert.include(output, `Created sink ${sinkName} to ${bucketName}`);
const [metadata] = await logging.sink(sinkName).getMetadata();
assert.strictEqual(metadata.name, sinkName);
assert.match(metadata.destination, new RegExp(bucketName));
assert.strictEqual(metadata.filter, filter);
assert.include(metadata.name, sinkName);
assert.include(metadata.destination, bucketName);
assert.include(metadata.filter, filter);
});

it(`should get a sink`, async () => {
const output = await exec(`${cmd} get ${sinkName}`);
assert.match(output, new RegExp(sinkName));
it(`should get a sink`, () => {
const output = execSync(`${cmd} get ${sinkName}`);
assert.include(output, sinkName);
});

it(`should list sinks`, async () => {
const output = await exec(`${cmd} list`);
assert.match(output, /Sinks:/);
assert.match(output, new RegExp(sinkName));
it(`should list sinks`, () => {
const output = execSync(`${cmd} list`);
assert.include(output, 'Sinks:');
assert.include(output, sinkName);
});

it(`should update a sink`, async () => {
const newFilter = 'severity >= WARNING';
const output = await exec(`${cmd} update ${sinkName} "${newFilter}"`);
assert.match(output, new RegExp(`Sink ${sinkName} updated.`));
const output = execSync(`${cmd} update ${sinkName} "${newFilter}"`);
assert.include(output, `Sink ${sinkName} updated.`);
const [metadata] = await logging.sink(sinkName).getMetadata();
assert.strictEqual(metadata.name, sinkName);
assert.match(metadata.destination, new RegExp(bucketName));
assert.strictEqual(metadata.filter, newFilter);
assert.include(metadata.name, sinkName);
assert.include(metadata.destination, bucketName);
assert.include(metadata.filter, newFilter);
});

it(`should delete a sink`, async () => {
const output = await exec(`${cmd} delete ${sinkName}`);
assert.strictEqual(output, `Sink ${sinkName} deleted.`);
await assertRejects(logging.sink(sinkName).getMetadata());
const output = execSync(`${cmd} delete ${sinkName}`);
assert.include(output, `Sink ${sinkName} deleted.`);
await assert.rejects(logging.sink(sinkName).getMetadata());
});
});
1 change: 0 additions & 1 deletion test/metadata.ts
Expand Up @@ -21,7 +21,6 @@ import {GCPEnv} from 'google-auth-library';
import * as proxyquire from 'proxyquire';

import assertRejects = require('assert-rejects');
import {detectServiceContext} from '../src/metadata';

let instanceOverride;
const fakeGcpMetadata = {
Expand Down