Skip to content

Commit fd88107

Browse files
JustinBeckwithBenjamin E. Coe
authored andcommitted
refactor: use execSync for tests (#409)
1 parent d09c741 commit fd88107

6 files changed

Lines changed: 44 additions & 60 deletions

File tree

handwritten/bigquery/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
"eslint-config-prettier": "^4.0.0",
8181
"eslint-plugin-node": "^8.0.0",
8282
"eslint-plugin-prettier": "^3.0.0",
83-
"execa": "^1.0.0",
8483
"gts": "^0.9.0",
8584
"ink-docstrap": "^1.3.2",
8685
"intelli-espower-loader": "^1.0.1",

handwritten/bigquery/samples/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
},
2222
"devDependencies": {
2323
"chai": "^4.2.0",
24-
"execa": "^1.0.0",
2524
"mocha": "^6.0.0",
2625
"uuid": "^3.3.0"
2726
}

handwritten/bigquery/samples/test/datasets.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
const {BigQuery} = require('@google-cloud/bigquery');
1919
const {assert} = require('chai');
20-
const execa = require('execa');
20+
const cp = require('child_process');
2121
const uuid = require('uuid');
2222

23-
const exec = async cmd => (await execa.shell(cmd)).stdout;
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
2425
const datasetId = `gcloud_tests_${uuid.v4()}`.replace(/-/gi, '_');
2526
const bigquery = new BigQuery();
2627

@@ -33,21 +34,21 @@ describe(`Datasets`, () => {
3334
});
3435

3536
it(`should create a dataset`, async () => {
36-
const output = await exec(`node createDataset.js ${datasetId}`);
37-
assert.strictEqual(output, `Dataset ${datasetId} created.`);
37+
const output = execSync(`node createDataset.js ${datasetId}`);
38+
assert.include(output, `Dataset ${datasetId} created.`);
3839
const [exists] = await bigquery.dataset(datasetId).exists();
3940
assert.ok(exists);
4041
});
4142

4243
it(`should list datasets`, async () => {
43-
const output = await exec(`node listDatasets.js`);
44+
const output = execSync(`node listDatasets.js`);
4445
assert.match(output, /Datasets:/);
4546
assert.match(output, new RegExp(datasetId));
4647
});
4748

4849
it(`should delete a dataset`, async () => {
49-
const output = await exec(`node deleteDataset.js ${datasetId}`);
50-
assert.strictEqual(output, `Dataset ${datasetId} deleted.`);
50+
const output = execSync(`node deleteDataset.js ${datasetId}`);
51+
assert.include(output, `Dataset ${datasetId} deleted.`);
5152
const [exists] = await bigquery.dataset(datasetId).exists();
5253
assert.strictEqual(exists, false);
5354
});

handwritten/bigquery/samples/test/queries.test.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,37 @@
1616
'use strict';
1717

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

21-
// const cmd = `node queries.js`;
22-
const exec = async cmd => {
23-
const res = await execa.shell(cmd);
24-
assert.isEmpty(res.stderr);
25-
return res.stdout;
26-
};
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2722

2823
describe(`Queries`, () => {
2924
it(`should query stackoverflow`, async () => {
30-
const output = await exec(`node queryStackOverflow.js`);
25+
const output = execSync(`node queryStackOverflow.js`);
3126
assert.match(output, /Query Results:/);
3227
assert.match(output, /views/);
3328
});
3429

3530
it(`should run a query`, async () => {
36-
const output = await exec(`node query.js`);
31+
const output = execSync(`node query.js`);
3732
assert.match(output, /Rows:/);
3833
assert.match(output, /name/);
3934
});
4035

4136
it(`should run a query with the cache disabled`, async () => {
42-
const output = await exec(`node queryDisableCache.js`);
37+
const output = execSync(`node queryDisableCache.js`);
4338
assert.match(output, /Rows:/);
4439
assert.match(output, /corpus/);
4540
});
4641

4742
it(`should run a query with named params`, async () => {
48-
const output = await exec(`node queryParamsNamed.js`);
43+
const output = execSync(`node queryParamsNamed.js`);
4944
assert.match(output, /Rows:/);
5045
assert.match(output, /word_count/);
5146
});
5247

5348
it(`should run a query with positional params`, async () => {
54-
const output = await exec(`node queryParamsNamed.js`);
49+
const output = execSync(`node queryParamsNamed.js`);
5550
assert.match(output, /Rows:/);
5651
assert.match(output, /word_count/);
5752
});

handwritten/bigquery/samples/test/quickstart.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717

1818
const {assert} = require('chai');
1919
const uuid = require('uuid');
20-
const execa = require('execa');
20+
const cp = require('child_process');
2121
const {BigQuery} = require('@google-cloud/bigquery');
2222

23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
2325
const bigquery = new BigQuery();
24-
const exec = async cmd => {
25-
const res = await execa.shell(cmd);
26-
assert.isEmpty(res.stderr);
27-
return res.stdout;
28-
};
2926

3027
describe('Quickstart', () => {
3128
const datasetName = `gcloud_tests_${uuid.v4()}`.replace(/-/gi, '_');
@@ -35,7 +32,7 @@ describe('Quickstart', () => {
3532
});
3633

3734
it('quickstart should create a dataset', async () => {
38-
const output = await exec(`node quickstart ${datasetName}`);
39-
assert.strictEqual(output, `Dataset ${datasetName} created.`);
35+
const output = execSync(`node quickstart ${datasetName}`);
36+
assert.include(output, `Dataset ${datasetName} created.`);
4037
});
4138
});

handwritten/bigquery/samples/test/tables.test.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
const {assert} = require('chai');
1919
const path = require('path');
2020
const uuid = require('uuid');
21-
const execa = require('execa');
21+
const cp = require('child_process');
2222
const {Storage} = require('@google-cloud/storage');
2323
const {BigQuery} = require('@google-cloud/bigquery');
2424

25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
2527
const storage = new Storage();
26-
const exec = async cmd => {
27-
const res = await execa.shell(cmd);
28-
assert.isEmpty(res.stderr);
29-
return res.stdout;
30-
};
3128
const generateUuid = () => `gcloud-tests-${uuid.v4()}`.replace(/-/gi, '_');
3229

3330
const datasetId = generateUuid();
@@ -82,10 +79,10 @@ describe('Tables', () => {
8279
});
8380

8481
it(`should create a table`, async () => {
85-
const output = await exec(
82+
const output = execSync(
8683
`node createTable.js ${datasetId} ${tableId} "${schema}"`
8784
);
88-
assert.strictEqual(output, `Table ${tableId} created.`);
85+
assert.include(output, `Table ${tableId} created.`);
8986
const [exists] = await bigquery
9087
.dataset(datasetId)
9188
.table(tableId)
@@ -94,13 +91,13 @@ describe('Tables', () => {
9491
});
9592

9693
it(`should list tables`, async () => {
97-
const output = await exec(`node listTables.js ${datasetId}`);
94+
const output = execSync(`node listTables.js ${datasetId}`);
9895
assert.match(output, /Tables:/);
9996
assert.match(output, new RegExp(tableId));
10097
});
10198

10299
it(`should load a local CSV file`, async () => {
103-
const output = await exec(
100+
const output = execSync(
104101
`node loadLocalFile.js ${datasetId} ${tableId} ${localFilePath}`
105102
);
106103
assert.match(output, /completed\./);
@@ -112,15 +109,15 @@ describe('Tables', () => {
112109
});
113110

114111
it(`should browse table rows`, async () => {
115-
const output = await exec(`node browseRows.js ${datasetId} ${tableId}`);
116-
assert.strictEqual(
112+
const output = execSync(`node browseRows.js ${datasetId} ${tableId}`);
113+
assert.include(
117114
output,
118115
`Rows:\n{ Name: 'Gandalf', Age: 2000, Weight: 140, IsMagic: true }`
119116
);
120117
});
121118

122119
it(`should extract a table to GCS`, async () => {
123-
const output = await exec(
120+
const output = execSync(
124121
`node extractTableToGCS.js ${datasetId} ${tableId} ${bucketName} ${exportFileName}`
125122
);
126123

@@ -134,9 +131,7 @@ describe('Tables', () => {
134131

135132
it(`should load a GCS ORC file`, async () => {
136133
const tableId = generateUuid();
137-
const output = await exec(
138-
`node loadTableGCSORC.js ${datasetId} ${tableId}`
139-
);
134+
const output = execSync(`node loadTableGCSORC.js ${datasetId} ${tableId}`);
140135
assert.match(output, /completed\./);
141136
const [rows] = await bigquery
142137
.dataset(datasetId)
@@ -147,7 +142,7 @@ describe('Tables', () => {
147142

148143
it(`should load a GCS Parquet file`, async () => {
149144
const tableId = generateUuid();
150-
const output = await exec(
145+
const output = execSync(
151146
`node loadTableGCSParquet.js ${datasetId} ${tableId}`
152147
);
153148
assert.match(output, /completed\./);
@@ -160,7 +155,7 @@ describe('Tables', () => {
160155

161156
it(`should load a GCS CSV file with explicit schema`, async () => {
162157
const tableId = generateUuid();
163-
const output = await exec(`node loadCSVFromGCS.js ${datasetId} ${tableId}`);
158+
const output = execSync(`node loadCSVFromGCS.js ${datasetId} ${tableId}`);
164159
assert.match(output, /completed\./);
165160
const [rows] = await bigquery
166161
.dataset(datasetId)
@@ -171,9 +166,7 @@ describe('Tables', () => {
171166

172167
it(`should load a GCS JSON file with explicit schema`, async () => {
173168
const tableId = generateUuid();
174-
const output = await exec(
175-
`node loadJSONFromGCS.js ${datasetId} ${tableId}`
176-
);
169+
const output = execSync(`node loadJSONFromGCS.js ${datasetId} ${tableId}`);
177170
assert.match(output, /completed\./);
178171
const [rows] = await bigquery
179172
.dataset(datasetId)
@@ -184,7 +177,7 @@ describe('Tables', () => {
184177

185178
it(`should load a GCS CSV file with autodetected schema`, async () => {
186179
const tableId = generateUuid();
187-
const output = await exec(
180+
const output = execSync(
188181
`node loadCSVFromGCSAutodetect.js ${datasetId} ${tableId}`
189182
);
190183
assert.match(output, /completed\./);
@@ -197,7 +190,7 @@ describe('Tables', () => {
197190

198191
it(`should load a GCS JSON file with autodetected schema`, async () => {
199192
const tableId = generateUuid();
200-
const output = await exec(
193+
const output = execSync(
201194
`node loadJSONFromGCSAutodetect.js ${datasetId} ${tableId}`
202195
);
203196
assert.match(output, /completed\./);
@@ -210,7 +203,7 @@ describe('Tables', () => {
210203

211204
it(`should load a GCS CSV file truncate table`, async () => {
212205
const tableId = generateUuid();
213-
const output = await exec(
206+
const output = execSync(
214207
`node loadCSVFromGCSTruncate.js ${datasetId} ${tableId}`
215208
);
216209
assert.match(output, /completed\./);
@@ -223,7 +216,7 @@ describe('Tables', () => {
223216

224217
it(`should load a GCS JSON file truncate table`, async () => {
225218
const tableId = generateUuid();
226-
const output = await exec(
219+
const output = execSync(
227220
`node loadJSONFromGCSTruncate.js ${datasetId} ${tableId}`
228221
);
229222
assert.match(output, /completed\./);
@@ -236,7 +229,7 @@ describe('Tables', () => {
236229

237230
it(`should load a GCS parquet file truncate table`, async () => {
238231
const tableId = generateUuid();
239-
const output = await exec(
232+
const output = execSync(
240233
`node loadParquetFromGCSTruncate.js ${datasetId} ${tableId}`
241234
);
242235
assert.match(output, /completed\./);
@@ -249,7 +242,7 @@ describe('Tables', () => {
249242

250243
it(`should load a GCS ORC file truncate table`, async () => {
251244
const tableId = generateUuid();
252-
const output = await exec(
245+
const output = execSync(
253246
`node loadOrcFromGCSTruncate.js ${datasetId} ${tableId}`
254247
);
255248
assert.match(output, /completed\./);
@@ -261,7 +254,7 @@ describe('Tables', () => {
261254
});
262255

263256
it(`should copy a table`, async () => {
264-
const output = await exec(
257+
const output = execSync(
265258
`node copyTable.js ${srcDatasetId} ${srcTableId} ${destDatasetId} ${destTableId}`
266259
);
267260
assert.match(output, /completed\./);
@@ -273,15 +266,15 @@ describe('Tables', () => {
273266
});
274267

275268
it(`should insert rows`, async () => {
276-
const output = await exec(
269+
const output = execSync(
277270
`node insertRowsAsStream.js ${datasetId} ${tableId}`
278271
);
279272
assert.match(output, /Inserted 2 rows/);
280273
});
281274

282275
it(`should delete a table`, async () => {
283-
const output = await exec(`node deleteTable.js ${datasetId} ${tableId}`);
284-
assert.strictEqual(output, `Table ${tableId} deleted.`);
276+
const output = execSync(`node deleteTable.js ${datasetId} ${tableId}`);
277+
assert.include(output, `Table ${tableId} deleted.`);
285278
const [exists] = await bigquery
286279
.dataset(datasetId)
287280
.table(tableId)

0 commit comments

Comments
 (0)