Skip to content

Commit a5decaf

Browse files
jbdosterJustinBeckwith
authored andcommitted
refactor: replace execa with execSync in sample tests (#372)
* fix #354 * matching execsync sample shared in issue * dropped pipe option for execSync in examples, unified exec calls, removed unused path requires * removed unnecessary awaits for exec calls
1 parent ee0f603 commit a5decaf

File tree

4 files changed

+55
-78
lines changed

4 files changed

+55
-78
lines changed

dialogflow/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
},
2525
"devDependencies": {
2626
"chai": "^4.2.0",
27-
"execa": "^1.0.0",
2827
"mocha": "^6.0.0"
2928
}
3029
}

dialogflow/system-test/detect.test.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
const path = require('path');
1919
const {assert} = require('chai');
20-
const execa = require('execa');
21-
20+
const execSync = require('child_process').execSync;
2221
const cmd = 'node detect.js';
2322
const cmd_tts = 'node detect-intent-TTS-response.v2.js';
2423
const cmd_sentiment = 'node detect-intent-sentiment.v2.js';
25-
const cwd = path.join(__dirname, '..');
2624
const projectId =
2725
process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT;
2826
const testQuery = 'Where is my data stored?';
@@ -31,48 +29,43 @@ const audioFilepathBookARoom = path
3129
.join(__dirname, '../resources/book_a_room.wav')
3230
.replace(/(\s+)/g, '\\$1');
3331

32+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
33+
3434
describe('basic detection', () => {
3535
it('should detect text queries', async () => {
36-
const {stdout} = await execa.shell(`${cmd} text -q "hello"`, {cwd});
36+
const stdout = exec(`${cmd} text -q "hello"`);
3737
assert.include(stdout, 'Detected intent');
3838
});
3939

4040
it('should detect event query', async () => {
41-
const {stdout} = await execa.shell(`${cmd} event WELCOME`, {cwd});
41+
const stdout = exec(`${cmd} event WELCOME`);
4242
assert.include(stdout, 'Query: WELCOME');
4343
});
4444

4545
it('should detect audio query', async () => {
46-
const {stdout} = await execa.shell(
47-
`${cmd} audio ${audioFilepathBookARoom} -r 16000`,
48-
{cwd}
49-
);
46+
const stdout = exec(
47+
`${cmd} audio ${audioFilepathBookARoom} -r 16000`);
5048
assert.include(stdout, 'Detected intent');
5149
});
5250

5351
it('should detect audio query in streaming fashion', async () => {
54-
const {stdout} = await execa.shell(
55-
`${cmd} stream ${audioFilepathBookARoom} -r 16000`,
56-
{cwd}
57-
);
52+
const stdout = exec(
53+
`${cmd} stream ${audioFilepathBookARoom} -r 16000`);
5854
assert.include(stdout, 'Detected intent');
5955
});
6056

6157
it('should detect Intent with Text to Speech Response', async () => {
62-
const {stdout} = await execa.shell(
63-
`${cmd_tts} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US' './resources/output.wav'`,
64-
{cwd}
65-
);
58+
const stdout = exec(
59+
`${cmd_tts} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US' './resources/output.wav'`);
6660
assert.include(
6761
stdout,
6862
'Audio content written to file: ./resources/output.wav'
6963
);
7064
});
7165

7266
it('should detect sentiment with intent', async () => {
73-
const {stdout} = await execa.shell(
74-
`${cmd_sentiment} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US'`,
75-
{cwd}
67+
const stdout = exec(
68+
`${cmd_sentiment} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US'`
7669
);
7770
assert.include(stdout, 'Detected sentiment');
7871
});

dialogflow/system-test/detect.v2beta1.test.js

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@
1515

1616
'use strict';
1717

18-
const path = require('path');
1918
const {assert} = require('chai');
20-
const execa = require('execa');
19+
const execSync = require('child_process').execSync;
2120
const uuid = require('uuid/v4');
22-
2321
const cmd = 'node detect.v2beta1.js';
24-
const {cwd} = path.join(__dirname, '..');
2522
const testQuery = 'Where is my data stored?';
2623
const testKnowledgeBaseName = `${uuid().split('-')[0]}-TestKnowledgeBase`;
2724
const testDocName = 'TestDoc';
2825
const testDocumentPath = 'https://cloud.google.com/storage/docs/faq';
2926

30-
const exec = async cmd => {
31-
const res = await execa.shell(cmd, {cwd});
32-
if (res.stderr) {
33-
throw new Error(res.stderr);
34-
}
35-
return res.stdout;
36-
};
27+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
3728

3829
describe('v2beta1 detection', () => {
3930
let knowbaseFullName;
@@ -42,11 +33,11 @@ describe('v2beta1 detection', () => {
4233

4334
it('should create a knowledge base', async () => {
4435
// Check that the knowledge base does not yet exist
45-
let output = await exec(`${cmd} listKnowledgeBases`);
36+
let output = exec(`${cmd} listKnowledgeBases`);
4637
assert.notInclude(output, testKnowledgeBaseName);
4738

4839
// Creates a knowledge base
49-
output = await exec(
40+
output = exec(
5041
`${cmd} createKnowledgeBase -k ${testKnowledgeBaseName}`
5142
);
5243
assert.include(output, `displayName: ${testKnowledgeBaseName}`);
@@ -62,66 +53,66 @@ describe('v2beta1 detection', () => {
6253
});
6354

6455
it('should list the knowledge bases', async () => {
65-
const output = await exec(`${cmd} listKnowledgeBases`);
56+
const output = exec(`${cmd} listKnowledgeBases`);
6657
assert.include(output, testKnowledgeBaseName);
6758
});
6859

6960
it('should get a knowledge base', async () => {
70-
const output = await exec(`${cmd} getKnowledgeBase -b "${knowbaseId}"`);
61+
const output = exec(`${cmd} getKnowledgeBase -b "${knowbaseId}"`);
7162
assert.include(output, `displayName: ${testKnowledgeBaseName}`);
7263
assert.include(output, `name: ${knowbaseFullName}`);
7364
});
7465

7566
it('should create a document', async () => {
76-
const output = await exec(
67+
const output = exec(
7768
`${cmd} createDocument -n "${knowbaseFullName}" -z "${testDocumentPath}" -m "${testDocName}"`
7869
);
7970
assert.include(output, 'Document created');
8071
});
8172

8273
it('should list documents', async () => {
83-
const output = await exec(`${cmd} listDocuments -n "${knowbaseFullName}"`);
74+
const output = exec(`${cmd} listDocuments -n "${knowbaseFullName}"`);
8475
const parsedOut = output.split('\n');
8576
documentFullPath = parsedOut[parsedOut.length - 1].split(':')[1];
8677
assert.include(output, `There are 1 documents in ${knowbaseFullName}`);
8778
});
8879

8980
it('should detect intent with a knowledge base', async () => {
90-
const output = await exec(
81+
const output = exec(
9182
`${cmd} detectIntentKnowledge -q "${testQuery}" -n "${knowbaseId}"`
9283
);
9384
assert.include(output, 'Detected Intent:');
9485
});
9586

9687
it('should delete a document', async () => {
97-
const output = await exec(`${cmd} deleteDocument -d ${documentFullPath}`);
88+
const output = exec(`${cmd} deleteDocument -d ${documentFullPath}`);
9889
assert.include(output, 'document deleted');
9990
});
10091

10192
it('should list the document', async () => {
102-
const output = await exec(`${cmd} listDocuments -n "${knowbaseFullName}"`);
93+
const output = exec(`${cmd} listDocuments -n "${knowbaseFullName}"`);
10394
assert.notInclude(output, documentFullPath);
10495
});
10596

10697
it('should delete the Knowledge Base', async () => {
107-
await exec(`${cmd} deleteKnowledgeBase -n "${knowbaseFullName}"`);
98+
exec(`${cmd} deleteKnowledgeBase -n "${knowbaseFullName}"`);
10899
});
109100

110101
it('should list the Knowledge Base', async () => {
111-
const output = await exec(`${cmd} listKnowledgeBases`);
102+
const output = exec(`${cmd} listKnowledgeBases`);
112103
assert.notInclude(output, testKnowledgeBaseName);
113104
});
114105

115106
it('should detect Intent with Model Selection', async () => {
116-
const output = await exec(`${cmd} detectIntentwithModelSelection`);
107+
const output = exec(`${cmd} detectIntentwithModelSelection`);
117108
assert.include(
118109
output,
119110
'Response: I can help with that. Where would you like to reserve a room?'
120111
);
121112
});
122113

123114
it('should detect Intent with Text to Speech Response', async () => {
124-
const output = await exec(
115+
const output = exec(
125116
`${cmd} detectIntentwithTexttoSpeechResponse -q "${testQuery}"`
126117
);
127118
assert.include(
@@ -131,7 +122,7 @@ describe('v2beta1 detection', () => {
131122
});
132123

133124
it('should detect sentiment with intent', async () => {
134-
const output = await exec(
125+
const output = exec(
135126
`${cmd} detectIntentandSentiment -q "${testQuery}"`
136127
);
137128
assert.include(output, 'Detected sentiment');

0 commit comments

Comments
 (0)