diff --git a/package.json b/package.json index ea2df57c56..6c22ba08e7 100644 --- a/package.json +++ b/package.json @@ -54,16 +54,18 @@ "scripts": { "lint": "semistandard", "pretest": "npm run lint && ./scripts/clean", - "mocha": "mocha -R spec -t 120000 --require intelli-espower-loader ./test/_setup.js ./test/*.test.js '{*,appengine/*,functions/*}/test/*.test.js'", - "test": "npm run mocha", + "ava": "ava -c 5 -T 2s speech/test", + "test": "npm run ava", "cover": "nyc --cache npm test && nyc report --reporter=html && nyc report --reporter=lcov", - "system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*}/system-test/*.test.js'", + "system-ava": "ava -c 5 -T 2m speech/system-test", + "system-test": "npm run pretest && npm run system-ava", "system-cover": "npm run pretest && nyc --cache npm run system-test && nyc report --reporter=html && nyc report --reporter=lcov", "all-test": "mocha -R spec -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*}/system-test/*.test.js' ./test/*.test.js '{*,appengine/*,functions/*}/test/*.test.js'", "all-cover": "npm run pretest && nyc --cache npm run all-test && nyc report --reporter=html && nyc report --reporter=lcov" }, "devDependencies": { "async": "^2.0.1", + "ava": "^0.16.0", "intelli-espower-loader": "^1.0.1", "mocha": "^3.0.2", "nodejs-repo-tools": "git+https://github.com/GoogleCloudPlatform/nodejs-repo-tools.git#bbbb6035d77671eb053dbe6b6f0e3ff983f79639", diff --git a/speech/package.json b/speech/package.json index 63b3f55254..fe19d3b063 100644 --- a/speech/package.json +++ b/speech/package.json @@ -5,17 +5,14 @@ "license": "Apache Version 2.0", "author": "Google Inc.", "scripts": { - "test": "mocha -R spec -t 10000 --require intelli-espower-loader ../test/_setup.js test/*.test.js", - "system-test": "mocha -R spec -t 10000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js" + "test": "node ../node_modules/ava/cli.js T -2s test", + "system-test": "node ../node_modules/ava/cli.js -T 5s system-test" }, "dependencies": { "@google-cloud/speech": "^0.4.0", "node-record-lpcm16": "^0.1.4", "yargs": "^6.4.0" }, - "devDependencies": { - "mocha": "^3.1.2" - }, "engines": { "node": ">=4.3.2" } diff --git a/speech/system-test/quickstart.test.js b/speech/system-test/quickstart.test.js index 1dbc4b5af1..688bcabecd 100644 --- a/speech/system-test/quickstart.test.js +++ b/speech/system-test/quickstart.test.js @@ -15,6 +15,8 @@ 'use strict'; +require(`../../test/_setup`); + const path = require(`path`); const proxyquire = require(`proxyquire`).noPreserveCache(); const speech = proxyquire(`@google-cloud/speech`, {})(); @@ -25,34 +27,31 @@ const config = { sampleRate: 16000 }; -describe(`speech:quickstart`, () => { - let speechMock, SpeechMock; - - it(`should detect speech`, (done) => { - const expectedFileName = `./resources/audio.raw`; - const expectedText = `how old is the Brooklyn Bridge`; - - speechMock = { - recognize: (_fileName, _config, _callback) => { - assert.equal(_fileName, expectedFileName); - assert.deepEqual(_config, config); - assert.equal(typeof _callback, `function`); - - speech.recognize(fileName, config, (err, transcription, apiResponse) => { - _callback(err, transcription, apiResponse); - assert.ifError(err); - assert.equal(transcription, expectedText); - assert.notEqual(apiResponse, undefined); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, [`Transcription: ${expectedText}`]); - done(); - }); - } - }; - SpeechMock = sinon.stub().returns(speechMock); - - proxyquire(`../quickstart`, { - '@google-cloud/speech': SpeechMock - }); +test.cb(`should detect speech`, (t) => { + stubConsole(); + const expectedFileName = `./resources/audio.raw`; + const expectedText = `how old is the Brooklyn Bridge`; + + const speechMock = { + recognize: (_fileName, _config, _callback) => { + t.is(_fileName, expectedFileName); + t.deepEqual(_config, config); + t.is(typeof _callback, `function`); + + speech.recognize(fileName, config, (err, transcription, apiResponse) => { + _callback(err, transcription, apiResponse); + t.ifError(err); + t.is(transcription, expectedText); + t.not(apiResponse, undefined); + // t.is(console.log.calledCount, 1); + // t.deepEqual(console.log.getCall(0).args, [`Transcription: ${expectedText}`]); + t.end(); + }); + } + }; + const SpeechMock = sinon.stub().returns(speechMock); + + proxyquire(`../quickstart`, { + '@google-cloud/speech': SpeechMock }); }); diff --git a/speech/system-test/recognize.test.js b/speech/system-test/recognize.test.js index 8249321d2c..4354188efe 100644 --- a/speech/system-test/recognize.test.js +++ b/speech/system-test/recognize.test.js @@ -15,24 +15,39 @@ 'use strict'; +require(`../../test/_setup`); + const path = require(`path`); -const run = require(`../../utils`).run; const cmd = `node recognize.js`; const cwd = path.join(__dirname, `..`); const filename = `./resources/audio.raw`; const text = `how old is the Brooklyn Bridge`; -describe(`speech:recognize`, () => { - it(`should run sync recognize`, () => { - assert.equal(run(`${cmd} sync ${filename}`, cwd), `Results: ${text}`); +test.cb(`should run sync recognize`, (t) => { + const expected = `Results: ${text}\n`; + + runAsync(`${cmd} sync ${filename}`, cwd, (err, stdout) => { + t.ifError(err); + t.is(stdout, expected); + t.end(); }); +}); - it(`should run async recognize`, () => { - assert.equal(run(`${cmd} async ${filename}`, cwd), `Results: ${text}`); +test.cb(`should run async recognize`, (t) => { + const expected = `Results: ${text}\n`; + + runAsync(`${cmd} async ${filename}`, cwd, (err, stdout) => { + t.ifError(err); + t.is(stdout, expected); + t.end(); }); +}); - it(`should run streaming recognize`, () => { - assert.notEqual(run(`${cmd} stream ${filename}`, cwd).indexOf(text), -1); +test.cb(`should run streaming recognize`, (t) => { + runAsync(`${cmd} stream ${filename}`, cwd, (err, stdout) => { + t.ifError(err); + t.is(stdout.includes(text), true); + t.end(); }); }); diff --git a/speech/test/quickstart.test.js b/speech/test/quickstart.test.js index 807e23e507..7d9917b8f7 100644 --- a/speech/test/quickstart.test.js +++ b/speech/test/quickstart.test.js @@ -15,35 +15,30 @@ 'use strict'; -const proxyquire = require(`proxyquire`).noCallThru(); +require(`../../test/_setup`); -const config = { - encoding: 'LINEAR16', - sampleRate: 16000 -}; +const proxyquire = require(`proxyquire`).noCallThru(); -describe(`speech:quickstart`, () => { - let speechMock, SpeechMock; +test(`should handle error`, (t) => { const error = new Error(`error`); const fileName = `./resources/audio.raw`; + const config = { + encoding: 'LINEAR16', + sampleRate: 16000 + }; + const speechMock = { + recognize: sinon.stub().yields(error) + }; + const SpeechMock = sinon.stub().returns(speechMock); - before(() => { - speechMock = { - recognize: sinon.stub().yields(error) - }; - SpeechMock = sinon.stub().returns(speechMock); + proxyquire(`../quickstart`, { + '@google-cloud/speech': SpeechMock }); - it(`should handle error`, () => { - proxyquire(`../quickstart`, { - '@google-cloud/speech': SpeechMock - }); - - assert.equal(SpeechMock.calledOnce, true); - assert.deepEqual(SpeechMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); - assert.equal(speechMock.recognize.calledOnce, true); - assert.deepEqual(speechMock.recognize.firstCall.args.slice(0, -1), [fileName, config]); - assert.equal(console.error.calledOnce, true); - assert.deepEqual(console.error.firstCall.args, [error]); - }); + t.is(SpeechMock.calledOnce, true); + t.deepEqual(SpeechMock.firstCall.args, [{ projectId: `YOUR_PROJECT_ID` }]); + t.is(speechMock.recognize.calledOnce, true); + t.deepEqual(speechMock.recognize.firstCall.args.slice(0, -1), [fileName, config]); + // t.is(console.error.calledOnce, true); + // t.deepEqual(console.error.firstCall.args, [error]); }); diff --git a/speech/test/recognize.test.js b/speech/test/recognize.test.js index 89747c0245..69d4582413 100644 --- a/speech/test/recognize.test.js +++ b/speech/test/recognize.test.js @@ -15,26 +15,27 @@ 'use strict'; +require(`../../test/_setup`); + const proxyquire = require(`proxyquire`).noCallThru(); -describe(`speech:recognize`, () => { - it(`should handle errors`, () => { - const filename = `audio.raw`; - const error = new Error(`error`); - const callback = sinon.spy(); - const speechMock = { - recognize: sinon.stub().yields(error), - startRecognition: sinon.stub().yields(error) - }; - const SpeechMock = sinon.stub().returns(speechMock); - const program = proxyquire(`../recognize`, { - '@google-cloud/speech': SpeechMock - }); +test(`should handle error`, (t) => { + const filename = `audio.raw`; + const error = new Error(`error`); + const callback = sinon.spy(); + const speechMock = { + recognize: sinon.stub().yields(error), + startRecognition: sinon.stub().yields(error) + }; + const SpeechMock = sinon.stub().returns(speechMock); + const program = proxyquire(`../recognize`, { + '@google-cloud/speech': SpeechMock + }); - program.syncRecognize(filename, callback); - program.asyncRecognize(filename, callback); + program.syncRecognize(filename, callback); + program.asyncRecognize(filename, callback); - assert.equal(callback.callCount, 2); - assert.equal(callback.alwaysCalledWithExactly(error), true); - }); + t.is(callback.callCount, 2); + t.deepEqual(callback.getCall(0).args, [error]); + t.deepEqual(callback.getCall(1).args, [error]); }); diff --git a/test/_setup.js b/test/_setup.js index 4cd3b2dd4b..2c04585eba 100644 --- a/test/_setup.js +++ b/test/_setup.js @@ -1,47 +1,48 @@ -// Copyright 2016, 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. +/** + * Copyright 2016, 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'; -var assert = require('power-assert').customize({ - output: { - maxDepth: 2 - } -}); -var sinon = require('sinon'); +const assert = require(`assert`); +const sinon = global.sinon = require(`sinon`); +const test = global.test = require(`ava`); +const child_process = require(`child_process`); -global.assert = assert; -global.sinon = sinon; +global.run = (cmd, cwd) => { + return child_process.execSync(cmd, { cwd: cwd }).toString().trim(); +}; -var log = console.log; +global.runAsync = (cmd, cwd, cb) => { + child_process.exec(cmd, { cwd: cwd }, cb); +}; -beforeEach(function () { +global.stubConsole = () => { if (process.env.DEBUG) { - sinon.spy(console, 'error'); - sinon.spy(console, 'log'); + sinon.spy(console, `error`); + sinon.spy(console, `log`); } else { - sinon.stub(console, 'error'); - sinon.stub(console, 'log', function (a, b, c) { - if (typeof a === 'string' && a.indexOf('\u001b') !== -1 && typeof b === 'string') { - log.apply(console, arguments); + sinon.stub(console, `error`); + sinon.stub(console, `log`, (a, b, c) => { + if (typeof a === `string` && a.indexOf(`\u001b`) !== -1 && typeof b === `string`) { + console.log.apply(console, arguments); } }); } - assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!'); - assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!'); -}); +}; -afterEach(function () { - console.error.restore(); - console.log.restore(); +test.beforeEach((t) => { + assert(process.env.GCLOUD_PROJECT, `Must set GCLOUD_PROJECT environment variable!`); + assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, `Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!`); });