From 2f0febd97f07581d2d75a1f886ece715d6fbceeb Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 4 Apr 2019 14:05:02 -0700 Subject: [PATCH] refactor: use execSync for tests refactor: use execSync for tests #225 automerged by dpebot --- cloud-language/snippets/package.json | 1 - .../snippets/test/analyze.v1.test.js | 28 ++++++--------- .../snippets/test/analyze.v1beta2.test.js | 25 ++++++------- .../test/automlNaturalLanguage.test.js | 36 +++++++++---------- .../snippets/test/quickstart.test.js | 4 +-- 5 files changed, 40 insertions(+), 54 deletions(-) diff --git a/cloud-language/snippets/package.json b/cloud-language/snippets/package.json index 2143f003af..50e114b409 100644 --- a/cloud-language/snippets/package.json +++ b/cloud-language/snippets/package.json @@ -23,7 +23,6 @@ }, "devDependencies": { "chai": "^4.2.0", - "execa": "^1.0.0", "mocha": "^6.0.0", "uuid": "^3.2.1" } diff --git a/cloud-language/snippets/test/analyze.v1.test.js b/cloud-language/snippets/test/analyze.v1.test.js index 95e3e2c28e..f88cfe9891 100644 --- a/cloud-language/snippets/test/analyze.v1.test.js +++ b/cloud-language/snippets/test/analyze.v1.test.js @@ -19,11 +19,9 @@ const fs = require('fs'); const path = require('path'); const {Storage} = require('@google-cloud/storage'); const {assert} = require('chai'); -const execa = require('execa'); +const {execSync} = require('child_process'); const uuid = require('uuid'); -const exec = async cmd => (await execa.shell(cmd)).stdout; - describe('analyze.v1', () => { const storage = new Storage(); const cmd = 'node analyze.v1.js'; @@ -49,7 +47,7 @@ describe('analyze.v1', () => { }); it('should analyze sentiment in text', async () => { - const output = await exec(`${cmd} sentiment-text "${text}"`); + const output = execSync(`${cmd} sentiment-text "${text}"`); assert.match(output, /Document sentiment:/); assert.match(output, new RegExp(`Sentence: ${text}`)); assert.match(output, /Score: 0/); @@ -57,9 +55,7 @@ describe('analyze.v1', () => { }); it('should analyze sentiment in a file', async () => { - const output = await exec( - `${cmd} sentiment-file ${bucketName} ${fileName}` - ); + const output = execSync(`${cmd} sentiment-file ${bucketName} ${fileName}`); assert(output, /Document sentiment:/); assert.match(output, new RegExp(`Sentence: ${text}`)); assert.match(output, /Score: 0/); @@ -67,7 +63,7 @@ describe('analyze.v1', () => { }); it('should analyze entities in text', async () => { - const output = await exec(`${cmd} entities-text "${text}"`); + const output = execSync(`${cmd} entities-text "${text}"`); assert.match(output, /Obama/); assert.match(output, /Type: PERSON/); assert.match(output, /White House/); @@ -75,7 +71,7 @@ describe('analyze.v1', () => { }); it('should analyze entities in a file', async () => { - const output = await exec(`${cmd} entities-file ${bucketName} ${fileName}`); + const output = execSync(`${cmd} entities-file ${bucketName} ${fileName}`); assert.match(output, /Entities:/); assert.match(output, /Obama/); assert.match(output, /Type: PERSON/); @@ -84,7 +80,7 @@ describe('analyze.v1', () => { }); it('should analyze syntax in text', async () => { - const output = await exec(`${cmd} syntax-text "${text}"`); + const output = execSync(`${cmd} syntax-text "${text}"`); assert.match(output, /Tokens:/); assert.match(output, /NOUN:/); assert.match(output, /President/); @@ -94,7 +90,7 @@ describe('analyze.v1', () => { }); it('should analyze syntax in a file', async () => { - const output = await exec(`${cmd} syntax-file ${bucketName} ${fileName}`); + const output = execSync(`${cmd} syntax-file ${bucketName} ${fileName}`); assert.match(output, /NOUN:/); assert.match(output, /President/); assert.match(output, /Obama/); @@ -103,7 +99,7 @@ describe('analyze.v1', () => { }); it('should analyze entity sentiment in text', async () => { - const output = await exec(`${cmd} entity-sentiment-text "${text}"`); + const output = execSync(`${cmd} entity-sentiment-text "${text}"`); assert.match(output, /Entities and sentiments:/); assert.match(output, /Obama/); assert.match(output, /PERSON/); @@ -112,7 +108,7 @@ describe('analyze.v1', () => { }); it('should analyze entity sentiment in a file', async () => { - const output = await exec( + const output = execSync( `${cmd} entity-sentiment-file ${bucketName} ${fileName}` ); assert.match(output, /Entities and sentiments:/); @@ -123,15 +119,13 @@ describe('analyze.v1', () => { }); it('should classify text in a file', async () => { - const output = await exec( - `${cmd} classify-file ${bucketName} ${fileName2}` - ); + const output = execSync(`${cmd} classify-file ${bucketName} ${fileName2}`); assert.match(output, /Name:/); assert.match(output, /Computers & Electronics/); }); it('should classify text in text', async () => { - const output = await exec(`${cmd} classify-text "${text2}"`); + const output = execSync(`${cmd} classify-text "${text2}"`); assert.match(output, /Name:/); assert.match(output, /Computers & Electronics/); }); diff --git a/cloud-language/snippets/test/analyze.v1beta2.test.js b/cloud-language/snippets/test/analyze.v1beta2.test.js index 9277805284..478f6b481a 100644 --- a/cloud-language/snippets/test/analyze.v1beta2.test.js +++ b/cloud-language/snippets/test/analyze.v1beta2.test.js @@ -19,10 +19,9 @@ const fs = require('fs'); const path = require('path'); const {Storage} = require('@google-cloud/storage'); const {assert} = require('chai'); -const execa = require('execa'); +const {execSync} = require('child_process'); const uuid = require('uuid'); -const exec = async cmd => (await execa.shell(cmd)).stdout; const storage = new Storage(); const cmd = 'node analyze.v1beta2.js'; const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; @@ -49,7 +48,7 @@ describe('analyze.v1beta2', () => { }); it(`should analyze sentiment in text`, async () => { - const output = await exec(`${cmd} sentiment-text "${text}"`); + const output = execSync(`${cmd} sentiment-text "${text}"`); assert.match(output, /Document sentiment:/); assert.match(output, new RegExp(`Sentence: ${text}`)); assert.match(output, /Score: 0/); @@ -57,9 +56,7 @@ describe('analyze.v1beta2', () => { }); it(`should analyze sentiment in a file`, async () => { - const output = await exec( - `${cmd} sentiment-file ${bucketName} ${fileName}` - ); + const output = execSync(`${cmd} sentiment-file ${bucketName} ${fileName}`); assert.match(output, /Document sentiment:/); assert.match(output, new RegExp(`Sentence: ${text}`)); assert.match(output, /Score: 0/); @@ -67,7 +64,7 @@ describe('analyze.v1beta2', () => { }); it(`should analyze entities in text`, async () => { - const output = await exec(`${cmd} entities-text "${text}"`); + const output = execSync(`${cmd} entities-text "${text}"`); assert.match(output, /Obama/); assert.match(output, /Type: PERSON/); assert.match(output, /White House/); @@ -75,7 +72,7 @@ describe('analyze.v1beta2', () => { }); it('should analyze entities in a file', async () => { - const output = await exec(`${cmd} entities-file ${bucketName} ${fileName}`); + const output = execSync(`${cmd} entities-file ${bucketName} ${fileName}`); assert.match(output, /Entities:/); assert.match(output, /Type: PERSON/); assert.match(output, /White House/); @@ -83,7 +80,7 @@ describe('analyze.v1beta2', () => { }); it(`should analyze syntax in text`, async () => { - const output = await exec(`${cmd} syntax-text "${text}"`); + const output = execSync(`${cmd} syntax-text "${text}"`); assert.match(output, /Parts of speech:/); assert.match(output, /NOUN:/); assert.match(output, /President/); @@ -93,7 +90,7 @@ describe('analyze.v1beta2', () => { }); it('should analyze syntax in a file', async () => { - const output = await exec(`${cmd} syntax-file ${bucketName} ${fileName}`); + const output = execSync(`${cmd} syntax-file ${bucketName} ${fileName}`); assert.match(output, /NOUN:/); assert.match(output, /President/); assert.match(output, /Obama/); @@ -102,7 +99,7 @@ describe('analyze.v1beta2', () => { }); it('should analyze syntax in a 1.1 language (German)', async () => { - const output = await exec(`${cmd} syntax-text "${germanText}"`); + const output = execSync(`${cmd} syntax-text "${germanText}"`); assert.match(output, /Parts of speech:/); assert.match(output, /ADV: Willkommen/); assert.match(output, /ADP: bei/); @@ -110,15 +107,13 @@ describe('analyze.v1beta2', () => { }); it('should classify text in a file', async () => { - const output = await exec( - `${cmd} classify-file ${bucketName} ${fileName2}` - ); + const output = execSync(`${cmd} classify-file ${bucketName} ${fileName2}`); assert.match(output, /Name:/); assert.match(output, /Computers & Electronics/); }); it('should classify text in text', async () => { - const output = await exec(`${cmd} classify-text "${text2}"`); + const output = execSync(`${cmd} classify-text "${text2}"`); assert.match(output, /Name:/); assert.match(output, /Computers & Electronics/); }); diff --git a/cloud-language/snippets/test/automlNaturalLanguage.test.js b/cloud-language/snippets/test/automlNaturalLanguage.test.js index d4829a2209..bf13609923 100644 --- a/cloud-language/snippets/test/automlNaturalLanguage.test.js +++ b/cloud-language/snippets/test/automlNaturalLanguage.test.js @@ -16,7 +16,7 @@ 'use strict'; const {assert} = require('chai'); -const execa = require('execa'); +const {execSync} = require('child_process'); const cmdDataset = 'node automl/automlNaturalLanguageDataset.js'; const cmdModel = 'node automl/automlNaturalLanguageModel.js'; @@ -28,24 +28,22 @@ const testModelName = 'dummyModel'; const sampleText = './resources/test.txt'; const projectId = process.env.GCLOUD_PROJECT; -const exec = async cmd => (await execa.shell(cmd)).stdout; - describe.skip('automl', () => { // Skipped because it's been taking too long to delete datasets it('should create a create, list, and delete a dataset', async () => { // Check to see that this dataset does not yet exist - let output = await exec(`${cmdDataset} list-datasets`); + let output = execSync(`${cmdDataset} list-datasets`); //t.false(output.includes(testDataSetName)); assert.notMatch(output, /testDataset/); // Create dataset - output = await exec(`${cmdDataset} create-dataset -n "${testDataSetName}"`); + output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`); const parsedOut = output.split('\n'); const dataSetId = parsedOut[1].split(':')[1].trim(); assert.match(output, /Dataset display name: {2}testDataset/); // Delete dataset - output = await exec(`${cmdDataset} delete-dataset -i "${dataSetId}"`); + output = execSync(`${cmdDataset} delete-dataset -i "${dataSetId}"`); assert.match(output, /Dataset deleted./); }); @@ -53,11 +51,11 @@ describe.skip('automl', () => { // We make two models running this test, see hard-coded workaround below it('should create a dataset, import data, and start making a model', async () => { // Check to see that this dataset does not yet exist - let output = await exec(`${cmdDataset} list-datasets`); + let output = execSync(`${cmdDataset} list-datasets`); assert.notMatch(output, /dummyDataset/); // Create dataset - output = await exec(`${cmdDataset} create-dataset -n "${dummyDataSet}"`); + output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`); const dataSetId = output .split('\n')[1] @@ -66,17 +64,17 @@ describe.skip('automl', () => { assert.match(output, /Dataset display name: {2}dummyDataSet/); // Import Data - output = await exec( + output = execSync( `${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/happiness.csv"` ); assert.match(output, /Data imported./); // Check to make sure model doesn't already exist - output = await exec(`${cmdModel} list-models`); + output = execSync(`${cmdModel} list-models`); assert.notMatch(output, /dummyModel/); // Begin training dataset, getting operation ID for next operation - output = await exec( + output = execSync( `${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"` ); const operationName = output @@ -86,7 +84,7 @@ describe.skip('automl', () => { assert.match(output, /Training started.../); // Poll operation status, here confirming that operation is not complete yet - output = await exec( + output = execSync( `${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"` ); assert.match(output, /done: false/); @@ -96,16 +94,16 @@ describe.skip('automl', () => { const donotdeleteModelId = `TCN4740161257642267869`; // Confirm dataset exists - let output = await exec(`${cmdDataset} list-datasets`); + let output = execSync(`${cmdDataset} list-datasets`); assert.match(output, /dummyDb/); // List model evaluations, confirm model exists - output = await exec( + output = execSync( `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` ); // Display evaluation - output = await exec( + output = execSync( `${cmdModel} display-evaluation -a "${donotdeleteModelId}"` ); assert.match(output, /Model Precision:/); @@ -115,17 +113,17 @@ describe.skip('automl', () => { const donotdeleteModelId = `TCN4740161257642267869`; // Confirm dataset exists - let output = await exec(`${cmdDataset} list-datasets`); + let output = execSync(`${cmdDataset} list-datasets`); assert.match(output, /do_not_delete_me/); // List model evaluations, confirm model exists - output = await exec( + output = execSync( `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` ); assert.match(output, /classificationEvaluationMetrics:/); // Run prediction on 'test.txt' in resources folder - output = await exec( + output = execSync( `${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -s "0.5"` ); assert.match(output, /Firm_Cheese/); @@ -133,7 +131,7 @@ describe.skip('automl', () => { // List datasets it('should list datasets', async () => { - const output = await exec(`${cmdDataset} list-datasets ${projectId}`); + const output = execSync(`${cmdDataset} list-datasets ${projectId}`); assert.match(output, /List of datasets:/); }); }); diff --git a/cloud-language/snippets/test/quickstart.test.js b/cloud-language/snippets/test/quickstart.test.js index e4fa6f8bd1..be26e4228d 100644 --- a/cloud-language/snippets/test/quickstart.test.js +++ b/cloud-language/snippets/test/quickstart.test.js @@ -16,11 +16,11 @@ 'use strict'; const {assert} = require('chai'); -const execa = require('execa'); +const {execSync} = require('child_process'); describe('quickstart', () => { it('should analyze sentiment in text', async () => { - const {stdout} = await execa.shell('node quickstart.js'); + const stdout = execSync('node quickstart.js'); assert(stdout, /Text: Hello, world!/); assert(stdout, /Sentiment score: /); assert(stdout, /Sentiment magnitude: /);