From 93bd76a49d668b9136952427b3714ae2bfab1333 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Fri, 30 Sep 2016 17:00:40 -0700 Subject: [PATCH] New quickstarts. --- README.md | 18 +++++ bigquery/package.json | 3 + bigquery/quickstart.js | 33 ++++---- bigquery/system-test/quickstart.test.js | 32 ++++---- bigquery/test/quickstart.test.js | 33 ++++---- datastore/package.json | 3 + datastore/quickstart.js | 33 ++++---- datastore/system-test/quickstart.test.js | 32 ++++---- datastore/test/quickstart.test.js | 38 +++++---- dns/README.md | 50 ++++++++++++ dns/package.json | 22 ++++++ dns/quickstart.js | 40 ++++++++++ dns/system-test/quickstart.test.js | 67 ++++++++++++++++ dns/system-test/zones.test.js | 52 +++++++++++++ dns/test/quickstart.test.js | 43 +++++++++++ dns/test/zones.test.js | 37 +++++++++ dns/zones.js | 63 +++++++++++++++ language/package.json | 3 + language/quickstart.js | 43 +++++++++++ language/system-test/quickstart.test.js | 51 ++++++++++++ language/test/quickstart.test.js | 44 +++++++++++ logging/package.json | 3 + logging/quickstart.js | 33 ++++---- logging/system-test/quickstart.test.js | 38 +++++---- logging/test/quickstart.test.js | 32 ++++---- pubsub/package.json | 2 +- pubsub/quickstart.js | 33 ++++---- pubsub/system-test/quickstart.test.js | 32 ++++---- pubsub/test/quickstart.test.js | 34 ++++---- resource/package.json | 10 ++- resource/projects.js | 81 +++++++++----------- resource/quickstart.js | 40 ++++++++++ resource/system-test/projects.test.js | 48 ++++++------ resource/system-test/quickstart.test.js | 48 ++++++++++++ resource/test/projects.test.js | 98 +++++++----------------- resource/test/quickstart.test.js | 43 +++++++++++ scripts/install | 1 + scripts/uninstall | 1 + speech/package.json | 4 +- speech/quickstart.js | 48 ++++++++++++ speech/system-test/quickstart.test.js | 58 ++++++++++++++ speech/test/quickstart.test.js | 49 ++++++++++++ storage/package.json | 7 +- storage/quickstart.js | 33 ++++---- storage/system-test/quickstart.test.js | 32 ++++---- storage/test/quickstart.test.js | 34 ++++---- translate/package.json | 7 +- translate/quickstart.js | 34 ++++---- translate/system-test/quickstart.test.js | 36 +++++---- translate/test/quickstart.test.js | 33 ++++---- vision/package.json | 5 +- vision/quickstart.js | 43 +++++++++++ vision/system-test/quickstart.test.js | 52 +++++++++++++ vision/test/quickstart.test.js | 44 +++++++++++ 54 files changed, 1432 insertions(+), 404 deletions(-) create mode 100644 dns/README.md create mode 100644 dns/package.json create mode 100644 dns/quickstart.js create mode 100644 dns/system-test/quickstart.test.js create mode 100644 dns/system-test/zones.test.js create mode 100644 dns/test/quickstart.test.js create mode 100644 dns/test/zones.test.js create mode 100644 dns/zones.js create mode 100644 language/quickstart.js create mode 100644 language/system-test/quickstart.test.js create mode 100644 language/test/quickstart.test.js create mode 100644 resource/quickstart.js create mode 100644 resource/system-test/quickstart.test.js create mode 100644 resource/test/quickstart.test.js create mode 100644 speech/quickstart.js create mode 100644 speech/system-test/quickstart.test.js create mode 100644 speech/test/quickstart.test.js create mode 100644 vision/quickstart.js create mode 100644 vision/system-test/quickstart.test.js create mode 100644 vision/test/quickstart.test.js diff --git a/README.md b/README.md index 5c2092c38a5..ea192d8b3f0 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ on Google Cloud Platform. * [Stackdriver Logging (Beta)](#stackdriver-logging-beta) * [Stackdriver Monitoring (Beta)](#stackdriver-monitoring-beta) * [Stackdriver Trace (Beta)](#stackdriver-trace-beta) + * [**Networking**](#management-tools) + * [Google Cloud DNS](#google-cloud-dns) * [Other sample applications](#other-sample-applications) * [Bookshelf tutorial app](#bookshelf-tutorial-app) * [LabelCat](#labelcat) @@ -403,6 +405,22 @@ View the [Stackdriver Trace Node.js sample][trace_sample]. [trace_docs]: https://cloud.google.com/trace/docs/ [trace_sample]: trace +### Networking + +#### Google Cloud DNS + +Publish your domain names using Google's infrastructure for production-quality, +high-volume DNS services. Google's global network of anycast name servers +provide reliable, low-latency authoritative name lookups for your domains from +anywhere in the world. Read more about [Google Cloud DNS][dns_docs]. + +[dns_docs]: https://cloud.google.com/dns/docs + +View the [Google Cloud DNS Node.js sample][dns_sample]. + +[dns_docs]: https://cloud.google.com/dns/docs/ +[dns_sample]: dns + ## Other sample applications ### Bookshelf tutorial app diff --git a/bigquery/package.json b/bigquery/package.json index 194b89d5b7a..ba22673bf94 100644 --- a/bigquery/package.json +++ b/bigquery/package.json @@ -18,5 +18,8 @@ "devDependencies": { "mocha": "^3.0.2", "node-uuid": "^1.4.7" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js index 134cd803fb5..90d00b55032 100644 --- a/bigquery/quickstart.js +++ b/bigquery/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -30,8 +32,11 @@ const datasetName = 'my_new_dataset'; // Creates the new dataset bigqueryClient.createDataset(datasetName, (err, dataset) => { - if (!err) { - // The dataset was created successfully + if (err) { + console.error(err); + return; } + + console.log(`Dataset ${dataset.name} created.`); }); // [END bigquery_quickstart] diff --git a/bigquery/system-test/quickstart.test.js b/bigquery/system-test/quickstart.test.js index 9da54b1ebc5..4436bc01bca 100644 --- a/bigquery/system-test/quickstart.test.js +++ b/bigquery/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -30,13 +32,17 @@ describe(`bigquery:quickstart`, () => { it(`should create a dataset`, (done) => { bigqueryMock = { - createDataset: (_datasetName) => { + createDataset: (_datasetName, _callback) => { assert.equal(_datasetName, datasetName); + assert.equal(typeof _callback, 'function'); bigquery.createDataset(datasetName, (err, dataset, apiResponse) => { + _callback(err, dataset, apiResponse); assert.ifError(err); assert.notEqual(dataset, undefined); assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Dataset ${dataset.name} created.`]); done(); }); } diff --git a/bigquery/test/quickstart.test.js b/bigquery/test/quickstart.test.js index 487d32208c3..dcbd68a3160 100644 --- a/bigquery/test/quickstart.test.js +++ b/bigquery/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,15 +19,16 @@ const proxyquire = require(`proxyquire`).noCallThru(); describe(`bigquery:quickstart`, () => { let bigqueryMock, BigqueryMock; + const error = new Error(`error`); before(() => { bigqueryMock = { - createDataset: sinon.stub().yields(null, {}, {}) + createDataset: sinon.stub().yields(error) }; BigqueryMock = sinon.stub().returns(bigqueryMock); }); - it(`should create a dataset`, () => { + it(`should handle error`, () => { proxyquire(`../quickstart`, { '@google-cloud/bigquery': BigqueryMock }); @@ -34,5 +37,7 @@ describe(`bigquery:quickstart`, () => { assert.deepEqual(BigqueryMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); assert.equal(bigqueryMock.createDataset.calledOnce, true); assert.deepEqual(bigqueryMock.createDataset.firstCall.args.slice(0, -1), ['my_new_dataset']); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); }); diff --git a/datastore/package.json b/datastore/package.json index 06c325ce0ee..009de7d1538 100644 --- a/datastore/package.json +++ b/datastore/package.json @@ -15,5 +15,8 @@ }, "devDependencies": { "mocha": "^3.0.2" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/datastore/quickstart.js b/datastore/quickstart.js index 01a8cf25829..3bbae17824c 100644 --- a/datastore/quickstart.js +++ b/datastore/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -34,8 +36,11 @@ const taskKey = datastoreClient.key([kind, id]); // Retrieves the entity datastoreClient.get(taskKey, (err, entity) => { - if (!err) { - // The entity was retrieved successfully + if (err) { + console.error(err); + return; } + + console.log(`Entity: ${entity.key.id}`); }); // [END datastore_quickstart] diff --git a/datastore/system-test/quickstart.test.js b/datastore/system-test/quickstart.test.js index 2eb1ac9d65c..a7ced037e6a 100644 --- a/datastore/system-test/quickstart.test.js +++ b/datastore/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -47,15 +49,19 @@ describe(`datastore:quickstart`, () => { return key; }, - get: (_key) => { + get: (_key, _callback) => { assert.equal(_key, key); + assert.equal(typeof _callback, 'function'); datastore.get(_key, (err, entity) => { + _callback(err, entity); assert.ifError(err); assert.notEqual(entity, undefined); assert.notEqual(entity.key, undefined); assert.equal(entity.key.kind, kind); assert.deepEqual(entity.data, { message: message }); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Entity: ${entity.key.id}`]); done(); }); } diff --git a/datastore/test/quickstart.test.js b/datastore/test/quickstart.test.js index a48e12caa1e..60585968172 100644 --- a/datastore/test/quickstart.test.js +++ b/datastore/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,16 +19,18 @@ const proxyquire = require(`proxyquire`).noPreserveCache(); describe(`datastore:quickstart`, () => { let datastoreMock, DatastoreMock; + const error = new Error(`error`); + const mockKey = {}; before(() => { datastoreMock = { - get: sinon.stub().yields(null, { key: 1234567890 }), - key: sinon.stub.returns(`task/1234`) + get: sinon.stub().yields(error), + key: sinon.stub().returns(mockKey) }; DatastoreMock = sinon.stub().returns(datastoreMock); }); - it(`should get a task from Datastore`, () => { + it(`should handle error`, () => { proxyquire(`../quickstart`, { '@google-cloud/datastore': DatastoreMock }); @@ -34,6 +38,8 @@ describe(`datastore:quickstart`, () => { assert.equal(DatastoreMock.calledOnce, true); assert.deepEqual(DatastoreMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); assert.equal(datastoreMock.get.calledOnce, true); - assert.deepEqual(datastoreMock.get.firstCall.args.slice(0, -1), [datastoreMock.key(['Task', 1234567890])]); + assert.deepEqual(datastoreMock.get.firstCall.args.slice(0, -1), [mockKey]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); }); diff --git a/dns/README.md b/dns/README.md new file mode 100644 index 00000000000..34ba9a67660 --- /dev/null +++ b/dns/README.md @@ -0,0 +1,50 @@ +Google Cloud Platform logo + +# Google Cloud DNS Node.js Samples + +Publish your domain names using Google's infrastructure for production-quality, +high-volume DNS services. Google's global network of anycast name servers +provide reliable, low-latency authoritative name lookups for your domains from +anywhere in the world. Read more about [Google Cloud DNS][dns_docs]. + +[dns_docs]: https://cloud.google.com/dns/docs + +## Table of Contents + +* [Setup](#setup) +* [Samples](#samples) + * [Zones](#zones) + +## Setup + +1. Read [Prerequisites][prereq] and [How to run a sample][run] first. +1. Install dependencies: + + npm install + +[prereq]: ../README.md#prerequisities +[run]: ../README.md#how-to-run-a-sample + +## Samples + +### Zones + +View the [documentation][zones_docs] or the [source code][zones_code]. + +__Usage:__ `node zones --help` + +``` +Commands: + list Lists all zones in the current project. + +Options: + --help Show help [boolean] + +Examples: + node zones list Lists all zones in the current project. + +For more information, see https://cloud.google.com/dns/docs +``` + +[zones_docs]: https://cloud.google.com/dns/docs +[zones_code]: zones.js diff --git a/dns/package.json b/dns/package.json new file mode 100644 index 00000000000..4a8aceedc36 --- /dev/null +++ b/dns/package.json @@ -0,0 +1,22 @@ +{ + "name": "nodejs-docs-samples-dns", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "scripts": { + "test": "mocha -R spec -t 120000 --require intelli-espower-loader ../test/_setup.js test/*.test.js", + "system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js" + }, + "dependencies": { + "@google-cloud/dns": "^0.2.0", + "yargs": "^6.0.0" + }, + "devDependencies": { + "mocha": "^3.1.0", + "uuid": "^2.0.3" + }, + "engines": { + "node": ">=4.3.2" + } +} diff --git a/dns/quickstart.js b/dns/quickstart.js new file mode 100644 index 00000000000..fd657ba3fc7 --- /dev/null +++ b/dns/quickstart.js @@ -0,0 +1,40 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START dns_quickstart] +// Imports the Google Cloud client library +const DNS = require('@google-cloud/dns'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const dnsClient = DNS({ + projectId: projectId +}); + +// Lists all zones in the current project +dnsClient.getZones((err, zones) => { + if (err) { + console.error(err); + return; + } + + console.log('Zones:'); + zones.forEach((zone) => console.log(zone.name)); +}); +// [END dns_quickstart] diff --git a/dns/system-test/quickstart.test.js b/dns/system-test/quickstart.test.js new file mode 100644 index 00000000000..6bda2b4c668 --- /dev/null +++ b/dns/system-test/quickstart.test.js @@ -0,0 +1,67 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noPreserveCache(); +const dns = proxyquire(`@google-cloud/dns`, {})(); + +const uuid = require(`uuid`); +const zoneName = `test-${uuid().substr(0, 13)}`; + +describe(`dns:quickstart`, () => { + let dnsMock, DNSMock; + + before((done) => { + dns.createZone(zoneName, { + dnsName: `${process.env.GCLOUD_PROJECT}.appspot.com.` + }, done); + }); + + after((done) => { + dns.zone(zoneName).delete(() => { + // Ignore error + done(); + }); + }); + + it(`should list zones`, (done) => { + dnsMock = { + getZones: (_callback) => { + assert.equal(typeof _callback, 'function'); + + // Listing is eventually consistent, give the indexes time to update + setTimeout(() => { + dns.getZones((err, zones) => { + _callback(err, zones); + assert.ifError(err); + assert.equal(Array.isArray(zones), true); + assert.equal(console.log.called, true); + assert.deepEqual(console.log.firstCall.args, [`Zones:`]); + zones.forEach((zone, i) => { + assert.deepEqual(console.log.getCall(i + 1).args, [zone.name]); + }); + done(); + }); + }, 5000); + } + }; + DNSMock = sinon.stub().returns(dnsMock); + + proxyquire(`../quickstart`, { + '@google-cloud/dns': DNSMock + }); + }); +}); diff --git a/dns/system-test/zones.test.js b/dns/system-test/zones.test.js new file mode 100644 index 00000000000..c85caedbc06 --- /dev/null +++ b/dns/system-test/zones.test.js @@ -0,0 +1,52 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const path = require(`path`); +const run = require(`../../utils`).run; +const dns = require(`@google-cloud/dns`)(); +const uuid = require(`uuid`); + +const zoneName = `test-${uuid().substr(0, 13)}`; +const cwd = path.join(__dirname, `..`); +const cmd = `node zones.js`; + +describe(`dns:zones`, () => { + before((done) => { + dns.createZone(zoneName, { + dnsName: `${process.env.GCLOUD_PROJECT}.appspot.com.` + }, done); + }); + + after((done) => { + dns.zone(zoneName).delete(() => { + dns.zone('my-awesome-zone').delete(() => { + // Ignore error + done(); + }); + }); + }); + + it(`should list zones`, (done) => { + // Listing is eventually consistent, give the indexes time to update + setTimeout(() => { + const output = run(`${cmd} list`, cwd); + assert.notEqual(output.indexOf(`Zones:`), -1); + assert.notEqual(output.indexOf(zoneName), -1); + done(); + }, 5000); + }); +}); diff --git a/dns/test/quickstart.test.js b/dns/test/quickstart.test.js new file mode 100644 index 00000000000..cbceb9ecb2d --- /dev/null +++ b/dns/test/quickstart.test.js @@ -0,0 +1,43 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`dns:quickstart`, () => { + let dnsMock, DNSMock; + const error = new Error(`error`); + + before(() => { + dnsMock = { + getZones: sinon.stub().yields(error) + }; + DNSMock = sinon.stub().returns(dnsMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/dns': DNSMock + }); + + assert.equal(DNSMock.calledOnce, true); + assert.deepEqual(DNSMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(dnsMock.getZones.calledOnce, true); + assert.deepEqual(dnsMock.getZones.firstCall.args.slice(0, -1), []); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +}); diff --git a/dns/test/zones.test.js b/dns/test/zones.test.js new file mode 100644 index 00000000000..0ac1e8a1490 --- /dev/null +++ b/dns/test/zones.test.js @@ -0,0 +1,37 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`dns:zones`, () => { + it(`should handle errors`, () => { + const error = new Error(`error`); + const callback = sinon.spy(); + const dnsMock = { + getZones: sinon.stub().yields(error) + }; + const DNSMock = sinon.stub().returns(dnsMock); + const program = proxyquire(`../zones`, { + '@google-cloud/dns': DNSMock + }); + + program.listZones(callback); + + assert.equal(callback.callCount, 1); + assert.equal(callback.alwaysCalledWithExactly(error), true); + }); +}); diff --git a/dns/zones.js b/dns/zones.js new file mode 100644 index 00000000000..f36e3514b91 --- /dev/null +++ b/dns/zones.js @@ -0,0 +1,63 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const DNS = require('@google-cloud/dns'); + +// [START dns_list_zones] +function listZones (callback) { + // Instantiates a client + const dns = DNS(); + + // Lists all zones in the current project + dns.getZones((err, zones) => { + if (err) { + callback(err); + return; + } + + console.log('Zones:'); + zones.forEach((zone) => console.log(zone.name)); + callback(); + }); +} +// [END dns_list_zones] + +// The command-line program +const cli = require('yargs'); +const makeHandler = require('../utils').makeHandler; + +const program = module.exports = { + listZones: listZones, + main: (args) => { + // Run the command-line program + cli.help().strict().parse(args).argv; + } +}; + +cli + .demand(1) + .command('list', 'Lists all zones in the current project.', {}, () => { + program.listZones(makeHandler(false)); + }) + .example('node $0 list', 'Lists all zones in the current project.') + .wrap(120) + .recommendCommands() + .epilogue('For more information, see https://cloud.google.com/dns/docs'); + +if (module === require.main) { + program.main(process.argv.slice(2)); +} diff --git a/language/package.json b/language/package.json index 284be2a837e..c5ac342fd00 100644 --- a/language/package.json +++ b/language/package.json @@ -16,5 +16,8 @@ "devDependencies": { "mocha": "^3.0.2", "node-uuid": "^1.4.7" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/language/quickstart.js b/language/quickstart.js new file mode 100644 index 00000000000..ac595aa563b --- /dev/null +++ b/language/quickstart.js @@ -0,0 +1,43 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START language_quickstart] +// Imports the Google Cloud client library +const Language = require('@google-cloud/language'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const languageClient = Language({ + projectId: projectId +}); + +// The text to analyze +const text = 'Hello, world!'; + +// Detects the sentiment of the text +languageClient.detectSentiment(text, { verbose: true }, (err, sentiment) => { + if (err) { + console.error(err); + return; + } + + console.log('Text: %s', text); + console.log('Sentiment: %j', sentiment); +}); +// [END language_quickstart] diff --git a/language/system-test/quickstart.test.js b/language/system-test/quickstart.test.js new file mode 100644 index 00000000000..6c68b794007 --- /dev/null +++ b/language/system-test/quickstart.test.js @@ -0,0 +1,51 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noPreserveCache(); +const language = proxyquire(`@google-cloud/language`, {})(); + +describe(`language:quickstart`, () => { + let languageMock, LanguageMock; + + it(`should detect sentiment`, (done) => { + const expectedText = `Hello, world!`; + + languageMock = { + detectSentiment: (_text, _config, _callback) => { + assert.equal(_text, expectedText); + assert.deepEqual(_config, { verbose: true }); + assert.equal(typeof _callback, 'function'); + + language.detectSentiment(_text, _config, (err, sentiment, apiResponse) => { + _callback(err, sentiment, apiResponse); + assert.ifError(err); + assert.equal(typeof sentiment, 'object'); + assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledTwice, true); + assert.deepEqual(console.log.firstCall.args, [`Text: %s`, expectedText]); + assert.deepEqual(console.log.secondCall.args, [`Sentiment: %j`, sentiment]); + done(); + }); + } + }; + LanguageMock = sinon.stub().returns(languageMock); + + proxyquire(`../quickstart`, { + '@google-cloud/language': LanguageMock + }); + }); +}); diff --git a/language/test/quickstart.test.js b/language/test/quickstart.test.js new file mode 100644 index 00000000000..df0519c73c0 --- /dev/null +++ b/language/test/quickstart.test.js @@ -0,0 +1,44 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`language:quickstart`, () => { + let languageMock, LanguageMock; + const error = new Error(`error`); + const text = 'Hello, world!'; + + before(() => { + languageMock = { + detectSentiment: sinon.stub().yields(error) + }; + LanguageMock = sinon.stub().returns(languageMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/language': LanguageMock + }); + + assert.equal(LanguageMock.calledOnce, true); + assert.deepEqual(LanguageMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(languageMock.detectSentiment.calledOnce, true); + assert.deepEqual(languageMock.detectSentiment.firstCall.args.slice(0, -1), [text, { verbose: true }]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +}); diff --git a/logging/package.json b/logging/package.json index d09a24477d2..83b5de5b0e4 100644 --- a/logging/package.json +++ b/logging/package.json @@ -18,5 +18,8 @@ "devDependencies": { "mocha": "^3.0.2", "node-uuid": "^1.4.7" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/logging/quickstart.js b/logging/quickstart.js index 435aa28c04f..84a1ab5f521 100644 --- a/logging/quickstart.js +++ b/logging/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -39,8 +41,11 @@ const entry = log.entry(resource, text); // Writes the log entry log.write(entry, (err) => { - if (!err) { - // The entry was logged successfully + if (err) { + console.error(err); + return; } + + console.log(`Logged: ${text}`); }); // [END logging_quickstart] diff --git a/logging/system-test/quickstart.test.js b/logging/system-test/quickstart.test.js index 179493c449d..f942f7a0edb 100644 --- a/logging/system-test/quickstart.test.js +++ b/logging/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -34,16 +36,20 @@ describe(`logging:quickstart`, () => { logMock = { entry: sinon.stub().returns({}), - write: (_entry) => { + write: (_entry, _callback) => { assert.deepEqual(_entry, {}); + assert.equal(typeof _callback, 'function'); const log = logging.log(logName); - const entry = log.entry({ type: `global` }, `Hello, world!`); + const text = `Hello, world!`; + const entry = log.entry({ type: `global` }, text); log.write(entry, (err, apiResponse) => { + _callback(err, apiResponse); assert.ifError(err); assert.notEqual(apiResponse, undefined); - // Logs are eventually consistent - setTimeout(done, 5000); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Logged: ${text}`]); + done(); }); } }; diff --git a/logging/test/quickstart.test.js b/logging/test/quickstart.test.js index c5f4a7b9534..2e9a8124f33 100644 --- a/logging/test/quickstart.test.js +++ b/logging/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,7 +19,7 @@ const proxyquire = require(`proxyquire`).noCallThru(); describe(`logging:quickstart`, () => { let logMock, loggingMock, LoggingMock; - + const error = new Error(`error`); const expectedLogName = `my-log`; const expectedResource = { type: `global` }; const expectedMessage = `Hello, world!`; @@ -25,7 +27,7 @@ describe(`logging:quickstart`, () => { before(() => { logMock = { entry: sinon.stub().returns({}), - write: sinon.stub().yields(null, {}) + write: sinon.stub().yields(error) }; loggingMock = { log: sinon.stub().returns(logMock) @@ -46,5 +48,7 @@ describe(`logging:quickstart`, () => { assert.deepEqual(logMock.entry.firstCall.args, [expectedResource, expectedMessage]); assert.equal(logMock.write.calledOnce, true); assert.deepEqual(logMock.write.firstCall.args.slice(0, -1), [{}]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); }); diff --git a/pubsub/package.json b/pubsub/package.json index af9187367d0..17c1001dd49 100644 --- a/pubsub/package.json +++ b/pubsub/package.json @@ -18,6 +18,6 @@ "node-uuid": "^1.4.7" }, "engines": { - "node": "~4.5" + "node": ">=4.3.2" } } diff --git a/pubsub/quickstart.js b/pubsub/quickstart.js index 0ba215fb2d5..6f4f44e970a 100644 --- a/pubsub/quickstart.js +++ b/pubsub/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -30,8 +32,11 @@ const topicName = 'my-new-topic'; // Creates the new topic pubsubClient.createTopic(topicName, (err, topic) => { - if (!err) { - // The topic was created successfully + if (err) { + console.error(err); + return; } + + console.log(`Topic ${topic.name} created.`); }); // [END pubsub_quickstart] diff --git a/pubsub/system-test/quickstart.test.js b/pubsub/system-test/quickstart.test.js index 858b6abe54d..dffc6589ce1 100644 --- a/pubsub/system-test/quickstart.test.js +++ b/pubsub/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -35,14 +37,18 @@ describe(`pubsub:quickstart`, () => { const expectedTopicName = `my-new-topic`; pubsubMock = { - createTopic: (_topicName) => { + createTopic: (_topicName, _callback) => { assert.equal(_topicName, expectedTopicName); + assert.equal(typeof _callback, 'function'); pubsub.createTopic(topicName, (err, topic, apiResponse) => { + _callback(err, topic, apiResponse); assert.ifError(err); assert.notEqual(topic, undefined); assert.equal(topic.name, fullTopicName); assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Topic ${topic.name} created.`]); done(); }); } diff --git a/pubsub/test/quickstart.test.js b/pubsub/test/quickstart.test.js index 9bd1f639b6c..45c9dec7ccc 100644 --- a/pubsub/test/quickstart.test.js +++ b/pubsub/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,17 +19,17 @@ const proxyquire = require(`proxyquire`).noCallThru(); describe(`pubsub:quickstart`, () => { let pubsubMock, PubSubMock; - + const error = new Error(`error`); const expectedTopicName = `my-new-topic`; before(() => { pubsubMock = { - createTopic: sinon.stub().yields(null, {}, {}) + createTopic: sinon.stub().yields(error) }; PubSubMock = sinon.stub().returns(pubsubMock); }); - it(`should create a topic`, () => { + it(`should handle error`, () => { proxyquire(`../quickstart`, { '@google-cloud/pubsub': PubSubMock }); @@ -36,5 +38,7 @@ describe(`pubsub:quickstart`, () => { assert.deepEqual(PubSubMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); assert.equal(pubsubMock.createTopic.calledOnce, true); assert.deepEqual(pubsubMock.createTopic.firstCall.args.slice(0, -1), [expectedTopicName]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); }); diff --git a/resource/package.json b/resource/package.json index 5cc68e45a65..e067a12bbd2 100644 --- a/resource/package.json +++ b/resource/package.json @@ -9,11 +9,13 @@ "system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js" }, "dependencies": { - "@google-cloud/resource": "^0.1.1", - "request": "^2.74.0", - "yargs": "^5.0.0" + "@google-cloud/resource": "^0.2.0", + "yargs": "^6.0.0" }, "devDependencies": { - "mocha": "^3.0.2" + "mocha": "^3.1.0" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/resource/projects.js b/resource/projects.js index 6d6b17d01ac..53c5e727612 100644 --- a/resource/projects.js +++ b/resource/projects.js @@ -1,57 +1,48 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; -// [START all] -// [START setup] -// By default, the client will authenticate using the service account file -// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use -// the project specified by the GCLOUD_PROJECT environment variable. See -// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication -var Resource = require('@google-cloud/resource'); - -// Instantiate a resource client -var resource = Resource(); -// [END setup] +const Resource = require('@google-cloud/resource'); -// [START list_projects] -/** - * List all projects the authenticated user has access to. - * - * @param {function} callback The callback function. - */ +// [START resource_list_projects] function listProjects (callback) { - // See https://googlecloudplatform.github.io/gcloud-node/#/docs/resource/latest/resource - resource.getProjects(function (err, projects) { + // Instantiates a client + const resource = Resource(); + + // Lists all current projects + resource.getProjects((err, projects) => { if (err) { - return callback(err); + callback(err); + return; } - console.log('Found %d project(s)!', projects.length); - return callback(null, projects); + console.log('Projects:'); + projects.forEach((project) => console.log(project.id)); + callback(); }); } -// [END list_projects] -// [END all] +// [END resource_list_projects] // The command-line program -var cli = require('yargs'); -var makeHandler = require('../utils').makeHandler; +const cli = require(`yargs`); +const makeHandler = require(`../utils`).makeHandler; -var program = module.exports = { +const program = module.exports = { listProjects: listProjects, - main: function (args) { + main: (args) => { // Run the command-line program cli.help().strict().parse(args).argv; } @@ -59,13 +50,13 @@ var program = module.exports = { cli .demand(1) - .command('list', 'List all projects the authenticated user has access to.', {}, function () { - program.listProjects(makeHandler(true, 'id')); + .command(`list`, `List all current projects.`, {}, () => { + program.listProjects(makeHandler(false)); }) - .example('node $0 list', 'List projects.') - .wrap(80) + .example(`node $0 list`, `Lists all current projects.`) + .wrap(120) .recommendCommands() - .epilogue('For more information, see https://cloud.google.com/resource-manager/docs/'); + .epilogue(`For more information, see https://cloud.google.com/resource-manager/docs`); if (module === require.main) { program.main(process.argv.slice(2)); diff --git a/resource/quickstart.js b/resource/quickstart.js new file mode 100644 index 00000000000..85f06e5cda2 --- /dev/null +++ b/resource/quickstart.js @@ -0,0 +1,40 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START resource_quickstart] +// Imports the Google Cloud client library +const Resource = require('@google-cloud/resource'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const resourceClient = Resource({ + projectId: projectId +}); + +// Lists current projects +resourceClient.getProjects((err, projects) => { + if (err) { + console.error(err); + return; + } + + console.log('Projects:'); + projects.forEach((project) => console.log(project.id)); +}); +// [END resource_quickstart] diff --git a/resource/system-test/projects.test.js b/resource/system-test/projects.test.js index 9d39c209151..36bac853d1c 100644 --- a/resource/system-test/projects.test.js +++ b/resource/system-test/projects.test.js @@ -1,30 +1,30 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; -var program = require('../projects'); +const path = require(`path`); +const run = require(`../../utils`).run; -describe('resource:projects', function () { - describe('list', function () { - it('should list projects', function (done) { - program.listProjects(function (err, projects) { - assert.ifError(err); - assert(Array.isArray(projects)); - assert(projects.length > 0); - assert(console.log.calledWith('Found %d project(s)!', projects.length)); - setTimeout(done, 2000); - }); - }); +const cwd = path.join(__dirname, `..`); +const cmd = `node projects.js`; + +describe(`resource:projects`, () => { + it(`should list projects`, () => { + const output = run(`${cmd} list`, cwd); + assert.notEqual(output.indexOf(`Projects:`), -1); + assert.notEqual(output.indexOf(`${process.env.GCLOUD_PROJECT}`), -1); }); }); diff --git a/resource/system-test/quickstart.test.js b/resource/system-test/quickstart.test.js new file mode 100644 index 00000000000..ccbea5ea0af --- /dev/null +++ b/resource/system-test/quickstart.test.js @@ -0,0 +1,48 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noPreserveCache(); +const resource = proxyquire(`@google-cloud/resource`, {})(); + +describe(`resource:quickstart`, () => { + let resourceMock, ResourceMock; + + it(`should list projects`, (done) => { + resourceMock = { + getProjects: (_callback) => { + assert.equal(typeof _callback, 'function'); + + resource.getProjects((err, projects) => { + _callback(err, projects); + assert.ifError(err); + assert.equal(Array.isArray(projects), true); + assert.equal(console.log.called, true); + assert.deepEqual(console.log.firstCall.args, [`Projects:`]); + projects.forEach((project, i) => { + assert.deepEqual(console.log.getCall(i + 1).args, [project.id]); + }); + done(); + }); + } + }; + ResourceMock = sinon.stub().returns(resourceMock); + + proxyquire(`../quickstart`, { + '@google-cloud/resource': ResourceMock + }); + }); +}); diff --git a/resource/test/projects.test.js b/resource/test/projects.test.js index 8706023c119..89d4422fdd3 100644 --- a/resource/test/projects.test.js +++ b/resource/test/projects.test.js @@ -1,77 +1,37 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; -var proxyquire = require('proxyquire').noCallThru(); - -function getSample () { - var projectsMock = [{ id: 'foo' }]; - var resourceMock = { - getProjects: sinon.stub().yields(null, projectsMock, null, projectsMock) - }; - var ResourceMock = sinon.stub().returns(resourceMock); - - return { - program: proxyquire('../projects', { - '@google-cloud/resource': ResourceMock, - yargs: proxyquire('yargs', {}) - }), - mocks: { - Resource: ResourceMock, - resource: resourceMock, - projects: projectsMock - } - }; -} - -describe('resource:projects', function () { - describe('listProjects', function () { - it('should list projects', function () { - var sample = getSample(); - var callback = sinon.stub(); - - sample.program.listProjects(callback); - - assert.equal(sample.mocks.resource.getProjects.calledOnce, true); - assert.deepEqual(sample.mocks.resource.getProjects.firstCall.args.slice(0, -1), []); - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [null, sample.mocks.projects]); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Found %d project(s)!', sample.mocks.projects.length]); +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`resource:projects`, () => { + it(`should handle errors`, () => { + const error = new Error(`error`); + const callback = sinon.spy(); + const resourceMock = { + getProjects: sinon.stub().yields(error) + }; + const ResourceMocket = sinon.stub().returns(resourceMock); + const program = proxyquire(`../projects`, { + '@google-cloud/resource': ResourceMocket }); - it('should handle error', function () { - var error = new Error('error'); - var sample = getSample(); - var callback = sinon.stub(); - sample.mocks.resource.getProjects.yields(error); - - sample.program.listProjects(callback); + program.listProjects(callback); - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [error]); - }); - }); - - describe('main', function () { - it('should call listProjects', function () { - var program = getSample().program; - - sinon.stub(program, 'listProjects'); - program.main(['list']); - assert.equal(program.listProjects.calledOnce, true); - assert.deepEqual(program.listProjects.firstCall.args.slice(0, -1), []); - }); + assert.equal(callback.callCount, 1); + assert.equal(callback.alwaysCalledWithExactly(error), true); }); }); diff --git a/resource/test/quickstart.test.js b/resource/test/quickstart.test.js new file mode 100644 index 00000000000..073572e4f57 --- /dev/null +++ b/resource/test/quickstart.test.js @@ -0,0 +1,43 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`resource:quickstart`, () => { + let resourceMock, ResourceMock; + const error = new Error(`error`); + + before(() => { + resourceMock = { + getProjects: sinon.stub().yields(error) + }; + ResourceMock = sinon.stub().returns(resourceMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/resource': ResourceMock + }); + + assert.equal(ResourceMock.calledOnce, true); + assert.deepEqual(ResourceMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(resourceMock.getProjects.calledOnce, true); + assert.deepEqual(resourceMock.getProjects.firstCall.args.slice(0, -1), []); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +}); diff --git a/scripts/install b/scripts/install index bdf61cd1b31..650d950654b 100755 --- a/scripts/install +++ b/scripts/install @@ -28,6 +28,7 @@ queue.push('bigquery'); queue.push('computeengine'); queue.push('datastore'); queue.push('debugger'); +queue.push('dns'); queueDirectories('functions'); queue.push('functions/ocr/app'); queue.push('language'); diff --git a/scripts/uninstall b/scripts/uninstall index 03e768c5a4e..af90f447f20 100755 --- a/scripts/uninstall +++ b/scripts/uninstall @@ -28,6 +28,7 @@ queue.push('bigquery'); queue.push('computeengine'); queue.push('datastore'); queue.push('debugger'); +queue.push('dns'); queueDirectories('functions'); queue.push('functions/ocr/app'); queue.push('language'); diff --git a/speech/package.json b/speech/package.json index 37cafd9fdd6..b2a31de6791 100644 --- a/speech/package.json +++ b/speech/package.json @@ -11,10 +11,10 @@ "dependencies": { "@google-cloud/speech": "^0.1.1", "node-record-lpcm16": "^0.1.4", - "yargs": "^5.0.0" + "yargs": "^6.0.0" }, "devDependencies": { - "mocha": "^3.0.2" + "mocha": "^3.1.0" }, "engines": { "node": ">=4.3.2" diff --git a/speech/quickstart.js b/speech/quickstart.js new file mode 100644 index 00000000000..fa28527ca30 --- /dev/null +++ b/speech/quickstart.js @@ -0,0 +1,48 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START speech_quickstart] +// Imports the Google Cloud client library +const Speech = require('@google-cloud/speech'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const speechClient = Speech({ + projectId: projectId +}); + +// The name of the audio file to transcribe +const fileName = './resources/audio.raw'; + +// The audio file's encoding and sample rate +const options = { + encoding: 'LINEAR16', + sampleRate: 16000 +}; + +// Detects speech in the audio file +speechClient.recognize(fileName, options, (err, result) => { + if (err) { + console.error(err); + return; + } + + console.log(`Transcription: ${result}`); +}); +// [END speech_quickstart] diff --git a/speech/system-test/quickstart.test.js b/speech/system-test/quickstart.test.js new file mode 100644 index 00000000000..1dbc4b5af18 --- /dev/null +++ b/speech/system-test/quickstart.test.js @@ -0,0 +1,58 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const path = require(`path`); +const proxyquire = require(`proxyquire`).noPreserveCache(); +const speech = proxyquire(`@google-cloud/speech`, {})(); + +const fileName = path.join(__dirname, `../resources/audio.raw`); +const config = { + encoding: `LINEAR16`, + sampleRate: 16000 +}; + +describe(`speech:quickstart`, () => { + let speechMock, SpeechMock; + + it(`should detect speech`, (done) => { + const expectedFileName = `./resources/audio.raw`; + const expectedText = `how old is the Brooklyn Bridge`; + + speechMock = { + recognize: (_fileName, _config, _callback) => { + assert.equal(_fileName, expectedFileName); + assert.deepEqual(_config, config); + assert.equal(typeof _callback, `function`); + + speech.recognize(fileName, config, (err, transcription, apiResponse) => { + _callback(err, transcription, apiResponse); + assert.ifError(err); + assert.equal(transcription, expectedText); + assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Transcription: ${expectedText}`]); + done(); + }); + } + }; + SpeechMock = sinon.stub().returns(speechMock); + + proxyquire(`../quickstart`, { + '@google-cloud/speech': SpeechMock + }); + }); +}); diff --git a/speech/test/quickstart.test.js b/speech/test/quickstart.test.js new file mode 100644 index 00000000000..807e23e507f --- /dev/null +++ b/speech/test/quickstart.test.js @@ -0,0 +1,49 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +const config = { + encoding: 'LINEAR16', + sampleRate: 16000 +}; + +describe(`speech:quickstart`, () => { + let speechMock, SpeechMock; + const error = new Error(`error`); + const fileName = `./resources/audio.raw`; + + before(() => { + speechMock = { + recognize: sinon.stub().yields(error) + }; + SpeechMock = sinon.stub().returns(speechMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/speech': SpeechMock + }); + + assert.equal(SpeechMock.calledOnce, true); + assert.deepEqual(SpeechMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(speechMock.recognize.calledOnce, true); + assert.deepEqual(speechMock.recognize.firstCall.args.slice(0, -1), [fileName, config]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +}); diff --git a/storage/package.json b/storage/package.json index 2a705e0460d..d13d9b8eb95 100644 --- a/storage/package.json +++ b/storage/package.json @@ -12,10 +12,13 @@ "@google-cloud/storage": "^0.1.1", "googleapis": "^12.2.0", "moment": "^2.14.1", - "yargs": "^5.0.0" + "yargs": "^6.0.0" }, "devDependencies": { - "mocha": "^3.0.2", + "mocha": "^3.1.0", "node-uuid": "^1.4.7" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/storage/quickstart.js b/storage/quickstart.js index 8a6633a3310..dca56ed1c9a 100644 --- a/storage/quickstart.js +++ b/storage/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -30,8 +32,11 @@ const bucketName = 'my-new-bucket'; // Creates the new bucket storageClient.createBucket(bucketName, (err, bucket) => { - if (!err) { - // The bucket was created successfully + if (err) { + console.error(err); + return; } + + console.log(`Bucket ${bucket.name} created.`); }); // [END storage_quickstart] diff --git a/storage/system-test/quickstart.test.js b/storage/system-test/quickstart.test.js index bf2a022ffc7..cbf1069756e 100644 --- a/storage/system-test/quickstart.test.js +++ b/storage/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -33,14 +35,18 @@ describe(`storage:quickstart`, () => { const expectedBucketName = `my-new-bucket`; storageMock = { - createBucket: (_bucketName) => { + createBucket: (_bucketName, _callback) => { assert.equal(_bucketName, expectedBucketName); + assert.equal(typeof _callback, 'function'); storage.createBucket(bucketName, (err, bucket, apiResponse) => { + _callback(err, bucket, apiResponse); assert.ifError(err); assert.notEqual(bucket, undefined); assert.equal(bucket.name, bucketName); assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, [`Bucket ${bucket.name} created.`]); done(); }); } diff --git a/storage/test/quickstart.test.js b/storage/test/quickstart.test.js index 7e37dd84e5b..c929f78dcaa 100644 --- a/storage/test/quickstart.test.js +++ b/storage/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,17 +19,17 @@ const proxyquire = require(`proxyquire`).noCallThru(); describe(`storage:quickstart`, () => { let storageMock, StorageMock; - + const error = new Error(`error`); const expectedBucketName = `my-new-bucket`; before(() => { storageMock = { - createBucket: sinon.stub().yields(null, {}, {}) + createBucket: sinon.stub().yields(error) }; StorageMock = sinon.stub().returns(storageMock); }); - it(`should create a bucket`, () => { + it(`should handle error`, () => { proxyquire(`../quickstart`, { '@google-cloud/storage': StorageMock }); @@ -36,5 +38,7 @@ describe(`storage:quickstart`, () => { assert.deepEqual(StorageMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); assert.equal(storageMock.createBucket.calledOnce, true); assert.deepEqual(storageMock.createBucket.firstCall.args.slice(0, -1), [expectedBucketName]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); }); diff --git a/translate/package.json b/translate/package.json index 53120d7e1c5..8c7dca14199 100644 --- a/translate/package.json +++ b/translate/package.json @@ -11,9 +11,12 @@ "dependencies": { "@google-cloud/translate": "^0.2.0", "iso-639-1": "^1.2.1", - "yargs": "^5.0.0" + "yargs": "^6.0.0" }, "devDependencies": { - "mocha": "^3.0.2" + "mocha": "^3.1.0" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/translate/quickstart.js b/translate/quickstart.js index e3f49aedf66..2ed38c9ba57 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -32,8 +34,12 @@ const target = 'ru'; // Translates some text into Russian translateClient.translate(text, target, (err, translation) => { - if (!err) { - // The text was translated successfully + if (err) { + console.error(err); + return; } + + console.log(`Text: ${text}`); + console.log(`Translation: ${translation}`); }); // [END translate_quickstart] diff --git a/translate/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js index 3936592c5b9..4580e77a7fa 100644 --- a/translate/system-test/quickstart.test.js +++ b/translate/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -18,6 +20,7 @@ const translate = proxyquire(`@google-cloud/translate`, {})({ key: process.env.TRANSLATE_API_KEY }); const string = `Hello, world!`; +const translated = `Привет мир!`; const targetLanguage = `ru`; describe(`translate:quickstart`, () => { @@ -25,14 +28,19 @@ describe(`translate:quickstart`, () => { it(`should translate a string`, (done) => { translateMock = { - translate: (_string, _targetLanguage) => { + translate: (_string, _targetLanguage, _callback) => { assert.equal(_string, string); assert.equal(_targetLanguage, targetLanguage); + assert.equal(typeof _callback, 'function'); translate.translate(_string, _targetLanguage, (err, translation, apiResponse) => { + _callback(err, translation, apiResponse); assert.ifError(err); - assert.equal(translation, `Привет мир!`); + assert.equal(translation, translated); assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledTwice, true); + assert.deepEqual(console.log.firstCall.args, [`Text: ${string}`]); + assert.deepEqual(console.log.secondCall.args, [`Translation: ${translated}`]); done(); }); } diff --git a/translate/test/quickstart.test.js b/translate/test/quickstart.test.js index 85c20267b68..e9c3dd683d2 100644 --- a/translate/test/quickstart.test.js +++ b/translate/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,15 +19,16 @@ const proxyquire = require(`proxyquire`).noCallThru(); describe(`translate:quickstart`, () => { let translateMock, TranslateMock; + const error = new Error(`error`); before(() => { translateMock = { - translate: sinon.stub().yields(null, `Привет мир!`, {}) + translate: sinon.stub().yields(error) }; TranslateMock = sinon.stub().returns(translateMock); }); - it(`should translate a string`, () => { + it(`should handle error`, () => { proxyquire(`../quickstart`, { '@google-cloud/translate': TranslateMock }); @@ -34,5 +37,7 @@ describe(`translate:quickstart`, () => { assert.deepEqual(TranslateMock.firstCall.args, [{ key: 'YOUR_API_KEY' }]); assert.equal(translateMock.translate.calledOnce, true); assert.deepEqual(translateMock.translate.firstCall.args.slice(0, -1), ['Hello, world!', 'ru']); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); }); diff --git a/vision/package.json b/vision/package.json index dec58f3fd21..f57724adaa1 100644 --- a/vision/package.json +++ b/vision/package.json @@ -15,9 +15,12 @@ "redis": "^2.6.0-2" }, "devDependencies": { - "mocha": "^2.5.3" + "mocha": "^3.1.0" }, "optionalDependencies": { "canvas": "^1.3.15" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/vision/quickstart.js b/vision/quickstart.js new file mode 100644 index 00000000000..fafa46b1a27 --- /dev/null +++ b/vision/quickstart.js @@ -0,0 +1,43 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START vision_quickstart] +// Imports the Google Cloud client library +const Vision = require('@google-cloud/vision'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const visionClient = Vision({ + projectId: projectId +}); + +// The name of the image file to annotate +const fileName = './resources/wakeupcat.jpg'; + +// Performs label detection on the image file +visionClient.detectLabels(fileName, (err, labels) => { + if (err) { + console.error(err); + return; + } + + console.log('Labels:'); + labels.forEach((label) => console.log(label)); +}); +// [END vision_quickstart] diff --git a/vision/system-test/quickstart.test.js b/vision/system-test/quickstart.test.js new file mode 100644 index 00000000000..a5ff0fb6f8c --- /dev/null +++ b/vision/system-test/quickstart.test.js @@ -0,0 +1,52 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noPreserveCache(); +const vision = proxyquire(`@google-cloud/vision`, {})(); + +describe(`vision:quickstart`, () => { + let visionMock, VisionMock; + + it(`should detect labels`, (done) => { + const expectedFileName = `./resources/wakeupcat.jpg`; + + visionMock = { + detectLabels: (_fileName, _callback) => { + assert.equal(_fileName, expectedFileName); + assert.equal(typeof _callback, 'function'); + + vision.detectLabels(_fileName, (err, labels, apiResponse) => { + _callback(err, labels, apiResponse); + assert.ifError(err); + assert.equal(Array.isArray(labels), true); + assert.notEqual(apiResponse, undefined); + assert.equal(console.log.called, true); + assert.deepEqual(console.log.firstCall.args, [`Labels:`]); + labels.forEach((label, i) => { + assert.deepEqual(console.log.getCall(i + 1).args, [label]); + }); + done(); + }); + } + }; + VisionMock = sinon.stub().returns(visionMock); + + proxyquire(`../quickstart`, { + '@google-cloud/vision': VisionMock + }); + }); +}); diff --git a/vision/test/quickstart.test.js b/vision/test/quickstart.test.js new file mode 100644 index 00000000000..9a75af8f179 --- /dev/null +++ b/vision/test/quickstart.test.js @@ -0,0 +1,44 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`vision:quickstart`, () => { + let visionMock, VisionMock; + const error = new Error(`error`); + const fileName = `./resources/wakeupcat.jpg`; + + before(() => { + visionMock = { + detectLabels: sinon.stub().yields(error) + }; + VisionMock = sinon.stub().returns(visionMock); + }); + + it(`should handle error`, () => { + proxyquire(`../quickstart`, { + '@google-cloud/vision': VisionMock + }); + + assert.equal(VisionMock.calledOnce, true); + assert.deepEqual(VisionMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); + assert.equal(visionMock.detectLabels.calledOnce, true); + assert.deepEqual(visionMock.detectLabels.firstCall.args.slice(0, -1), [fileName]); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); + }); +});