Skip to content

Commit 54a05b4

Browse files
JustinBeckwithAce Nassri
authored and
Ace Nassri
committed
refactor: use execSync for tests (#237)
1 parent 5454e22 commit 54a05b4

16 files changed

+87
-87
lines changed

translate/.eslintrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
---
22
rules:
33
no-console: off
4+
node/no-missing-require: off

translate/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"node": ">=8"
99
},
1010
"scripts": {
11-
"test": "mocha system-test --recursive --timeout 90000"
11+
"test": "mocha --recursive --timeout 90000"
1212
},
1313
"dependencies": {
1414
"@google-cloud/translate": "^3.0.0",
@@ -17,7 +17,6 @@
1717
},
1818
"devDependencies": {
1919
"chai": "^4.2.0",
20-
"execa": "^1.0.0",
2120
"@google-cloud/storage": "^2.4.3",
2221
"mocha": "^6.0.0",
2322
"uuid": "^3.3.2"
File renamed without changes.

translate/system-test/automlTranslation.test.js renamed to translate/test/automlTranslation.test.js

+19-27
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,9 @@
1616
'use strict';
1717

1818
const {assert} = require('chai');
19-
const execa = require('execa');
20-
const path = require('path');
21-
22-
const cwd = path.join(__dirname, '..', 'automl');
23-
const exec = async cmd => {
24-
const res = await execa.shell(cmd, {cwd});
25-
if (res.stderr) {
26-
throw new Error(res.stderr);
27-
}
28-
return res.stdout;
29-
};
19+
const cp = require('child_process');
20+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
3022

3123
const cmdDataset = 'node automlTranslationDataset.js';
3224
const cmdModel = 'node automlTranslationModel.js';
@@ -41,11 +33,11 @@ const donotdeleteModelId = 'TRL188026453969732486';
4133
describe.skip('automl sample tests', () => {
4234
it(`should create a create, list, and delete a dataset`, async () => {
4335
// Check to see that this dataset does not yet exist
44-
let output = await exec(`${cmdDataset} list-datasets`);
36+
let output = execSync(`${cmdDataset} list-datasets`);
4537
assert.match(output, new RegExp(testDataSetName));
4638

4739
// Create dataset
48-
output = await exec(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
40+
output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`);
4941
const dataSetId = output
5042
.split(`\n`)[1]
5143
.split(`:`)[1]
@@ -56,36 +48,36 @@ describe.skip('automl sample tests', () => {
5648
);
5749

5850
// Delete dataset
59-
output = await exec(`${cmdDataset} delete-dataset -i "${dataSetId}"`);
51+
output = execSync(`${cmdDataset} delete-dataset -i "${dataSetId}"`);
6052
assert.match(output, /Dataset deleted./);
6153
});
6254

6355
// We make two models running this test, see hard-coded workaround below
6456
it(`should create a dataset, import data, and start making a model`, async () => {
6557
// Check to see that this dataset does not yet exist
66-
let output = await exec(`${cmdDataset} list-datasets`);
58+
let output = execSync(`${cmdDataset} list-datasets`);
6759
assert.notMatch(output, new RegExp(dummyDataSet));
6860

6961
// Create dataset
70-
output = await exec(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);
62+
output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`);
7163
const dataSetId = output
7264
.split(`\n`)[1]
7365
.split(`:`)[1]
7466
.trim();
7567
assert.match(output, new RegExp(`Dataset display name: ${dummyDataSet}`));
7668

7769
// Import Data
78-
output = await exec(
70+
output = execSync(
7971
`${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv"`
8072
);
8173
assert.match(output, /Data imported./);
8274

8375
// Check to make sure model doesn't already exist
84-
output = await exec(`${cmdModel} list-models`);
76+
output = execSync(`${cmdModel} list-models`);
8577
assert.notMatch(output, testModelName);
8678

8779
// Begin training dataset, getting operation ID for next operation
88-
output = await exec(
80+
output = execSync(
8981
`${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"`
9082
);
9183
const operationName = output
@@ -95,41 +87,41 @@ describe.skip('automl sample tests', () => {
9587
assert.match(output, `Training started...`);
9688

9789
// Poll operation status, here confirming that operation is not complete yet
98-
output = await exec(
90+
output = execSync(
9991
`${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"`
10092
);
10193
assert.match(output, /done: false/);
10294
});
10395

10496
it(`should run get model (from a prexisting model)`, async () => {
10597
// Confirm dataset exists
106-
let output = await exec(`${cmdDataset} list-datasets`);
98+
let output = execSync(`${cmdDataset} list-datasets`);
10799
assert.match(output, /me_do_not_delete/);
108100

109101
// List model evaluations, confirm model exists
110-
output = await exec(
102+
output = execSync(
111103
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
112104
);
113105
assert.match(output, /translationEvaluationMetrics:/);
114106

115107
// Get model evaluation
116-
output = await exec(`${cmdModel} get-model -a "${donotdeleteModelId}"`);
108+
output = execSync(`${cmdModel} get-model -a "${donotdeleteModelId}"`);
117109
assert.match(output, /Model deployment state: DEPLOYED/);
118110
});
119111

120112
it(`should run Prediction from prexisting model`, async () => {
121113
// Confirm dataset exists
122-
let output = await exec(`${cmdDataset} list-datasets`);
114+
let output = execSync(`${cmdDataset} list-datasets`);
123115
assert.match(output, /me_do_not_delete/);
124116

125117
// List model evaluations, confirm model exists
126-
output = await exec(
118+
output = execSync(
127119
`${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"`
128120
);
129121
assert.match(output, `translationEvaluationMetrics:`);
130122

131123
// Run prediction on 'testImage.jpg' in resources folder
132-
output = await exec(
124+
output = execSync(
133125
`${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleText}" -t "False"`
134126
);
135127
assert.match(
@@ -140,7 +132,7 @@ describe.skip('automl sample tests', () => {
140132

141133
// List datasets
142134
it(`should list datasets`, async () => {
143-
const output = await exec(`${cmdDataset} list-datasets`);
135+
const output = execSync(`${cmdDataset} list-datasets`);
144136
assert.match(output, /List of datasets:/);
145137
});
146138
});

translate/system-test/quickstart.test.js renamed to translate/test/quickstart.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
'use strict';
1717

1818
const {assert} = require('chai');
19-
const execa = require('execa');
19+
const cp = require('child_process');
2020
const path = require('path');
2121

22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
2224
const cwd = path.join(__dirname, '..');
2325
const projectId = process.env.GCLOUD_PROJECT;
2426

2527
describe('quickstart sample tests', () => {
2628
it('should translate a string', async () => {
27-
const {stdout} = await execa.shell(`node quickstart ${projectId}`, {cwd});
29+
const stdout = execSync(`node quickstart ${projectId}`, {cwd});
2830
assert.match(stdout, new RegExp('Translation: Привет, мир!'));
2931
});
3032
});

translate/system-test/translate.test.js renamed to translate/test/translate.test.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,60 @@
1515

1616
'use strict';
1717

18-
const path = require('path');
1918
const {assert} = require('chai');
2019
const {Translate} = require('@google-cloud/translate');
21-
const execa = require('execa');
20+
const cp = require('child_process');
2221

22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}).trim();
2323
const translate = new Translate();
24-
25-
const cwd = path.join(__dirname, '..');
2624
const cmd = 'node translate.js';
2725
const text = 'Hello world!';
2826
const text2 = 'Goodbye!';
2927
const model = 'nmt';
3028
const toLang = 'ru';
3129

32-
const exec = async cmd => {
33-
return (await execa.shell(cmd, {cwd})).stdout;
34-
};
35-
3630
describe('translate sample tests', () => {
3731
it('should detect language of a single string', async () => {
38-
const output = await exec(`${cmd} detect "${text}"`);
32+
const output = execSync(`${cmd} detect "${text}"`);
3933
const [detection] = await translate.detect(text);
40-
const expected = `Detections:\n${text} => ${detection.language}`;
41-
assert.strictEqual(output, expected);
34+
const expected = new RegExp(
35+
`Detections:\n${text} => ${detection.language}`
36+
);
37+
assert.match(output, expected);
4238
});
4339

4440
it('should detect language of multiple strings', async () => {
45-
const output = await exec(`${cmd} detect "${text}" "${text2}"`, cwd);
41+
const output = execSync(`${cmd} detect "${text}" "${text2}"`);
4642
const [detections] = await translate.detect([text, text2]);
47-
const expected = `Detections:\n${text} => ${
48-
detections[0].language
49-
}\n${text2} => ${detections[1].language}`;
50-
assert.strictEqual(output, expected);
43+
const expected = new RegExp(
44+
`Detections:\n${text} => ${detections[0].language}\n${text2} => ${
45+
detections[1].language
46+
}`
47+
);
48+
assert.match(output, expected);
5149
});
5250

53-
it('should list languages', async () => {
54-
const output = await exec(`${cmd} list`);
51+
it('should list languages', () => {
52+
const output = execSync(`${cmd} list`);
5553
assert.match(output, /Languages:/);
5654
assert.match(output, new RegExp(`{ code: 'af', name: 'Afrikaans' }`));
5755
});
5856

59-
it('should list languages with a target', async () => {
60-
const output = await exec(`${cmd} list es`);
57+
it('should list languages with a target', () => {
58+
const output = execSync(`${cmd} list es`);
6159
assert.match(output, /Languages:/);
6260
assert.match(output, new RegExp(`{ code: 'af', name: 'afrikáans' }`));
6361
});
6462

6563
it('should translate a single string', async () => {
66-
const output = await exec(`${cmd} translate ${toLang} "${text}"`);
64+
const output = execSync(`${cmd} translate ${toLang} "${text}"`);
6765
const [translation] = await translate.translate(text, toLang);
6866
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
6967
assert.strictEqual(output, expected);
7068
});
7169

7270
it('should translate multiple strings', async () => {
73-
const output = await exec(
74-
`${cmd} translate ${toLang} "${text}" "${text2}"`
75-
);
71+
const output = execSync(`${cmd} translate ${toLang} "${text}" "${text2}"`);
7672
const [translations] = await translate.translate([text, text2], toLang);
7773
const expected = `Translations:\n${text} => (${toLang}) ${
7874
translations[0]
@@ -81,7 +77,7 @@ describe('translate sample tests', () => {
8177
});
8278

8379
it('should translate a single string with a model', async () => {
84-
const output = await exec(
80+
const output = execSync(
8581
`${cmd} translate-with-model ${toLang} ${model} "${text}"`
8682
);
8783
const [translation] = await translate.translate(text, toLang);
@@ -90,7 +86,7 @@ describe('translate sample tests', () => {
9086
});
9187

9288
it('should translate multiple strings with a model', async () => {
93-
const output = await exec(
89+
const output = execSync(
9490
`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`
9591
);
9692
const [translations] = await translate.translate([text, text2], toLang);

translate/system-test/v3beta1/translate_batch_translate_text_beta.test.js renamed to translate/test/v3beta1/translate_batch_translate_text_beta.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
const {assert} = require('chai');
1919
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
2020
const {Storage} = require('@google-cloud/storage');
21-
const execa = require('execa');
21+
const cp = require('child_process');
2222
const uuid = require('uuid');
23-
const exec = async cmd => (await execa.shell(cmd)).stdout;
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2425

2526
const REGION_TAG = 'translate_batch_translate_text_beta';
2627

@@ -53,7 +54,7 @@ describe(REGION_TAG, () => {
5354
const inputUri = `gs://cloud-samples-data/translation/text.txt`;
5455

5556
const outputUri = `gs://${projectId}/${bucketName}`;
56-
const output = await exec(
57+
const output = execSync(
5758
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
5859
);
5960
assert.match(output, /Total Characters: 13/);

translate/system-test/v3beta1/translate_create_glossary_beta.test.js renamed to translate/test/v3beta1/translate_create_glossary_beta.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
const {assert} = require('chai');
1919
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
20-
const execa = require('execa');
21-
const exec = async cmd => (await execa.shell(cmd)).stdout;
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2223

2324
const REGION_TAG = 'translate_create_glossary_beta';
2425

@@ -29,7 +30,7 @@ describe(REGION_TAG, () => {
2930
const projectId = await translationClient.getProjectId();
3031
const location = 'us-central1';
3132
const glossaryId = 'test-glossary';
32-
const output = await exec(
33+
const output = execSync(
3334
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
3435
);
3536
assert.match(

translate/system-test/v3beta1/translate_delete_glossary_beta.test.js renamed to translate/test/v3beta1/translate_delete_glossary_beta.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
const {assert} = require('chai');
1919
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
20-
const execa = require('execa');
21-
const exec = async cmd => (await execa.shell(cmd)).stdout;
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2223

2324
const REGION_TAG = 'translate_delete_glossary_beta';
2425

@@ -60,7 +61,7 @@ describe(REGION_TAG, () => {
6061
it('should delete a glossary', async () => {
6162
const projectId = await translationClient.getProjectId();
6263

63-
const output = await exec(
64+
const output = execSync(
6465
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
6566
);
6667
assert.match(output, /glossary/);

translate/system-test/v3beta1/translate_detect_language_beta.test.js renamed to translate/test/v3beta1/translate_detect_language_beta.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
const {assert} = require('chai');
1919
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
20-
const execa = require('execa');
21-
const exec = async cmd => (await execa.shell(cmd)).stdout;
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2223

2324
const REGION_TAG = 'translate_detect_language_beta';
2425

@@ -28,7 +29,7 @@ describe(REGION_TAG, () => {
2829
const projectId = await translationClient.getProjectId();
2930
const location = 'global';
3031
const text = `'Hæ sæta'`;
31-
const output = await exec(
32+
const output = execSync(
3233
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${text}`
3334
);
3435
assert.match(output, /Language Code: is/);

translate/system-test/v3beta1/translate_get_glossary_beta.test.js renamed to translate/test/v3beta1/translate_get_glossary_beta.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
const {assert} = require('chai');
1919
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
20-
const execa = require('execa');
21-
const exec = async cmd => (await execa.shell(cmd)).stdout;
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2223

2324
const REGION_TAG = 'translate_get_glossary_beta';
2425

@@ -59,7 +60,7 @@ describe(REGION_TAG, () => {
5960
it('should get a glossary', async () => {
6061
const projectId = await translationClient.getProjectId();
6162

62-
const output = await exec(
63+
const output = execSync(
6364
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
6465
);
6566
assert.match(output, /test-glossary/);

0 commit comments

Comments
 (0)