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

fix: refactor stubConsole and restoreConsole methods in repo-tools with sinon dependency #1600

Merged
merged 7 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 22 additions & 2 deletions appengine/building-an-app/update/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,35 @@
const path = require('path');
const assert = require('assert');
const utils = require('@google-cloud/nodejs-repo-tools');
const sinon = require('sinon');

const cwd = path.join(__dirname, '../');
const requestObj = utils.getRequest({
cwd: cwd,
cmd: 'server',
});

beforeEach(utils.stubConsole);
afterEach(utils.restoreConsole);
const stubConsole = function () {
sinon.stub(console, `error`);
sinon.stub(console, `log`).callsFake((a, b) => {
if (
typeof a === `string` &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the tests pass without this if clause?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not (at least for this very specific one). I tried with other ones and they also failed, but definitely interested in making it simpler!

a.indexOf(`\u001b`) !== -1 &&
typeof b === `string`
) {
console.log.apply(console, arguments);
}
});
};


const restoreConsole = function() {
console.log.restore();
console.error.restore();

};
beforeEach(stubConsole);
afterEach(restoreConsole);

it('should send greetings', async () => {
await requestObj
Expand Down
29 changes: 26 additions & 3 deletions appengine/cloudsql/test/createTables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const assert = require('assert');
const path = require('path');
const proxyquire = require('proxyquire').noPreserveCache();
const sinon = require('sinon');
const tools = require('@google-cloud/nodejs-repo-tools');

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

Expand Down Expand Up @@ -57,8 +56,32 @@ const getSample = () => {
};
};

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

const stubConsole = function () {
/* eslint-disable no-console */
sinon.stub(console, `error`);
sinon.stub(console, `log`).callsFake((a, b) => {
if (
typeof a === `string` &&
a.indexOf(`\u001b`) !== -1 &&
typeof b === `string`
) {
console.log.apply(console, arguments);
}
});
};


const restoreConsole = function() {
console.log.restore();
console.error.restore();
}



beforeEach(stubConsole);
afterEach(restoreConsole);


describe('gae_flex_mysql_create_tables', () => {
it('should create a table', async () => {
Expand Down
3 changes: 0 additions & 3 deletions appengine/cloudsql/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ const getSample = () => {
};
};

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

describe('gae_flex_mysql_connect', () => {
it('should set up sample in Postgres', () => {
const sample = getSample();
Expand Down
27 changes: 24 additions & 3 deletions appengine/cloudsql_postgresql/test/createTables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const assert = require('assert');
const path = require('path');
const proxyquire = require('proxyquire').noPreserveCache();
const sinon = require('sinon');
const tools = require('@google-cloud/nodejs-repo-tools');

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

Expand Down Expand Up @@ -57,8 +56,30 @@ const getSample = () => {
};
};

beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);
const stubConsole = function () {
sinon.stub(console, `error`);
sinon.stub(console, `log`).callsFake((a, b) => {
if (
typeof a === `string` &&
a.indexOf(`\u001b`) !== -1 &&
typeof b === `string`
) {
console.log.apply(console, arguments);
}
});
};


const restoreConsole = function() {
console.log.restore();
console.error.restore();
}



beforeEach(stubConsole);
afterEach(restoreConsole);


describe('gae_flex_postgres_create_tables', () => {
it('should create a table', async () => {
Expand Down
3 changes: 0 additions & 3 deletions appengine/cloudsql_postgresql/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ const getSample = () => {
};
};

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

it('gae_flex_postgres_connect should set up sample in Postgres', () => {
const sample = getSample();

Expand Down
15 changes: 12 additions & 3 deletions appengine/endpoints/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const proxyquire = require('proxyquire').noCallThru();
const request = require('supertest');
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');

const SAMPLE_PATH = path.join(__dirname, '../app.js');

Expand All @@ -39,8 +38,18 @@ const getSample = () => {
};
};

beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);
const stubConsole = function () {
sinon.stub(console, `error`);
}


const restoreConsole = function() {
console.error.restore();
}


beforeEach(stubConsole);
afterEach(restoreConsole);

it(`sets up the sample`, done => {
const sample = getSample();
Expand Down
17 changes: 14 additions & 3 deletions endpoints/getting-started/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const proxyquire = require('proxyquire').noPreserveCache();
const request = require('supertest');
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');

const SAMPLE_PATH = path.join(__dirname, '../app.js');

Expand All @@ -40,8 +39,20 @@ const getSample = () => {
};
};

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

const stubConsole = function () {
sinon.stub(console, `error`);
};


//Restore console
const restoreConsole = function() {
console.error.restore();
}


beforeEach(stubConsole);
afterEach(restoreConsole);

it('should echo a message', async () => {
await request(getSample().app)
Expand Down
26 changes: 23 additions & 3 deletions functions/firebase/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');

const getSample = () => {
const firestoreMock = {
Expand All @@ -35,8 +34,29 @@ const getSample = () => {
};
};

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

const stubConsole = function () {
sinon.stub(console, `error`);
sinon.stub(console, `log`).callsFake((a, b) => {
if (
typeof a === `string` &&
a.indexOf(`\u001b`) !== -1 &&
typeof b === `string`
) {
console.log.apply(console, arguments);
}
});
}


const restoreConsole = function() {
console.log.restore();
console.error.restore();
}


beforeEach(stubConsole);
afterEach(restoreConsole);

describe('functions_firebase_rtdb', () => {
it('should listen to RTDB', () => {
Expand Down
9 changes: 4 additions & 5 deletions functions/helloworld/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,13 @@ describe('index.test.js', () => {
});
});



describe('functions_helloworld_error', () => {
describe('Error handling (unit tests)', () => {
// Silence dummy console calls in the samples
before(tools.stubConsole);
after(tools.restoreConsole);


it('helloError: should throw an error', () => {
assert.throws(program.helloError, 'I failed you');
assert.throws(program.helloError, 'Error: I failed you');
});

it('helloError2: should throw a value', () => {
Expand Down
26 changes: 24 additions & 2 deletions functions/helloworld/test/sample.unit.pubsub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,33 @@ describe('functions_helloworld_pubsub', () => {
const assert = require('assert');
const uuid = require('uuid');
const utils = require('@google-cloud/nodejs-repo-tools');
const sinon = require('sinon');

const {helloPubSub} = require('..');

beforeEach(utils.stubConsole);
afterEach(utils.restoreConsole);
const stubConsole = function () {
sinon.stub(console, `error`);
sinon.stub(console, `log`).callsFake((a, b) => {
if (
typeof a === `string` &&
a.indexOf(`\u001b`) !== -1 &&
typeof b === `string`
) {
console.log('e');
console.log.apply(console, arguments);
}
});

};


const restoreConsole = function() {
console.log.restore();
console.error.restore();
}

beforeEach(stubConsole);
afterEach(restoreConsole);

it('helloPubSub: should print a name', () => {
// Create mock Pub/Sub event
Expand Down
26 changes: 24 additions & 2 deletions functions/helloworld/test/sample.unit.storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,33 @@ describe('functions_helloworld_storage', () => {
const assert = require('assert');
const uuid = require('uuid');
const utils = require('@google-cloud/nodejs-repo-tools');
const sinon = require('sinon');

const {helloGCS} = require('..');

beforeEach(utils.stubConsole);
afterEach(utils.restoreConsole);
const stubConsole = function () {
sinon.stub(console, `error`);
sinon.stub(console, `log`).callsFake((a, b) => {
if (
typeof a === `string` &&
a.indexOf(`\u001b`) !== -1 &&
typeof b === `string`
) {
console.log('e');
console.log.apply(console, arguments);
}
});

};


const restoreConsole = function() {
console.log.restore();
console.error.restore();
}

beforeEach(stubConsole);
afterEach(restoreConsole);

it('helloGCS: should print uploaded message', () => {
// Initialize mocks
Expand Down
16 changes: 13 additions & 3 deletions functions/http/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const assert = require('assert');
const uuid = require('uuid');
const tools = require('@google-cloud/nodejs-repo-tools');

const getSample = () => {
const requestPromise = sinon
Expand Down Expand Up @@ -66,8 +65,19 @@ const getMocks = () => {
};
};

beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);
const stubConsole = function () {

sinon.stub(console, `error`);
};


const restoreConsole = function() {
console.error.restore();
}

beforeEach(stubConsole);
afterEach(restoreConsole);


describe('functions_http_method', () => {
it('http:helloHttp: should handle GET', () => {
Expand Down
16 changes: 13 additions & 3 deletions functions/imagemagick/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
'use strict';

const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');
const execPromise = require('child-process-promise').exec;
const path = require('path');
const {Storage} = require('@google-cloud/storage');
const sinon = require('sinon');

const storage = new Storage();

Expand Down Expand Up @@ -83,8 +83,18 @@ describe('functions/imagemagick tests', () => {
};
});

beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);
const stubConsole = function () {
sinon.stub(console, `error`);
};


const restoreConsole = function() {
console.error.restore();
};

beforeEach(stubConsole);
afterEach(restoreConsole);


describe('functions_imagemagick_analyze', () => {
it('blurOffensiveImages detects safe images using Cloud Vision', async () => {
Expand Down
Loading