Skip to content

Commit 9a0cddc

Browse files
JustinBeckwithjkwlui
authored andcommitted
refactor: use execSync for tests (googleapis#664)
* refactor: use execSync for tests * turn to sync functions * remove async function * fixit * refactor: wrap execSync with encoding: utf-8 * lint * loosen regexes * loosen assertions
1 parent 237a29c commit 9a0cddc

10 files changed

Lines changed: 135 additions & 137 deletions

File tree

handwritten/storage/samples/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
},
2424
"devDependencies": {
2525
"chai": "^4.2.0",
26-
"execa": "^1.0.0",
2726
"mocha": "^6.0.0",
2827
"node-fetch": "^2.3.0"
2928
}

handwritten/storage/samples/system-test/acl.test.js

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

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

24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
2426
const storage = new Storage();
2527
const bucketName = `nodejs-storage-samples-${uuid.v4()}`;
2628
const bucket = storage.bucket(bucketName);
@@ -29,8 +31,6 @@ const fileName = 'test.txt';
2931
const filePath = path.join(__dirname, '..', 'resources', fileName);
3032
const cmd = 'node acl.js';
3133

32-
const exec = async cmd => (await execa.shell(cmd)).stdout;
33-
3434
before(async () => {
3535
await bucket.create();
3636
await bucket.upload(filePath);
@@ -54,49 +54,47 @@ after(async () => {
5454
}
5555
});
5656

57-
it('should print acl for a bucket', async () => {
58-
const out = await exec(`${cmd} print-bucket-acl ${bucketName}`);
57+
it('should print acl for a bucket', () => {
58+
const out = execSync(`${cmd} print-bucket-acl ${bucketName}`);
5959
assert.match(out, /OWNER: project-editors-/);
6060
assert.match(out, /OWNER: project-owners-/);
6161
assert.match(out, /READER: project-viewers-/);
6262
});
6363

64-
it('should print acl for a file', async () => {
65-
const out = await exec(`${cmd} print-file-acl ${bucketName} ${fileName}`);
64+
it('should print acl for a file', () => {
65+
const out = execSync(`${cmd} print-file-acl ${bucketName} ${fileName}`);
6666
assert.match(out, /OWNER: project-editors-/);
6767
assert.match(out, /OWNER: project-owners-/);
6868
assert.match(out, /READER: project-viewers-/);
6969
});
7070

7171
it('should print a users acl for a bucket', async () => {
7272
await bucket.acl.readers.addUser(userEmail);
73-
const out = await exec(
73+
const out = execSync(
7474
`${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`
7575
);
7676
assert.match(out, new RegExp(`READER: user-${userEmail}`));
7777
await bucket.acl.readers.deleteUser(userEmail);
7878
});
7979

80-
it('should add a user as an owner on a bucket', async () => {
81-
const out = await exec(`${cmd} add-bucket-owner ${bucketName} ${userEmail}`);
80+
it('should add a user as an owner on a bucket', () => {
81+
const out = execSync(`${cmd} add-bucket-owner ${bucketName} ${userEmail}`);
8282
assert.match(
8383
out,
8484
new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`)
8585
);
8686
});
8787

88-
it('should remove a user from a bucket', async () => {
89-
const out = await exec(
90-
`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`
91-
);
88+
it('should remove a user from a bucket', () => {
89+
const out = execSync(`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`);
9290
assert.match(
9391
out,
9492
new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`)
9593
);
9694
});
9795

98-
it('should add a user as a default owner on a bucket', async () => {
99-
const out = await exec(
96+
it('should add a user as a default owner on a bucket', () => {
97+
const out = execSync(
10098
`${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`
10199
);
102100
assert.match(
@@ -105,8 +103,8 @@ it('should add a user as a default owner on a bucket', async () => {
105103
);
106104
});
107105

108-
it('should remove a default user from a bucket', async () => {
109-
const out = await exec(
106+
it('should remove a default user from a bucket', () => {
107+
const out = execSync(
110108
`${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`
111109
);
112110
assert.match(
@@ -117,15 +115,15 @@ it('should remove a default user from a bucket', async () => {
117115

118116
it('should print a users acl for a file', async () => {
119117
await bucket.file(fileName).acl.readers.addUser(userEmail);
120-
const out = await exec(
118+
const out = execSync(
121119
`${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`
122120
);
123121
assert.match(out, new RegExp(`READER: user-${userEmail}`));
124122
await bucket.file(fileName).acl.readers.deleteUser(userEmail);
125123
});
126124

127-
it('should add a user as an owner on a bucket', async () => {
128-
const out = await exec(
125+
it('should add a user as an owner on a bucket', () => {
126+
const out = execSync(
129127
`${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`
130128
);
131129
assert.match(
@@ -134,8 +132,8 @@ it('should add a user as an owner on a bucket', async () => {
134132
);
135133
});
136134

137-
it('should remove a user from a bucket', async () => {
138-
const out = await exec(
135+
it('should remove a user from a bucket', () => {
136+
const out = execSync(
139137
`${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`
140138
);
141139
assert.match(

handwritten/storage/samples/system-test/bucketLock.test.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
const path = require('path');
1919
const {Storage} = require('@google-cloud/storage');
2020
const {assert} = require('chai');
21-
const execa = require('execa');
21+
const cp = require('child_process');
2222
const uuid = require('uuid');
2323

24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
2426
const storage = new Storage();
2527
const cwd = path.join(__dirname, '..');
2628
const cmd = 'node bucketLock.js';
@@ -29,7 +31,6 @@ const bucket = storage.bucket(bucketName);
2931
const fileName = 'test.txt';
3032

3133
const uploadFilePath = path.join(cwd, 'resources', fileName);
32-
const exec = async cmd => (await execa.shell(cmd)).stdout;
3334

3435
before(async () => {
3536
await bucket.create();
@@ -49,9 +50,9 @@ after(async () => {
4950
}
5051
});
5152

52-
it('should set a retention policy on a bucket', async () => {
53+
it('should set a retention policy on a bucket', () => {
5354
const retentionPeriod = 5;
54-
const output = await exec(
55+
const output = execSync(
5556
`${cmd} set-retention-policy ${bucketName} ${retentionPeriod}`
5657
);
5758
assert.match(
@@ -62,13 +63,13 @@ it('should set a retention policy on a bucket', async () => {
6263
);
6364
});
6465

65-
it('should get a retention policy on a bucket', async () => {
66-
const output = await exec(`${cmd} get-retention-policy ${bucketName}`);
66+
it('should get a retention policy on a bucket', () => {
67+
const output = execSync(`${cmd} get-retention-policy ${bucketName}`);
6768
assert.match(output, /A retention policy exists!/);
6869
});
6970

70-
it('should enable default event-based hold on a bucket', async () => {
71-
const output = await exec(
71+
it('should enable default event-based hold on a bucket', () => {
72+
const output = execSync(
7273
`${cmd} enable-default-event-based-hold ${bucketName}`
7374
);
7475
assert.match(
@@ -77,15 +78,13 @@ it('should enable default event-based hold on a bucket', async () => {
7778
);
7879
});
7980

80-
it('should get default event-based hold on a bucket', async () => {
81-
const output = await exec(
82-
`${cmd} get-default-event-based-hold ${bucketName}`
83-
);
81+
it('should get default event-based hold on a bucket', () => {
82+
const output = execSync(`${cmd} get-default-event-based-hold ${bucketName}`);
8483
assert.match(output, /Default event-based hold: true./);
8584
});
8685

87-
it('should disable default event-based hold on a bucket', async () => {
88-
const output = await exec(
86+
it('should disable default event-based hold on a bucket', () => {
87+
const output = execSync(
8988
`${cmd} disable-default-event-based-hold ${bucketName}`
9089
);
9190
assert.match(
@@ -94,15 +93,15 @@ it('should disable default event-based hold on a bucket', async () => {
9493
);
9594
});
9695

97-
it('should set an event-based hold on a file', async () => {
98-
const output = await exec(
96+
it('should set an event-based hold on a file', () => {
97+
const output = execSync(
9998
`${cmd} set-event-based-hold ${bucketName} ${fileName}`
10099
);
101100
assert.match(output, new RegExp(`Event-based hold was set for ${fileName}.`));
102101
});
103102

104-
it('should release an event-based hold on a file', async () => {
105-
const output = await exec(
103+
it('should release an event-based hold on a file', () => {
104+
const output = execSync(
106105
`${cmd} release-event-based-hold ${bucketName} ${fileName}`
107106
);
108107
assert.match(
@@ -111,23 +110,23 @@ it('should release an event-based hold on a file', async () => {
111110
);
112111
});
113112

114-
it('should remove a retention policy on a bucket', async () => {
115-
const output = await exec(`${cmd} remove-retention-policy ${bucketName}`);
113+
it('should remove a retention policy on a bucket', () => {
114+
const output = execSync(`${cmd} remove-retention-policy ${bucketName}`);
116115
assert.match(
117116
output,
118117
new RegExp(`Removed bucket ${bucketName} retention policy.`)
119118
);
120119
});
121120

122-
it('should set an temporary hold on a file', async () => {
123-
const output = await exec(
121+
it('should set an temporary hold on a file', () => {
122+
const output = execSync(
124123
`${cmd} set-temporary-hold ${bucketName} ${fileName}`
125124
);
126125
assert.match(output, new RegExp(`Temporary hold was set for ${fileName}.`));
127126
});
128127

129-
it('should release an temporary hold on a file', async () => {
130-
const output = await exec(
128+
it('should release an temporary hold on a file', () => {
129+
const output = execSync(
131130
`${cmd} release-temporary-hold ${bucketName} ${fileName}`
132131
);
133132
assert.match(
@@ -136,10 +135,10 @@ it('should release an temporary hold on a file', async () => {
136135
);
137136
});
138137

139-
it('should lock a bucket with a retention policy', async () => {
138+
it('should lock a bucket with a retention policy', () => {
140139
const retentionPeriod = 5;
141-
await exec(`${cmd} set-retention-policy ${bucketName} ${retentionPeriod}`);
142-
const output = await exec(`${cmd} lock-retention-policy ${bucketName}`);
140+
execSync(`${cmd} set-retention-policy ${bucketName} ${retentionPeriod}`);
141+
const output = execSync(`${cmd} lock-retention-policy ${bucketName}`);
143142
assert.match(
144143
output,
145144
new RegExp(`Retention policy for ${bucketName} is now locked.`)

handwritten/storage/samples/system-test/buckets.test.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,41 @@
1717

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

23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
2325
const storage = new Storage();
2426
const bucketName = `nodejs-storage-samples-${uuid.v4()}`;
2527
const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA;
2628
const bucket = storage.bucket(bucketName);
2729
const cmd = 'node buckets.js';
28-
const exec = async cmd => (await execa.shell(cmd)).stdout;
2930

3031
after(async () => {
3132
return bucket.delete().catch(console.error);
3233
});
3334

3435
it('should create a bucket', async () => {
35-
const output = await exec(`${cmd} create ${bucketName}`);
36+
const output = execSync(`${cmd} create ${bucketName}`);
3637
assert.match(output, new RegExp(`Bucket ${bucketName} created.`));
3738
const [exists] = await bucket.exists();
3839
assert.strictEqual(exists, true);
3940
});
4041

41-
it('should list buckets', async () => {
42-
const output = await exec(`${cmd} list`);
42+
it('should list buckets', () => {
43+
const output = execSync(`${cmd} list`);
4344
assert.match(output, /Buckets:/);
4445
assert.match(output, new RegExp(bucketName));
4546
});
4647

4748
it('should set a buckets default KMS key', async () => {
48-
const output = await exec(
49+
const output = execSync(
4950
`${cmd} enable-default-kms-key ${bucketName} ${defaultKmsKeyName}`
5051
);
51-
assert.match(
52+
assert.include(
5253
output,
53-
new RegExp(
54-
`Default KMS key for ${bucketName} was set to ${defaultKmsKeyName}.`
55-
)
54+
`Default KMS key for ${bucketName} was set to ${defaultKmsKeyName}.`
5655
);
5756
const metadata = await bucket.getMetadata();
5857
assert.strictEqual(
@@ -62,7 +61,7 @@ it('should set a buckets default KMS key', async () => {
6261
});
6362

6463
it(`should enable a bucket's Bucket Policy Only`, async () => {
65-
const output = await exec(`${cmd} enable-bucket-policy-only ${bucketName}`);
64+
const output = execSync(`${cmd} enable-bucket-policy-only ${bucketName}`);
6665
assert.match(
6766
output,
6867
new RegExp(`Bucket Policy Only was enabled for ${bucketName}.`)
@@ -76,7 +75,7 @@ it(`should enable a bucket's Bucket Policy Only`, async () => {
7675
});
7776

7877
it(`should get a bucket's Bucket Policy Only metadata`, async () => {
79-
const output = await exec(`${cmd} get-bucket-policy-only ${bucketName}`);
78+
const output = execSync(`${cmd} get-bucket-policy-only ${bucketName}`);
8079

8180
assert.match(
8281
output,
@@ -92,7 +91,7 @@ it(`should get a bucket's Bucket Policy Only metadata`, async () => {
9291
});
9392

9493
it(`should disable a bucket's Bucket Policy Only`, async () => {
95-
const output = await exec(`${cmd} disable-bucket-policy-only ${bucketName}`);
94+
const output = execSync(`${cmd} disable-bucket-policy-only ${bucketName}`);
9695
assert.match(
9796
output,
9897
new RegExp(`Bucket Policy Only was disabled for ${bucketName}.`)
@@ -106,7 +105,7 @@ it(`should disable a bucket's Bucket Policy Only`, async () => {
106105
});
107106

108107
it(`should delete a bucket`, async () => {
109-
const output = await exec(`${cmd} delete ${bucketName}`);
108+
const output = execSync(`${cmd} delete ${bucketName}`);
110109
assert.match(output, new RegExp(`Bucket ${bucketName} deleted.`));
111110
const [exists] = await bucket.exists();
112111
assert.strictEqual(exists, false);

0 commit comments

Comments
 (0)