Skip to content

Commit

Permalink
fix: refactor stubConsole and restoreConsole methods in repo-tools wi…
Browse files Browse the repository at this point in the history
…th sinon dependency (#1600)
  • Loading branch information
sofisl authored Feb 4, 2020
1 parent 9631b11 commit d091c8f
Show file tree
Hide file tree
Showing 28 changed files with 383 additions and 84 deletions.
3 changes: 2 additions & 1 deletion appengine/building-an-app/update/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.3.0",
"mocha": "^7.0.0"
"mocha": "^7.0.0",
"sinon": "^8.0.0"
}
}
23 changes: 21 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,34 @@
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` &&
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
24 changes: 21 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,27 @@ 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
4 changes: 0 additions & 4 deletions appengine/cloudsql/test/server.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, '../server.js');

Expand Down Expand Up @@ -73,9 +72,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
23 changes: 20 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,26 @@ 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
4 changes: 0 additions & 4 deletions appengine/cloudsql_postgresql/test/server.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, '../server.js');

Expand Down Expand Up @@ -73,9 +72,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
13 changes: 10 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,16 @@ 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
14 changes: 11 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,17 @@ 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
23 changes: 20 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,26 @@ 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
6 changes: 1 addition & 5 deletions functions/helloworld/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,8 @@ 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
24 changes: 22 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,31 @@ 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
24 changes: 22 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,31 @@ 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
13 changes: 10 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,16 @@ 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
14 changes: 11 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,16 @@ 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

0 comments on commit d091c8f

Please sign in to comment.