Skip to content

Commit 1354bc9

Browse files
jmdobryAce Nassri
authored and
Ace Nassri
committed
Cleanup App Engine samples and re-work tests. (#354)
1 parent 53c5293 commit 1354bc9

File tree

7 files changed

+2986
-166
lines changed

7 files changed

+2986
-166
lines changed

vision/samples/detect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ function detectFulltextGCS (bucketName, fileName) {
633633
// [END vision_fulltext_detection_gcs]
634634
}
635635

636-
require(`yargs`)
636+
require(`yargs`) // eslint-disable-line
637637
.demand(1)
638638
.command(
639639
`faces <fileName>`,

vision/samples/package.json

+26-9
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,40 @@
22
"name": "nodejs-docs-samples-vision",
33
"version": "0.0.1",
44
"private": true,
5-
"license": "Apache Version 2.0",
5+
"license": "Apache-2.0",
66
"author": "Google Inc.",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"cloud": {
12+
"requiresKeyFile": true,
13+
"requiresProjectId": true
14+
},
15+
"engines": {
16+
"node": ">=4.3.2"
17+
},
718
"scripts": {
8-
"test": "cd ..; npm run st -- --verbose vision/system-test/*.test.js"
19+
"lint": "samples lint",
20+
"pretest": "npm run lint",
21+
"system-test": "ava -T 1m --verbose system-test/*.test.js",
22+
"test": "npm run system-test"
923
},
1024
"dependencies": {
11-
"@google-cloud/storage": "1.0.0",
12-
"@google-cloud/vision": "0.11.0",
25+
"@google-cloud/storage": "1.1.0",
26+
"@google-cloud/vision": "0.11.2",
1327
"async": "2.3.0",
14-
"natural": "0.5.0",
28+
"natural": "0.5.1",
1529
"redis": "2.7.1",
16-
"yargs": "7.0.2"
30+
"yargs": "7.1.0"
31+
},
32+
"devDependencies": {
33+
"@google-cloud/nodejs-repo-tools": "1.3.1",
34+
"ava": "0.19.1",
35+
"proxyquire": "1.7.11",
36+
"sinon": "2.1.0"
1737
},
1838
"optionalDependencies": {
1939
"canvas": "1.6.5"
20-
},
21-
"engines": {
22-
"node": ">=4.3.2"
2340
}
2441
}

vision/samples/system-test/detect.test.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
'use strict';
1717

18-
require(`../../system-test/_setup`);
19-
2018
const path = require(`path`);
2119
const storage = require(`@google-cloud/storage`)();
20+
const test = require(`ava`);
21+
const tools = require(`@google-cloud/nodejs-repo-tools`);
2222
const uuid = require(`uuid`);
2323

2424
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
@@ -38,6 +38,7 @@ const files = [
3838
};
3939
});
4040

41+
test.before(tools.checkCredentials);
4142
test.before(async () => {
4243
const [bucket] = await storage.createBucket(bucketName);
4344
await Promise.all(files.map((file) => bucket.upload(file.localPath)));
@@ -51,123 +52,123 @@ test.after.always(async () => {
5152
});
5253

5354
test(`should detect faces in a local file`, async (t) => {
54-
const output = await runAsync(`${cmd} faces ${files[0].localPath}`, cwd);
55+
const output = await tools.runAsync(`${cmd} faces ${files[0].localPath}`, cwd);
5556
t.true(output.includes(`Faces:`));
5657
t.true(output.includes(`Face #1:`));
5758
});
5859

5960
test(`should detect faces in a remote file`, async (t) => {
60-
const output = await runAsync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`, cwd);
61+
const output = await tools.runAsync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`, cwd);
6162
t.true(output.includes(`Faces:`));
6263
t.true(output.includes(`Face #1:`));
6364
});
6465

6566
test(`should detect labels in a local file`, async (t) => {
66-
const output = await runAsync(`${cmd} labels ${files[4].localPath}`, cwd);
67+
const output = await tools.runAsync(`${cmd} labels ${files[4].localPath}`, cwd);
6768
t.true(output.includes(`Labels:`));
6869
t.true(output.includes(`cat`));
6970
});
7071

7172
test(`should detect labels in a remote file`, async (t) => {
72-
const output = await runAsync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`, cwd);
73+
const output = await tools.runAsync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`, cwd);
7374
t.true(output.includes(`Labels:`));
7475
t.true(output.includes(`cat`));
7576
});
7677

7778
test(`should detect landmarks in a local file`, async (t) => {
78-
const output = await runAsync(`${cmd} landmarks ${files[1].localPath}`, cwd);
79+
const output = await tools.runAsync(`${cmd} landmarks ${files[1].localPath}`, cwd);
7980
t.true(output.includes(`Landmarks:`));
8081
t.true(output.includes(`Palace of Fine Arts`));
8182
});
8283

8384
test(`should detect landmarks in a remote file`, async (t) => {
84-
const output = await runAsync(`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`, cwd);
85+
const output = await tools.runAsync(`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`, cwd);
8586
t.true(output.includes(`Landmarks:`));
8687
t.true(output.includes(`Palace of Fine Arts`));
8788
});
8889

8990
test(`should detect text in a local file`, async (t) => {
90-
const output = await runAsync(`${cmd} text ${files[3].localPath}`, cwd);
91+
const output = await tools.runAsync(`${cmd} text ${files[3].localPath}`, cwd);
9192
t.true(output.includes(`Text:`));
9293
t.true(output.includes(`System Software Update`));
9394
});
9495

9596
test(`should detect text in a remote file`, async (t) => {
96-
const output = await runAsync(`${cmd} text-gcs ${bucketName} ${files[3].name}`, cwd);
97+
const output = await tools.runAsync(`${cmd} text-gcs ${bucketName} ${files[3].name}`, cwd);
9798
t.true(output.includes(`Text:`));
9899
t.true(output.includes(`System Software Update`));
99100
});
100101

101102
test(`should detect logos in a local file`, async (t) => {
102-
const output = await runAsync(`${cmd} logos ${files[2].localPath}`, cwd);
103+
const output = await tools.runAsync(`${cmd} logos ${files[2].localPath}`, cwd);
103104
t.true(output.includes(`Logos:`));
104105
t.true(output.includes(`Google`));
105106
});
106107

107108
test(`should detect logos in a remote file`, async (t) => {
108-
const output = await runAsync(`${cmd} logos-gcs ${bucketName} ${files[2].name}`, cwd);
109+
const output = await tools.runAsync(`${cmd} logos-gcs ${bucketName} ${files[2].name}`, cwd);
109110
t.true(output.includes(`Logos:`));
110111
t.true(output.includes(`Google`));
111112
});
112113

113114
test(`should detect properties in a local file`, async (t) => {
114-
const output = await runAsync(`${cmd} properties ${files[1].localPath}`, cwd);
115+
const output = await tools.runAsync(`${cmd} properties ${files[1].localPath}`, cwd);
115116
t.true(output.includes(`Colors:`));
116117
t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`);
117118
});
118119

119120
test(`should detect properties in a remote file`, async (t) => {
120-
const output = await runAsync(`${cmd} properties-gcs ${bucketName} ${files[1].name}`, cwd);
121+
const output = await tools.runAsync(`${cmd} properties-gcs ${bucketName} ${files[1].name}`, cwd);
121122
t.true(output.includes(`Colors:`));
122123
t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`);
123124
});
124125

125126
test(`should detect safe-search in a local file`, async (t) => {
126-
const output = await runAsync(`${cmd} safe-search ${files[4].localPath}`, cwd);
127+
const output = await tools.runAsync(`${cmd} safe-search ${files[4].localPath}`, cwd);
127128
t.true(output.includes(`Medical:`));
128129
});
129130

130131
test(`should detect safe-search in a remote file`, async (t) => {
131-
const output = await runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
132+
const output = await tools.runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
132133
t.true(output.includes(`Medical:`));
133134
});
134135

135136
test(`should detect crop hints in a local file`, async (t) => {
136-
const output = await runAsync(`${cmd} crops ${files[2].localPath}`, cwd);
137+
const output = await tools.runAsync(`${cmd} crops ${files[2].localPath}`, cwd);
137138
t.true(output.includes(`Crop Hint 0:`));
138139
t.true(output.includes(`Bound 2: (280, 43)`));
139140
});
140141

141142
test(`should detect crop hints in a remote file`, async (t) => {
142-
const output = await runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd);
143+
const output = await tools.runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd);
143144
t.true(output.includes(`Crop Hint 0:`));
144145
t.true(output.includes(`Bound 2: (280, 43)`));
145146
});
146147

147148
test(`should detect similar web images in a local file`, async (t) => {
148-
const output = await runAsync(`${cmd} web ${files[5].localPath}`, cwd);
149-
t.true(output.includes('Full matches found: 5'));
149+
const output = await tools.runAsync(`${cmd} web ${files[5].localPath}`, cwd);
150+
t.true(output.includes('Full matches found:'));
150151
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
151-
t.true(output.includes('Partial matches found: 5'));
152-
t.true(output.includes('Web entities found: 5'));
152+
t.true(output.includes('Partial matches found:'));
153+
t.true(output.includes('Web entities found:'));
153154
t.true(output.includes('Description: Google Cloud Platform'));
154155
});
155156

156157
test(`should detect similar web images in a remote file`, async (t) => {
157-
const output = await runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd);
158-
t.true(output.includes('Full matches found: 5'));
158+
const output = await tools.runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd);
159+
t.true(output.includes('Full matches found:'));
159160
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
160-
t.true(output.includes('Partial matches found: 5'));
161-
t.true(output.includes('Web entities found: 5'));
161+
t.true(output.includes('Partial matches found:'));
162+
t.true(output.includes('Web entities found:'));
162163
t.true(output.includes('Description: Google'));
163164
});
164165

165166
test(`should read a document from a local file`, async (t) => {
166-
const output = await runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd);
167+
const output = await tools.runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd);
167168
t.true(output.includes('Google Cloud Platform'));
168169
});
169170

170171
test(`should read a document from a remote file`, async (t) => {
171-
const output = await runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd);
172+
const output = await tools.runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd);
172173
t.true(output.includes('Google Cloud Platform'));
173174
});

vision/samples/system-test/faceDetection.test.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
'use strict';
1717

18-
require(`../../system-test/_setup`);
19-
2018
const fs = require(`fs`);
2119
const path = require(`path`);
20+
const test = require(`ava`);
21+
const tools = require(`@google-cloud/nodejs-repo-tools`);
2222

2323
class MockCanvas {
2424
getContext () {
@@ -36,9 +36,11 @@ class MockCanvas {
3636
if (event === 'end') {
3737
setTimeout(cb, 1000);
3838
} else if (event === `data`) {
39+
/* eslint-disable */
3940
cb(`test`);
4041
cb(`foo`);
4142
cb(`bar`);
43+
/* eslint-enable */
4244
}
4345
}
4446
};
@@ -51,8 +53,9 @@ const faceDetectionExample = require(`../faceDetection`);
5153
const inputFile = path.join(__dirname, `../resources`, `face.png`);
5254
const outputFile = path.join(__dirname, `../../vision`, `out.png`);
5355

54-
test.before(stubConsole);
55-
test.after.always(restoreConsole);
56+
test.before(tools.checkCredentials);
57+
test.before(tools.stubConsole);
58+
test.after.always(tools.restoreConsole);
5659

5760
test.cb(`should detect faces`, (t) => {
5861
faceDetectionExample.main(inputFile, outputFile, MockCanvas, (err, faces) => {

vision/samples/system-test/quickstart.test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515

1616
'use strict';
1717

18-
require(`../../system-test/_setup`);
19-
18+
const path = require(`path`);
2019
const proxyquire = require(`proxyquire`).noPreserveCache();
20+
const sinon = require(`sinon`);
21+
const test = require(`ava`);
22+
const tools = require(`@google-cloud/nodejs-repo-tools`);
23+
2124
const vision = proxyquire(`@google-cloud/vision`, {})();
22-
const path = require(`path`);
2325

24-
test.before(stubConsole);
25-
test.after.always(restoreConsole);
26+
test.before(tools.stubConsole);
27+
test.after.always(tools.restoreConsole);
2628

2729
test.cb(`should detect labels`, (t) => {
2830
const filePath = path.join(__dirname, `../resources/wakeupcat.jpg`);

vision/samples/system-test/textDetection.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
'use strict';
1717

18-
require(`../../system-test/_setup`);
19-
2018
const path = require(`path`);
19+
const test = require(`ava`);
20+
const tools = require(`@google-cloud/nodejs-repo-tools`);
2121

2222
const inputDir = path.join(__dirname, `../resources`);
2323
const textDetectionSample = require(`../textDetection`);
2424

25+
test.before(tools.checkCredentials);
26+
2527
test.cb(`should detect texts`, (t) => {
2628
textDetectionSample.main(inputDir, (err, textResponse) => {
2729
t.ifError(err);

0 commit comments

Comments
 (0)