From a9010d871a523406eb2dabf0fefd73a0ce89628d Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Wed, 26 Dec 2018 15:32:49 -0600 Subject: [PATCH] refactor: modernize the sample tests (#164) --- texttospeech/.eslintrc.yml | 1 + texttospeech/package.json | 9 +- texttospeech/system-test/audioProfile.test.js | 85 ----------------- texttospeech/system-test/synthesize.test.js | 85 ----------------- .../{system-test => test}/.eslintrc.yml | 0 texttospeech/test/audioProfile.test.js | 93 ++++++++++++++++++ .../{system-test => test}/listVoices.test.js | 20 ++-- .../{system-test => test}/quickstart.test.js | 37 ++++---- texttospeech/test/synthesize.test.js | 95 +++++++++++++++++++ 9 files changed, 219 insertions(+), 206 deletions(-) delete mode 100644 texttospeech/system-test/audioProfile.test.js delete mode 100644 texttospeech/system-test/synthesize.test.js rename texttospeech/{system-test => test}/.eslintrc.yml (100%) create mode 100644 texttospeech/test/audioProfile.test.js rename texttospeech/{system-test => test}/listVoices.test.js (59%) rename texttospeech/{system-test => test}/quickstart.test.js (50%) create mode 100644 texttospeech/test/synthesize.test.js diff --git a/texttospeech/.eslintrc.yml b/texttospeech/.eslintrc.yml index 282535f55f..0aa37ac630 100644 --- a/texttospeech/.eslintrc.yml +++ b/texttospeech/.eslintrc.yml @@ -1,3 +1,4 @@ --- rules: no-console: off + node/no-missing-require: off diff --git a/texttospeech/package.json b/texttospeech/package.json index 3315760a88..2304ad8cce 100644 --- a/texttospeech/package.json +++ b/texttospeech/package.json @@ -1,22 +1,25 @@ { "name": "nodejs-docs-samples-text-to-speech", - "version": "0.0.1", "private": true, "license": "Apache-2.0", "author": "Google Inc.", "repository": "googleapis/nodejs-text-to-speech", + "files": [ + "*.js" + ], "engines": { "node": ">=8" }, "scripts": { - "test": "mocha system-test/*.test.js --timeout=600000" + "test": "mocha --timeout=60000" }, "dependencies": { "@google-cloud/text-to-speech": "^0.4.0", "yargs": "^12.0.0" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", + "chai": "^4.2.0", + "execa": "^1.0.0", "mocha": "^5.2.0" }, "optionalDependencies": { diff --git a/texttospeech/system-test/audioProfile.test.js b/texttospeech/system-test/audioProfile.test.js deleted file mode 100644 index c9db7a1e43..0000000000 --- a/texttospeech/system-test/audioProfile.test.js +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright 2018, 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. - */ - -/* eslint-disable */ - -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); - -const cmd = 'node audioProfile.js'; -const cwd = path.join(__dirname, '..'); -const text = 'Hello Everybody! This is an Audio Profile Optimized Sound Byte.'; -const outputFile1 = 'phonetest.mp3'; -const outputFile2 = 'homeTheatreTest.mp3'; -const outputFile3 = 'carAudioTest.mp3'; -const outputFile4 = 'watchAudioTest.mp3'; - -before(tools.checkCredentials); - -after(() => { - function unlink(outputFile) { - try { - fs.unlinkSync(outputFile); - } catch(err) { - // Ignore error - } - } - - [outputFile1, outputFile2, outputFile3, outputFile4].map(unlink); -}); - -it('Should synthesize Speech for Telephone Audio Profile', async () => { - assert.strictEqual(fs.existsSync(outputFile1), false); - const output = await tools.runAsync( - `${cmd} synthesize '${text}' -f ${outputFile1} -e telephony-class-application`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile1}`)); - assert.ok(fs.existsSync(outputFile1)); -}); - -it('Should synthesize Speech for Home Theatre Audio Profile', async () => { - assert.strictEqual(fs.existsSync(outputFile2), false); - const output = await tools.runAsync( - `${cmd} synthesize '${text}' -f ${outputFile2} -e large-home-entertainment-class-device`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile2}`)); - assert.ok(fs.existsSync(outputFile2)); -}); - -it('Should synthesize Speech for Car Audio Audio Profile', async () => { - assert.strictEqual(fs.existsSync(outputFile3), false); - const output = await tools.runAsync( - `${cmd} synthesize '${text}' -f ${outputFile3} -e large-automotive-class-device`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile3}`)); - assert.ok(fs.existsSync(outputFile3)); -}); - -it('should synthesize Speech for Watch Audio Profile', async () => { - assert.strictEqual(fs.existsSync(outputFile4), false); - const output = await tools.runAsync( - `${cmd} synthesize '${text}' -f ${outputFile4} -e wearable-class-device`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile4}`)); - assert.ok(fs.existsSync(outputFile4)); -}); diff --git a/texttospeech/system-test/synthesize.test.js b/texttospeech/system-test/synthesize.test.js deleted file mode 100644 index d793632d03..0000000000 --- a/texttospeech/system-test/synthesize.test.js +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright 2018, 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. - */ - -/* eslint-disable */ - -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); - -const cmd = 'node synthesize.js'; -const cwd = path.join(__dirname, '..'); -const text = 'Hello there.'; -const ssml = 'Hello there.'; -const outputFile = 'test-output.mp3'; -const files = ['hello.txt', 'hello.ssml'].map(name => { - return { - name, - localPath: path.resolve(path.join(__dirname, `../resources/${name}`)), - }; -}); - -before(tools.checkCredentials); - -afterEach(() => { - try { - fs.unlinkSync(outputFile); - } catch(err) { - // Ignore error - } -}); - -it('should synthesize audio from text', async () => { - assert.strictEqual(fs.existsSync(outputFile), false); - const output = await tools.runAsync( - `${cmd} text '${text}' --outputFile ${outputFile}`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile}`)); - assert.ok(fs.existsSync(outputFile)); -}); - -it('should synthesize audio from ssml', async () => { - assert.strictEqual(fs.existsSync(outputFile), false); - const output = await tools.runAsync( - `${cmd} ssml "${ssml}" --outputFile ${outputFile}`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile}`)); - assert.ok(fs.existsSync(outputFile)); -}); - -it('should synthesize audio from text file', async () => { - assert.strictEqual(fs.existsSync(outputFile), false); - const output = await tools.runAsync( - `${cmd} text-file ${files[0].localPath} --outputFile ${outputFile}`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile}`)); - assert.ok(fs.existsSync(outputFile)); -}); - -it('should synthesize audio from ssml file', async () => { - assert.strictEqual(fs.existsSync(outputFile), false); - const output = await tools.runAsync( - `${cmd} ssml-file ${files[1].localPath} --outputFile ${outputFile}`, - cwd - ); - assert.ok(output.includes(`Audio content written to file: ${outputFile}`)); - assert.ok(fs.existsSync(outputFile)); -}); diff --git a/texttospeech/system-test/.eslintrc.yml b/texttospeech/test/.eslintrc.yml similarity index 100% rename from texttospeech/system-test/.eslintrc.yml rename to texttospeech/test/.eslintrc.yml diff --git a/texttospeech/test/audioProfile.test.js b/texttospeech/test/audioProfile.test.js new file mode 100644 index 0000000000..e96bf8e061 --- /dev/null +++ b/texttospeech/test/audioProfile.test.js @@ -0,0 +1,93 @@ +/** + * Copyright 2018, 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 fs = require('fs'); +const {assert} = require('chai'); +const execa = require('execa'); + +const cmd = 'node audioProfile.js'; +const exec = async cmd => { + const res = await execa.shell(cmd); + assert.isEmpty(res.stderr); + return res.stdout; +}; +const text = 'Hello Everybody! This is an Audio Profile Optimized Sound Byte.'; +const outputFile1 = 'phonetest.mp3'; +const outputFile2 = 'homeTheatreTest.mp3'; +const outputFile3 = 'carAudioTest.mp3'; +const outputFile4 = 'watchAudioTest.mp3'; + +describe('audio profile', () => { + after(() => { + function unlink(outputFile) { + try { + fs.unlinkSync(outputFile); + } catch (err) { + // Ignore error + } + } + [outputFile1, outputFile2, outputFile3, outputFile4].map(unlink); + }); + + it('should synthesize Speech for Telephone Audio Profile', async () => { + assert.strictEqual(fs.existsSync(outputFile1), false); + const output = await exec( + `${cmd} synthesize '${text}' -f ${outputFile1} -e telephony-class-application` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile1}`) + ); + assert.ok(fs.existsSync(outputFile1)); + }); + + it('should synthesize Speech for Home Theatre Audio Profile', async () => { + assert.strictEqual(fs.existsSync(outputFile2), false); + const output = await exec( + `${cmd} synthesize '${text}' -f ${outputFile2} -e large-home-entertainment-class-device` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile2}`) + ); + assert.ok(fs.existsSync(outputFile2)); + }); + + it('should synthesize Speech for Car Audio Audio Profile', async () => { + assert.strictEqual(fs.existsSync(outputFile3), false); + const output = await exec( + `${cmd} synthesize '${text}' -f ${outputFile3} -e large-automotive-class-device` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile3}`) + ); + assert.ok(fs.existsSync(outputFile3)); + }); + + it('should synthesize Speech for Watch Audio Profile', async () => { + assert.strictEqual(fs.existsSync(outputFile4), false); + const output = await exec( + `${cmd} synthesize '${text}' -f ${outputFile4} -e wearable-class-device` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile4}`) + ); + assert.ok(fs.existsSync(outputFile4)); + }); +}); diff --git a/texttospeech/system-test/listVoices.test.js b/texttospeech/test/listVoices.test.js similarity index 59% rename from texttospeech/system-test/listVoices.test.js rename to texttospeech/test/listVoices.test.js index b13d045752..80b46da6b1 100644 --- a/texttospeech/system-test/listVoices.test.js +++ b/texttospeech/test/listVoices.test.js @@ -13,21 +13,17 @@ * limitations under the License. */ -/* eslint-disable */ - 'use strict'; -const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); +const {assert} = require('chai'); +const execa = require('execa'); const cmd = 'node listVoices.js'; -const cwd = path.join(__dirname, '..'); - -before(tools.checkCredentials); -it('should list voices', async () => { - const output = await tools.runAsync(`${cmd} list-voices`, cwd); - assert.ok(output.includes('SSML Voice Gender: FEMALE')); - assert.ok(output.includes('Natural Sample Rate Hertz: 24000')); +describe('list voices', () => { + it('should list voices', async () => { + const {stdout} = await execa.shell(`${cmd} list-voices`); + assert.match(stdout, /SSML Voice Gender: FEMALE/); + assert.match(stdout, /Natural Sample Rate Hertz: 24000/); + }); }); diff --git a/texttospeech/system-test/quickstart.test.js b/texttospeech/test/quickstart.test.js similarity index 50% rename from texttospeech/system-test/quickstart.test.js rename to texttospeech/test/quickstart.test.js index 8f5a0d8859..09bf7c22ac 100644 --- a/texttospeech/system-test/quickstart.test.js +++ b/texttospeech/test/quickstart.test.js @@ -13,32 +13,27 @@ * limitations under the License. */ -/* eslint-disable */ - 'use strict'; const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); +const {assert} = require('chai'); +const execa = require('execa'); const outputFile = 'output.mp3'; -const cmd = 'node quickstart.js'; -const cwd = path.join(__dirname, '..'); -before(tools.stubConsole); -after(() => { - tools.restoreConsole(); - try { - fs.unlinkSync(outputFile); - } catch(err) { - // Ignore error - } -}); +describe('quickstart', () => { + after(() => { + try { + fs.unlinkSync(outputFile); + } catch (err) { + // Ignore error + } + }); -it('should synthesize speech to local mp3 file', async () => { - assert.strictEqual(fs.existsSync(outputFile), false); - const output = await tools.runAsync(`${cmd}`, cwd); - assert.ok(output.includes('Audio content written to file: output.mp3')); - assert.ok(fs.existsSync(outputFile)); + it('should synthesize speech to local mp3 file', async () => { + assert.strictEqual(fs.existsSync(outputFile), false); + const {stdout} = await execa.shell('node quickstart'); + assert.match(stdout, /Audio content written to file: output.mp3/); + assert.ok(fs.existsSync(outputFile)); + }); }); diff --git a/texttospeech/test/synthesize.test.js b/texttospeech/test/synthesize.test.js new file mode 100644 index 0000000000..34e2c9538f --- /dev/null +++ b/texttospeech/test/synthesize.test.js @@ -0,0 +1,95 @@ +/** + * Copyright 2018, 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 fs = require('fs'); +const path = require('path'); +const {assert} = require('chai'); +const execa = require('execa'); + +const exec = async cmd => { + const res = await execa.shell(cmd); + assert.isEmpty(res.stderr); + return res.stdout; +}; +const cmd = 'node synthesize.js'; +const text = 'Hello there.'; +const ssml = 'Hello there.'; +const outputFile = 'test-output.mp3'; +const files = ['hello.txt', 'hello.ssml'].map(name => { + return { + name, + localPath: path.resolve(path.join(__dirname, `../resources/${name}`)), + }; +}); + +describe('synthesize', () => { + afterEach(() => { + try { + fs.unlinkSync(outputFile); + } catch (err) { + // Ignore error + } + }); + + it('should synthesize audio from text', async () => { + assert.strictEqual(fs.existsSync(outputFile), false); + const output = await exec( + `${cmd} text '${text}' --outputFile ${outputFile}` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile}`) + ); + assert.ok(fs.existsSync(outputFile)); + }); + + it('should synthesize audio from ssml', async () => { + assert.strictEqual(fs.existsSync(outputFile), false); + const output = await exec( + `${cmd} ssml "${ssml}" --outputFile ${outputFile}` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile}`) + ); + assert.ok(fs.existsSync(outputFile)); + }); + + it('should synthesize audio from text file', async () => { + assert.strictEqual(fs.existsSync(outputFile), false); + const output = await exec( + `${cmd} text-file ${files[0].localPath} --outputFile ${outputFile}` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile}`) + ); + assert.ok(fs.existsSync(outputFile)); + }); + + it('should synthesize audio from ssml file', async () => { + assert.strictEqual(fs.existsSync(outputFile), false); + const output = await exec( + `${cmd} ssml-file ${files[1].localPath} --outputFile ${outputFile}` + ); + assert.match( + output, + new RegExp(`Audio content written to file: ${outputFile}`) + ); + assert.ok(fs.existsSync(outputFile)); + }); +});