Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit acfa55c

Browse files
refactor: modernize the sample tests (#558)
1 parent eace3b9 commit acfa55c

13 files changed

Lines changed: 417 additions & 795 deletions

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,19 @@
3030
"storage"
3131
],
3232
"scripts": {
33-
"codecov": "nyc report --reporter=json && codecov -f .coverage/*.json",
3433
"docs": "jsdoc -c .jsdoc.js",
3534
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json",
3635
"system-test": "mocha build/system-test --timeout 600000",
3736
"presystem-test": "npm run compile",
38-
"test-only": "nyc mocha build/test",
39-
"test": "npm run test-only",
40-
"pretest-only": "npm run compile",
37+
"test": "nyc mocha build/test",
38+
"pretest": "npm run compile",
4139
"lint": "eslint samples/ && gts check",
4240
"samples-test": "npm link && cd samples/ && npm link ../ && npm test && cd ../",
4341
"all-test": "npm test && npm run system-test && npm run samples-test",
4442
"check": "gts check",
4543
"clean": "gts clean",
4644
"compile": "tsc -p .",
47-
"fix": "gts fix && eslint --fix samples/**/*.js",
45+
"fix": "gts fix && eslint --fix '**/*.js'",
4846
"prepare": "npm run compile"
4947
},
5048
"dependencies": {

samples/package.json

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
{
22
"name": "@google-cloud/storage-samples",
33
"description": "Samples for the Cloud Storage Client Library for Node.js.",
4-
"version": "0.0.1",
54
"license": "Apache-2.0",
65
"author": "Google Inc.",
76
"engines": {
87
"node": ">=8"
98
},
109
"repository": "googleapis/nodejs-storage",
1110
"private": true,
12-
"nyc": {
13-
"exclude": [
14-
"**/*.test.js"
15-
]
16-
},
11+
"files": [
12+
"*.js"
13+
],
1714
"scripts": {
1815
"cleanup": "node scripts/cleanup",
19-
"mocha": "mocha system-test/*.js --timeout 600000",
20-
"cover": "nyc --reporter=lcov --cache mocha system-test/*.js --timeout 600000 && nyc report",
21-
"test": "npm run cover"
16+
"test": "mocha system-test/*.js --timeout 600000"
2217
},
2318
"dependencies": {
24-
"@google-cloud/pubsub": "*",
19+
"@google-cloud/pubsub": "^0.22.2",
2520
"@google-cloud/storage": "^2.3.3",
2621
"uuid": "^3.3.2",
2722
"yargs": "^12.0.0"
2823
},
2924
"devDependencies": {
30-
"@google-cloud/nodejs-repo-tools": "^3.0.0",
31-
"mocha": "^5.2.0",
32-
"nyc": "^13.0.0",
33-
"proxyquire": "^2.0.1",
34-
"sinon": "^7.0.0"
25+
"chai": "^4.2.0",
26+
"execa": "^1.0.0",
27+
"mocha": "^5.2.0"
3528
}
3629
}

samples/quickstart.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,21 @@
1616
'use strict';
1717

1818
// [START storage_quickstart]
19-
// Imports the Google Cloud client library
20-
const {Storage} = require('@google-cloud/storage');
19+
async function quickstart(
20+
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
21+
bucketName = 'my-new-bucket' // The name for the new bucket
22+
) {
23+
// Imports the Google Cloud client library
24+
const {Storage} = require('@google-cloud/storage');
2125

22-
// Your Google Cloud Platform project ID
23-
const projectId = 'YOUR_PROJECT_ID';
26+
// Creates a client
27+
const storage = new Storage({projectId});
2428

25-
// Creates a client
26-
const storage = new Storage({
27-
projectId: projectId,
28-
});
29-
30-
// The name for the new bucket
31-
const bucketName = 'my-new-bucket';
32-
33-
// Creates the new bucket
34-
async function createBucket() {
29+
// Creates the new bucket
3530
await storage.createBucket(bucketName);
3631
console.log(`Bucket ${bucketName} created.`);
3732
}
38-
39-
try {
40-
createBucket();
41-
} catch (err) {
42-
console.error('ERROR:', err);
43-
}
4433
// [END storage_quickstart]
34+
35+
const args = process.argv.slice(2);
36+
quickstart(...args).catch(console.error);

samples/system-test/.eslintrc.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
---
22
env:
3-
mocha : true
4-
rules:
5-
node/no-unpublished-require: off
6-
no-empty: off
3+
mocha : true

samples/system-test/acl.test.js

Lines changed: 73 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -15,179 +15,131 @@
1515

1616
'use strict';
1717

18-
const path = require(`path`);
19-
const {Storage} = require(`@google-cloud/storage`);
20-
const assert = require('assert');
21-
const tools = require(`@google-cloud/nodejs-repo-tools`);
22-
const uuid = require(`uuid`);
18+
const {Storage} = require('@google-cloud/storage');
19+
const {assert} = require('chai');
20+
const execa = require('execa');
21+
const uuid = require('uuid');
22+
const path = require('path');
2323

2424
const storage = new Storage();
25-
26-
const cwd = path.join(__dirname, `..`);
2725
const bucketName = `nodejs-storage-samples-${uuid.v4()}`;
2826
const bucket = storage.bucket(bucketName);
29-
const userEmail = `jdobry@google.com`;
30-
const fileName = `test.txt`;
31-
const filePath = path.join(__dirname, `../resources`, fileName);
32-
const cmd = `node acl.js`;
27+
const userEmail = 'jdobry@google.com';
28+
const fileName = 'test.txt';
29+
const filePath = path.join(__dirname, '..', 'resources', fileName);
30+
const cmd = 'node acl.js';
31+
32+
const exec = async cmd => (await execa.shell(cmd)).stdout;
3333

34-
before(tools.checkCredentials);
3534
before(async () => {
3635
await bucket.create();
3736
await bucket.upload(filePath);
3837
});
3938

4039
after(async () => {
41-
// Try deleting all files twice
4240
try {
4341
await bucket.deleteFiles({force: true});
44-
} catch (err) {} // ignore error
42+
} catch (err) {
43+
// ignore error
44+
}
4545
try {
4646
await bucket.deleteFiles({force: true});
47-
} catch (err) {} // ignore error
47+
} catch (err) {
48+
// ignore error
49+
}
4850
try {
4951
await bucket.delete();
50-
} catch (err) {} // ignore error
52+
} catch (err) {
53+
// ignore error
54+
}
5155
});
5256

53-
it(`should print acl for a bucket`, async () => {
54-
const results = await tools.runAsyncWithIO(
55-
`${cmd} print-bucket-acl ${bucketName}`,
56-
cwd
57-
);
58-
assert.strictEqual(
59-
(results.stdout + results.stderr).includes(`OWNER: project-editors-`),
60-
true
61-
);
62-
assert.strictEqual(
63-
(results.stdout + results.stderr).includes(`OWNER: project-owners-`),
64-
true
65-
);
66-
assert.strictEqual(
67-
(results.stdout + results.stderr).includes(`READER: project-viewers-`),
68-
true
69-
);
57+
it('should print acl for a bucket', async () => {
58+
const out = await exec(`${cmd} print-bucket-acl ${bucketName}`);
59+
assert.match(out, /OWNER: project-editors-/);
60+
assert.match(out, /OWNER: project-owners-/);
61+
assert.match(out, /READER: project-viewers-/);
7062
});
7163

72-
it(`should print acl for a file`, async () => {
73-
const results = await tools.runAsyncWithIO(
74-
`${cmd} print-file-acl ${bucketName} ${fileName}`,
75-
cwd
76-
);
77-
assert.strictEqual(
78-
(results.stdout + results.stderr).includes(`OWNER: project-editors-`),
79-
true
80-
);
81-
assert.strictEqual(
82-
(results.stdout + results.stderr).includes(`OWNER: project-owners-`),
83-
true
84-
);
85-
assert.strictEqual(
86-
(results.stdout + results.stderr).includes(`READER: project-viewers-`),
87-
true
88-
);
64+
it('should print acl for a file', async () => {
65+
const out = await exec(`${cmd} print-file-acl ${bucketName} ${fileName}`);
66+
assert.match(out, /OWNER: project-editors-/);
67+
assert.match(out, /OWNER: project-owners-/);
68+
assert.match(out, /READER: project-viewers-/);
8969
});
9070

91-
it(`should print a user's acl for a bucket`, async () => {
71+
it('should print a users acl for a bucket', async () => {
9272
await bucket.acl.readers.addUser(userEmail);
93-
const results = await tools.runAsyncWithIO(
94-
`${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`,
95-
cwd
96-
);
97-
assert.strictEqual(
98-
(results.stdout + results.stderr).includes(`READER: user-${userEmail}`),
99-
true
73+
const out = await exec(
74+
`${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`
10075
);
76+
assert.match(out, new RegExp(`READER: user-${userEmail}`));
10177
await bucket.acl.readers.deleteUser(userEmail);
10278
});
10379

104-
it(`should add a user as an owner on a bucket`, async () => {
105-
const results = await tools.runAsyncWithIO(
106-
`${cmd} add-bucket-owner ${bucketName} ${userEmail}`,
107-
cwd
108-
);
109-
assert.strictEqual(
110-
(results.stdout + results.stderr).includes(
111-
`Added user ${userEmail} as an owner on bucket ${bucketName}.`
112-
),
113-
true
80+
it('should add a user as an owner on a bucket', async () => {
81+
const out = await exec(`${cmd} add-bucket-owner ${bucketName} ${userEmail}`);
82+
assert.match(
83+
out,
84+
new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`)
11485
);
11586
});
11687

117-
it(`should remove a user from a bucket`, async () => {
118-
const results = await tools.runAsyncWithIO(
119-
`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`,
120-
cwd
88+
it('should remove a user from a bucket', async () => {
89+
const out = await exec(
90+
`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`
12191
);
122-
assert.strictEqual(
123-
(results.stdout + results.stderr).includes(
124-
`Removed user ${userEmail} from bucket ${bucketName}.`
125-
),
126-
true
92+
assert.match(
93+
out,
94+
new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`)
12795
);
12896
});
12997

130-
it(`should add a user as a default owner on a bucket`, async () => {
131-
const results = await tools.runAsyncWithIO(
132-
`${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`,
133-
cwd
98+
it('should add a user as a default owner on a bucket', async () => {
99+
const out = await exec(
100+
`${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`
134101
);
135-
assert.strictEqual(
136-
(results.stdout + results.stderr).includes(
137-
`Added user ${userEmail} as an owner on bucket ${bucketName}.`
138-
),
139-
true
102+
assert.match(
103+
out,
104+
new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`)
140105
);
141106
});
142107

143-
it(`should remove a default user from a bucket`, async () => {
144-
const results = await tools.runAsyncWithIO(
145-
`${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`,
146-
cwd
108+
it('should remove a default user from a bucket', async () => {
109+
const out = await exec(
110+
`${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`
147111
);
148-
assert.strictEqual(
149-
(results.stdout + results.stderr).includes(
150-
`Removed user ${userEmail} from bucket ${bucketName}.`
151-
),
152-
true
112+
assert.match(
113+
out,
114+
new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`)
153115
);
154116
});
155117

156-
it(`should print a user's acl for a file`, async () => {
118+
it('should print a users acl for a file', async () => {
157119
await bucket.file(fileName).acl.readers.addUser(userEmail);
158-
const results = await tools.runAsyncWithIO(
159-
`${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`,
160-
cwd
161-
);
162-
assert.strictEqual(
163-
(results.stdout + results.stderr).includes(`READER: user-${userEmail}`),
164-
true
120+
const out = await exec(
121+
`${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`
165122
);
123+
assert.match(out, new RegExp(`READER: user-${userEmail}`));
166124
await bucket.file(fileName).acl.readers.deleteUser(userEmail);
167125
});
168126

169-
it(`should add a user as an owner on a bucket`, async () => {
170-
const results = await tools.runAsyncWithIO(
171-
`${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`,
172-
cwd
127+
it('should add a user as an owner on a bucket', async () => {
128+
const out = await exec(
129+
`${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`
173130
);
174-
assert.strictEqual(
175-
(results.stdout + results.stderr).includes(
176-
`Added user ${userEmail} as an owner on file ${fileName}.`
177-
),
178-
true
131+
assert.match(
132+
out,
133+
new RegExp(`Added user ${userEmail} as an owner on file ${fileName}.`)
179134
);
180135
});
181136

182-
it(`should remove a user from a bucket`, async () => {
183-
const results = await tools.runAsyncWithIO(
184-
`${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`,
185-
cwd
137+
it('should remove a user from a bucket', async () => {
138+
const out = await exec(
139+
`${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`
186140
);
187-
assert.strictEqual(
188-
(results.stdout + results.stderr).includes(
189-
`Removed user ${userEmail} from file ${fileName}.`
190-
),
191-
true
141+
assert.match(
142+
out,
143+
new RegExp(`Removed user ${userEmail} from file ${fileName}.`)
192144
);
193145
});

0 commit comments

Comments
 (0)