From 858ebab55ef54e971d96a4337b6ffabb06e289a8 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Mon, 28 Mar 2016 16:14:37 -0700 Subject: [PATCH] Refactor for completely parallel tests. --- .gitignore | 3 +- .travis.yml | 2 +- README.md | 15 +- logging/export.js | 95 +++++--- logging/list.js | 37 +-- logging/package.json | 2 +- logging/write.js | 74 +++--- package.json | 27 ++- prediction/README.md | 2 +- prediction/hostedmodels.js | 40 ++-- prediction/package.json | 2 +- pubsub/iam.js | 191 ++++++++------- pubsub/subscription.js | 334 +++++++++++++++++---------- storage/README.md | 2 +- storage/authSample.js | 36 ++- storage/package.json | 2 +- test/.jshintrc | 4 - test/appengine/all.test.js | 132 +++++------ test/computeengine/sendgrid.test.js | 38 ++- test/datastore/.jshintrc | 4 + test/functions/helloworld.test.js | 21 +- test/functions/log.test.js | 119 ++++------ test/functions/message.test.js | 69 +++--- test/functions/module.test.js | 21 +- test/functions/uuid.test.js | 23 +- test/logging/export.test.js | 20 +- test/logging/list.test.js | 31 +-- test/logging/write.test.js | 28 +-- test/prediction/hostedmodels.test.js | 30 +-- test/pubsub/iam.test.js | 66 ++---- test/pubsub/subscription.test.js | 66 ++---- test/storage/authSample.test.js | 31 +-- 32 files changed, 798 insertions(+), 769 deletions(-) delete mode 100644 test/.jshintrc create mode 100644 test/datastore/.jshintrc diff --git a/.gitignore b/.gitignore index ff904281765..e8751f42d46 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ test/encrypted/nodejs-docs-samples.json dump.rdb logs/ *.iml -.idea/ \ No newline at end of file +.idea/ +.nyc_output \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index de617eb4999..6372f18a286 100644 --- a/.travis.yml +++ b/.travis.yml @@ -83,4 +83,4 @@ before_install: - npm set progress=false after_success: - - npm run coveralls + - npm run report diff --git a/README.md b/README.md index 910d9379942..e4fb314c2cd 100644 --- a/README.md +++ b/README.md @@ -129,30 +129,29 @@ a service account file. You can download one from your Google project's "permissions" page. 1. `npm test` -Since the tests use [Mocha.js](https://mochajs.org/), you can use the `--grep` -option to run only the tests that match a provided pattern. The `--invert` -option causes the matched tests to be excluded instead of included. +Since the tests use [AVA](https://github.com/sindresorhus/ava), you can use the +`--match` option to run only the tests that match a provided pattern. __Run only the tests that match a pattern:__ - npm test -- -- --grep + npm test -- -- --match="" __Only run the tests for the `datastore` sample:__ - npm test -- -- --grep datastore + npm test -- -- --match="datastore" __Skip the tests that match a pattern:__ - npm test -- -- --grep --invert + npm test -- -- --match="!" __Run all but the `datastore` tests:__ - npm test -- -- --grep datastore --invert + npm test -- -- --match="!datastore" __Skip the tests that require Redis and Memcached:__ - npm test -- -- --grep "express-memcached-session|redis" --invert + npm test -- -- --match="express-memcached-session|redis" ## License diff --git a/logging/export.js b/logging/export.js index fcab588ff81..313f8ea2673 100644 --- a/logging/export.js +++ b/logging/export.js @@ -28,70 +28,103 @@ var logging = gcloud.logging(); // [END setup] // [START listSinks] -function listSinks(callback) { +/** + * @param {Function} callback Callback function. + */ +function listSinksExample(callback) { // list all sinks in the authenticated project - logging.getSinks(callback); + logging.getSinks(function (err, sinks) { + if (err) { + return callback(err); + } + + // Should have received all sinks + console.log('Found ' + sinks.length + ' sinks'); + callback(null, sinks); + }); } // [END listSinks] // [START createSink] -function createSink(callback) { - // Get a reference to the Cloud Storage component - var gcs = gcloud.storage(); - +/** + * @param {string} sinkName Name of the new sink. + * @param {Object} config Configuration options for the new sink. + * @param {Function} callback Callback function. + */ +function createSinkExample(sinkName, config, callback) { // create a new sink in the authenticated project // // This method only works if you are authenticated as yourself, e.g. using the // gcloud SDK. - logging.createSink('mySink', { - destination: gcs.bucket('logging-bucket') - }, callback); + logging.createSink(sinkName, config, function (err, sink, apiResponse) { + if (err) { + return callback(err); + } + + // Should have received newly created sink + console.log('Created ' + sinkName, sink); + callback(null, sink, apiResponse); + }); } // [END createSink] // [START updateSink] -function updateSink(callback) { - // Get a reference to the Cloud Storage component - var gcs = gcloud.storage(); +/** + * @param {string} sinkName Name of the sink to update. + * @param {Object} config New configuration options for the sink. + * @param {Function} callback Callback function. + */ +function updateSinkExample(sinkName, config, callback) { // Get a reference to an existing sink - var sink = logging.sink('mySink'); + var sink = logging.sink(sinkName); // update a sink // // This method only works if you are authenticated as yourself, e.g. using the // gcloud SDK. - sink.setMetadata({ - // change destination to something else - destination: gcs.bucket('other-logging-bucket') - }, callback); + sink.setMetadata(config, function (err, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Updated ' + sinkName); + callback(null, apiResponse); + }); } // [END updateSink] // [START deleteSink] -function deleteSink(callback) { +/** + * @param {string} sinkName Name of the sink to delete. + * @param {Function} callback Callback function. + */ +function deleteSinkExample(sinkName, callback) { // Get a reference to an existing sink - var sink = logging.sink('mySink'); + var sink = logging.sink(sinkName); // delete a sink // // This method only works if you are authenticated as yourself, e.g. using the // gcloud SDK. - sink.delete(callback); + sink.delete(function (err, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Deleted ' + sinkName); + callback(null, apiResponse); + }); } // [END deleteSink] -exports.runExample = function (cb) { - listSinks(function (err, sinks, apiResponse) { - console.log(err, 'sinks:', sinks, 'apiResponse:', apiResponse); - if (typeof cb === 'function') { - cb(err, sinks); - } - }); +// Run the examples +exports.main = function (cb) { + listSinksExample(cb || console.log); }; -exports.createSink = createSink; -exports.updateSink = updateSink; -exports.deleteSink = deleteSink; +exports.createSinkExample = createSinkExample; +exports.updateSinkExample = updateSinkExample; +exports.deleteSinkExample = deleteSinkExample; if (module === require.main) { - exports.runExample(); + exports.main(); } diff --git a/logging/list.js b/logging/list.js index 53215f50145..aec74207bee 100644 --- a/logging/list.js +++ b/logging/list.js @@ -30,24 +30,33 @@ var gcloud = require('gcloud')({ // Get a reference to the logging component var logging = gcloud.logging(); -function list(callback) { - // Retrieve the latest 3 log entries from the authenticated project. - logging.getEntries({ - pageSize: 3 - }, callback); -} -// [END list] +/** + * @param {Object} [options] Configuration options for the request. + * @param {Function} callback Callback function. + */ +function listExample(options, callback) { + if (typeof options === 'function') { + callback = options; + } -exports.runExample = function (cb) { - console.log('retrieving latest 3 log entries...'); - list(function (err, entries, apiResponse) { - console.log(err, 'entries:', entries, 'apiResponse:', apiResponse); - if (typeof cb === 'function') { - cb(err, entries, apiResponse); + // Retrieve the latest some log entries from the authenticated project. + logging.getEntries(options, function (err, entries, nextQuery, apiResponse) { + if (err) { + return callback(err); } + + // Should have received some log entries + console.log('Found ' + entries.length + ' entries'); + callback(null, entries, nextQuery, apiResponse); }); +} +// [END list] + +// Run the examples +exports.main = function (options, cb) { + listExample(options || { pageSize: 1 }, cb || console.log); }; if (module === require.main) { - exports.runExample(); + exports.main(); } diff --git a/logging/package.json b/logging/package.json index bb512293083..52a7f647197 100644 --- a/logging/package.json +++ b/logging/package.json @@ -14,6 +14,6 @@ "export": "node export.js" }, "dependencies": { - "gcloud": "^0.27.0" + "gcloud": "^0.29.0" } } diff --git a/logging/write.js b/logging/write.js index 13399c16095..77f38bce7a0 100644 --- a/logging/write.js +++ b/logging/write.js @@ -14,6 +14,8 @@ /* jshint camelcase:false */ 'use strict'; +var async = require('async'); + // [START write] // [START setup] // You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT @@ -29,9 +31,13 @@ var gcloud = require('gcloud')({ var logging = gcloud.logging(); // [END setup] -function write(callback) { +/** + * @param {string} logName Name of the log to write to. + * @param {Function} callback Callback function. + */ +function writeExample(logName, callback) { // Get a reference to an existing log - var log = logging.log('myLog'); + var log = logging.log(logName); // Modify this resource type to match a resource in your project // See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \ @@ -61,50 +67,50 @@ function write(callback) { log.write([ entry, secondEntry - ], callback); + ], function (err, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Wrote to ' + logName); + callback(null, apiResponse); + }); } // [END write] // [START deleteLog] -function deleteLog(callback) { +/** + * @param {string} logName Name of the log to delete. + * @param {Function} callback Callback function. + */ +function deleteLogExample(logName, callback) { // Get a reference to an existing log - var log = logging.log('myLog'); + var log = logging.log(logName); // Delete the log - log.delete(callback); + log.delete(function (err, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Deleted ' + logName); + callback(null, apiResponse); + }); } // [END deleteLog] -exports.runExample = function (cb) { - var result = []; - console.log('writing 2 log entries...'); - write(function (err, apiResponse) { - console.log(err, 'apiResponse:', apiResponse); - if (err) { - return typeof cb === 'function' ? cb(err) : undefined; +// Run the examples +exports.main = function (cb) { + async.series([ + function (cb) { + writeExample('myLog', cb); + }, + function (cb) { + deleteLogExample('myLog', cb); } - result.push(apiResponse); - console.log('success!'); - console.log('deleting the log entries...'); - // If you remove this code, then you can find the two log entries that - // were written in the log view in the cloud console. - deleteLog(function (err, apiResponse) { - console.log(err, 'apiResponse:', apiResponse); - if (err && err.code === 404) { - err = undefined; - apiResponse = {}; - } - if (!err) { - console.log('success!'); - } - result.push(apiResponse); - if (typeof cb === 'function') { - cb(err, result); - } - }); - }); + ], cb || console.log); }; if (module === require.main) { - exports.runExample(); + exports.main(); } diff --git a/package.json b/package.json index dc873c15432..e2e7edfdb51 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,11 @@ }, "scripts": { "jshint": "jshint --exclude-path=.jshintignore .", - "mocha": "mocha --timeout 10000 --recursive", - "cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 30000 --recursive", - "coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls", + "deps_appengine": "ava --match='*: dependencies should install*'", + "ava": "npm run deps_appengine && ava --match='!*: dependencies should install*'", + "cover": "npm run deps_appengine && nyc ava --match='!*: dependencies should install*'", + "report": "nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls", + "report-html": "nyc report --reporter=html", "deps_datastore": "cd datastore; npm i; cd ../", "deps_pubsub": "cd pubsub; npm i; cd ../", "deps_storage": "cd storage; npm i; cd ../", @@ -39,13 +41,24 @@ "pretest": "npm run deps_datastore; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_sendgrid; npm run pretest_geddy", "test": "npm run jshint && npm run cover" }, + "ava": { + "files": [ + "test/appengine/*.test.js", + "test/computeengine/*.test.js", + "test/functions/*.test.js", + "test/logging/*.test.js", + "test/prediction/*.test.js", + "test/pubsub/*.test.js", + "test/storage/*.test.js" + ] + }, "devDependencies": { "async": "^1.5.2", - "coveralls": "^2.11.6", - "googleapis": "^2.1.7", - "istanbul": "^0.4.2", + "ava": "^0.13.0", + "coveralls": "^2.11.9", + "googleapis": "^4.0.0", "jshint": "~2.9.1", - "mocha": "^2.4.5", + "nyc": "^6.1.1", "proxyquire": "^1.7.4", "request": "^2.69.0", "supertest": "^1.1.0" diff --git a/prediction/README.md b/prediction/README.md index ed88404357b..3dc0d63d0e9 100644 --- a/prediction/README.md +++ b/prediction/README.md @@ -21,4 +21,4 @@ Execute a sample: Example: - npm run hostedmodels + npm run hostedmodels -- "good evening" diff --git a/prediction/hostedmodels.js b/prediction/hostedmodels.js index b21bab61a32..aa8a806d413 100644 --- a/prediction/hostedmodels.js +++ b/prediction/hostedmodels.js @@ -17,11 +17,12 @@ var google = require('googleapis'); var hostedmodels = google.prediction('v1.6').hostedmodels; -function auth (callback) { - google.auth.getApplicationDefault(function(err, authClient) { +function auth(callback) { + google.auth.getApplicationDefault(function (err, authClient) { if (err) { return callback(err); } + // The createScopedRequired method returns true when running on GAE or a // local developer machine. In that case, the desired scopes must be passed // in manually. When the code is running in GCE or a Managed VM, the scopes @@ -39,12 +40,17 @@ function auth (callback) { }); } -function predict(callback) { - auth(function(err, authClient) { +/** + * @param {string} phrase The phrase for which to predict sentiment, + * e.g. "good morning". + * @param {Function} callback Callback function. + */ +function predict(phrase, callback) { + auth(function (err, authClient) { if (err) { return callback(err); } - // Predict the sentiment for the word "hello". + // Predict the sentiment for the provided phrase hostedmodels.predict({ auth: authClient, // Project id used for this sample @@ -52,22 +58,28 @@ function predict(callback) { hostedModelName: 'sample.sentiment', resource: { input: { - // Predict sentiment of the word "hello" - csvInstance: ['hello'] + // Predict sentiment of the provided phrase + csvInstance: phrase.split(/\s/gi) } } - }, function (err, result) { - console.log(err, result); - if (typeof callback === 'function') { - callback(err, result); - } + }, function (err, prediction) { + if (err) { + return callback(err); + } + + // Received prediction result + console.log('Sentiment for "' + phrase + '": ' + prediction.outputLabel); + callback(null, prediction); }); }); } // [END predict] -exports.predict = predict; +// Run the examples +exports.main = function (phrase, cb) { + predict(phrase || 'good morning', cb || console.log); +}; if (module === require.main) { - predict(); + exports.main(process.argv.slice(2).shift()); } diff --git a/prediction/package.json b/prediction/package.json index b8d38bf03e4..e317db3060d 100644 --- a/prediction/package.json +++ b/prediction/package.json @@ -12,6 +12,6 @@ "hostedmodels": "node hostedmodels.js" }, "dependencies": { - "googleapis": "^2.1.7" + "googleapis": "^4.0.0" } } diff --git a/pubsub/iam.js b/pubsub/iam.js index 5fb8ed47b53..acbbf37aa03 100644 --- a/pubsub/iam.js +++ b/pubsub/iam.js @@ -15,46 +15,64 @@ var async = require('async'); var subscriptionSample = require('./subscription'); -var createTopic = subscriptionSample.createTopic; -var subscribe = subscriptionSample.subscribe; +var createTopicExample = subscriptionSample.createTopicExample; +var deleteTopicExample = subscriptionSample.deleteTopicExample; +var subscribeExample = subscriptionSample.subscribeExample; +var deleteSubscriptionExample = subscriptionSample.deleteSubscriptionExample; var pubsub = subscriptionSample.pubsub; // [START get_topic_policy] -function getTopicPolicy(callback) { +/** + * @param {string} topicName Name of the topic whose policy is to be retrieved. + * @param {Function} callback Callback function. + */ +function getTopicPolicyExample(topicName, callback) { // Grab a reference to an existing topic - var topic = pubsub.topic('messageCenter'); + var topic = pubsub.topic(topicName); // Retrieve the IAM policy for the topic - topic.iam.getPolicy(function (err, policy) { + topic.iam.getPolicy(function (err, policy, apiResponse) { if (err) { return callback(err); } + + // Received the policy console.log(policy); // { etag: 'ACAB' } - return callback(err, policy); + callback(null, policy, apiResponse); }); } // [END get_topic_policy] // [START get_subscription_policy] -function getSubscriptionPolicy(callback) { +/** + * @param {string} subscriptionName Name of the subscription whose policy is to + * be retrieved. + * @param {Function} callback Callback function. + */ +function getSubscriptionPolicyExample(subscriptionName, callback) { // Grab a reference to an existing subscription - var subscription = pubsub.subscription('newMessages'); + var subscription = pubsub.subscription(subscriptionName); // Retrieve the IAM policy for the subscription - subscription.iam.getPolicy(function (err, policy) { + subscription.iam.getPolicy(function (err, policy, apiResponse) { if (err) { return callback(err); } + console.log(policy); // { etag: 'ACAB' } - return callback(err, policy); + callback(null, policy, apiResponse); }); } // [END get_subscription_policy] // [START set_topic_policy] -function setTopicPolicy(callback) { +/** + * @param {string} topicName Name of the topic whose policy is to be updated. + * @param {Function} callback Callback function. + */ +function setTopicPolicyExample(topicName, callback) { // Grab a reference to an existing topic - var topic = pubsub.topic('messageCenter'); + var topic = pubsub.topic(topicName); // Policy update var myPolicy = { @@ -67,14 +85,26 @@ function setTopicPolicy(callback) { }; // Retrieve the IAM policy for the provided topic - topic.iam.setPolicy(myPolicy, callback); + topic.iam.setPolicy(myPolicy, function (err, policy, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Updated policy for ' + topicName); + callback(null, policy, apiResponse); + }); } // [END set_topic_policy] // [START set_subscription_policy] -function setSubscriptionPolicy(callback) { +/** + * @param {string} subscriptionName Name of the subscription whose policy is to + * be updated. + * @param {Function} callback Callback function. + */ +function setSubscriptionPolicyExample(subscriptionName, callback) { // Grab a reference to an existing subscription - var subscription = pubsub.subscription('newMessages'); + var subscription = pubsub.subscription(subscriptionName); // Policy update var myPolicy = { @@ -87,14 +117,25 @@ function setSubscriptionPolicy(callback) { }; // Retrieve the IAM policy for the provided subscription - subscription.iam.setPolicy(myPolicy, callback); + subscription.iam.setPolicy(myPolicy, function (err, policy, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Updated policy for ' + subscriptionName); + callback(null, policy, apiResponse); + }); } // [END set_subscription_policy] // [START test_topic_permissions] -function testTopicPermissions(callback) { +/** + * @param {string} topicName Name of the topic whose policy is to be tested. + * @param {Function} callback Callback function. + */ +function testTopicPermissionsExample(topicName, callback) { // Grab a reference to an existing topic - var topic = pubsub.topic('messageCenter'); + var topic = pubsub.topic(topicName); var tests = [ 'pubsub.topics.attachSubscription', @@ -103,14 +144,26 @@ function testTopicPermissions(callback) { ]; // Retrieve the IAM policy for the provided topic - topic.iam.testPermissions(tests, callback); + topic.iam.testPermissions(tests, function (err, permissions, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Got permissions for ' + topicName); + callback(null, permissions, apiResponse); + }); } // [END test_topic_permissions] // [START test_subscription_permissions] -function testSubscriptionPermissions(callback) { +/** + * @param {string} subscriptionName Name of the subscription whose policy is to + * be tested. + * @param {Function} callback Callback function. + */ +function testSubscriptionPermissionsExample(subscriptionName, callback) { // Grab a reference to an existing subscription - var subscription = pubsub.subscription('newMessages'); + var subscription = pubsub.subscription(subscriptionName); var tests = [ 'pubsub.subscriptions.consume', @@ -118,79 +171,55 @@ function testSubscriptionPermissions(callback) { ]; // Retrieve the IAM policy for the provided subscription - subscription.iam.testPermissions(tests, callback); + subscription.iam.testPermissions( + tests, + function (err, permissions, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Got permissions for ' + subscriptionName); + callback(null, permissions, apiResponse); + } + ); } // [END test_subscription_permissions] -exports.setTopicPolicy = setTopicPolicy; -exports.setSubscriptionPolicy = setSubscriptionPolicy; -exports.runSample = runSample; +exports.setTopicPolicyExample = setTopicPolicyExample; +exports.setSubscriptionPolicyExample = setSubscriptionPolicyExample; -function runSample(callback) { - var _subscription; - var _topic; - // Gather responses - var responses = []; - async.waterfall([ +// Run the examples +exports.main = function (cb) { + var topicName = 'messageCenter2'; + var subscriptionName = 'newMessages2'; + async.series([ function (cb) { - console.log('create topic...'); - createTopic(cb); + createTopicExample(topicName, cb); }, - function (topic, apiResponse, cb) { - _topic = topic; - responses.push([topic, apiResponse]); - console.log('created topic'); - console.log('get topic IAM policy...'); - getTopicPolicy(cb); + function (cb) { + getTopicPolicyExample(topicName, cb); }, - function (policy, cb) { - responses.push([policy]); - console.log('got topic policy', policy); - console.log('testing topic permissions...'); - testTopicPermissions(cb); + function (cb) { + testTopicPermissionsExample(topicName, cb); }, - function (permissions, apiResponse, cb) { - responses.push([permissions, apiResponse]); - console.log('tested topic permissions', permissions); - console.log('create subscription...'); - subscribe(cb); + function (cb) { + subscribeExample(topicName, subscriptionName, cb); }, - function (subscription, apiResponse, cb) { - _subscription = subscription; - responses.push([subscription, apiResponse]); - console.log('created subscription'); - console.log('get subscription IAM policy...'); - getSubscriptionPolicy(cb); + function (cb) { + getSubscriptionPolicyExample(subscriptionName, cb); }, - function (policy, cb) { - responses.push([policy]); - console.log('got subscription policy', policy); - console.log('testing subscription permissions...'); - testSubscriptionPermissions(cb); + function (cb) { + testSubscriptionPermissionsExample(subscriptionName, cb); }, - function (permissions, apiResponse, cb) { - responses.push([permissions, apiResponse]); - console.log('tested subscription permissions', permissions); - console.log('deleting subscription...'); - _subscription.delete(cb); + function (cb) { + deleteSubscriptionExample(subscriptionName, cb); }, - function (apiResponse, cb) { - console.log('deleted subscription'); - console.log('deleting topic...'); - _topic.delete(cb); - } - ], function (err) { - if (err) { - console.error(err); - } else { - console.log('deleted topic'); - } - if (typeof callback === 'function') { - callback(err, responses); + function (cb) { + deleteTopicExample(topicName, cb); } - }); -} + ], cb || console.log); +}; if (module === require.main) { - runSample(); + exports.main(); } diff --git a/pubsub/subscription.js b/pubsub/subscription.js index 1c0b1909a55..e63654859e2 100644 --- a/pubsub/subscription.js +++ b/pubsub/subscription.js @@ -31,110 +31,223 @@ var pubsub = gcloud.pubsub(); // [END auth] // [START create_topic] -function createTopic(callback) { - var topicName = 'messageCenter'; - +/** + * @param {string} topicName Name for the new topic. + * @param {Function} callback Callback function. + */ +function createTopicExample(topicName, callback) { var topic = pubsub.topic(topicName); // Get the topic if it exists. Create it if it does not exist. topic.get({ autoCreate: true - }, callback); + }, function (err, topic, apiResponse) { + if (err) { + return callback(err); + } + + // Created the topic + console.log('Created topic ' + topicName); + callback(null, topic, apiResponse); + }); } // [END create_topic] -// [START publish] -function publish(callback) { - var topicName = 'messageCenter'; +// [START delete_topic] +/** + * @param {string} topicName Name of the topic to delete. + * @param {Function} callback Callback function. + */ +function deleteTopicExample(topicName, callback) { + var topic = pubsub.topic(topicName); + + // Delete the topic + topic.delete(function (err, apiResponse) { + if (err) { + return callback(err); + } + + // Deleted the topic + console.log('Deleted topic ' + topicName); + callback(null, apiResponse); + }); +} +// [END delete_topic] + +// [START delete_subscription] +/** + * @param {string} subscriptionName Name of the subscription to delete. + * @param {Function} callback Callback function. + */ +function deleteSubscriptionExample(subscriptionName, callback) { + var subscription = pubsub.subscription(subscriptionName); + + // Delete the subscription + subscription.delete(function (err, apiResponse) { + if (err) { + return callback(err); + } + + // Deleted the subscription + console.log('Deleted subscription ' + subscriptionName); + callback(null, apiResponse); + }); +} +// [END delete_subscription] +// [START publish] +/** + * @param {string} topicName Name of the topic to which to publish. + * @param {Function} callback Callback function. + */ +function publishExample(topicName, callback) { // Grab a reference to an existing topic var topic = pubsub.topic(topicName); // Publish a message to the topic topic.publish({ data: 'Hello, world!' - }, callback); + }, function (err, messageIds, apiResponse) { + if (err) { + return callback(err); + } + + console.log('Published ' + messageIds.length + ' messages'); + callback(null, messageIds, apiResponse); + }); } // [END publish] // [START list_topics] -function getAllTopics(callback) { +/** + * @param {string} [pageToken] Page to retrieve. + * @param {Function} callback Callback function. + */ +function getAllTopicsExample(pageToken, callback) { + if (typeof pageToken === 'function') { + callback = pageToken; + pageToken = undefined; + } + var options = {}; + if (pageToken) { + options.pageToken = pageToken; + } + // Grab paginated topics - pubsub.getTopics(function (err, topics, nextQuery) { + pubsub.getTopics(options, function (err, topics, nextQuery) { // Quit on error if (err) { return callback(err); } + // There is another page of topics if (nextQuery) { // Grab the remaining pages of topics recursively - return getAllTopics(function (err, _topics) { + return getAllTopicsExample(nextQuery.token, function (err, _topics) { if (_topics) { topics = topics.concat(_topics); } - callback(err, topics); + callback(null, topics); }); } // Last page of topics - return callback(err, topics); + return callback(null, topics); }); } // [END list_topics] // [START get_all_subscriptions] -function getAllSubscriptions(callback) { +/** + * @param {string} [pageToken] Page to retrieve. + * @param {Function} callback Callback function. + */ +function getAllSubscriptionsExample(pageToken, callback) { + if (typeof pageToken === 'function') { + callback = pageToken; + pageToken = undefined; + } + var options = {}; + if (pageToken) { + options.pageToken = pageToken; + } // Grab paginated subscriptions - pubsub.getSubscriptions(function (err, subscriptions, nextQuery) { - // Quit on error - if (err) { - return callback(err); - } - // There is another page of subscriptions - if (nextQuery) { - // Grab the remaining pages of subscriptions recursively - return getAllSubscriptions(function (err, _subscriptions) { - if (_subscriptions) { - subscriptions = subscriptions.concat(_subscriptions); - } - callback(err, subscriptions); - }); + pubsub.getSubscriptions( + options, + function (err, subscriptions, nextQuery) { + // Quit on error + if (err) { + return callback(err); + } + + // There is another page of subscriptions + if (nextQuery) { + // Grab the remaining pages of subscriptions recursively + return getAllSubscriptionsExample( + nextQuery.token, + function (err, _subscriptions) { + if (_subscriptions) { + subscriptions = subscriptions.concat(_subscriptions); + } + callback(null, subscriptions); + } + ); + } + // Last page of subscriptions + return callback(null, subscriptions); } - // Last page of subscriptions - return callback(err, subscriptions); - }); + ); } // [END get_all_subscriptions] // [START create_subscription] -function subscribe(callback) { - var topicName = 'messageCenter'; - var subscriptionName = 'newMessages'; - +/** + * @param {string} topicName Name of the topic for the new subscription. + * @param {string} subscriptionName Name for the new subscription. + * @param {Function} callback Callback function. + */ +function subscribeExample(topicName, subscriptionName, callback) { var options = { reuseExisting: true }; - pubsub.subscribe(topicName, subscriptionName, options, callback); + pubsub.subscribe( + topicName, + subscriptionName, + options, + function (err, subscription, apiResponse) { + if (err) { + return callback(err); + } + + // Got the subscription + console.log('Subscribed to ' + topicName); + callback(null, subscription, apiResponse); + } + ); } // [END create_subscription] // [START handle_message] -function handleMessage(message) { +function handleMessageExample(message) { console.log('received message: ' + message.data); } // [END handle_message] // [START pull_messages] -function pullMessages(callback) { - // Create a topic - createTopic(function (err) { - if (err) { - return callback(err); - } - // Create a subscription to the topic - subscribe(function (err, subscription) { - if (err) { - return callback(err); - } +/** + * + */ +function pullMessagesExample(topicName, subscriptionName, callback) { + // Use the "async" library to handle a chain of asynchronous functions + async.waterfall([ + function (cb) { + // Create a topic + createTopicExample(topicName, cb); + }, + function (topic, apiResponse, cb) { + // Create a subscription + subscribeExample(topicName, subscriptionName, cb); + }, + function (subscription, apiResponse, cb) { var options = { // Limit the amount of messages pulled. maxResults: 100, @@ -143,101 +256,70 @@ function pullMessages(callback) { returnImmediately: false }; // Pull any messages on the subscription - subscription.pull(options, function (err, messages) { + subscription.pull(options, cb); + }, + function (messages, apiResponse, cb) { + // Do something for each message + messages.forEach(handleMessageExample); + + // Acknowledge messages + var subscription = pubsub.subscription(subscriptionName); + subscription.ack(messages.map(function (message) { + return message.ackId; + }), function (err) { if (err) { - return callback(err); + return cb(err); } - - // Do something for each message - messages.forEach(handleMessage); - - // Acknowledge messages - subscription.ack(messages.map(function (message) { - return message.ackId; - }), function (err) { - if (err) { - return callback(err); - } - callback(null, messages); - }); + cb(null, messages); }); - }); + } + ], function (err, messages) { + if (err) { + return callback(err); + } + + console.log('Pulled ' + messages.length + ' messages'); + callback(null, messages); }); } // [END pull_messages] -exports.createTopic = createTopic; -exports.subscribe = subscribe; -exports.runSample = runSample; +exports.createTopicExample = createTopicExample; +exports.deleteTopicExample = deleteTopicExample; +exports.subscribeExample = subscribeExample; +exports.deleteSubscriptionExample = deleteSubscriptionExample; exports.pubsub = pubsub; - -function runSample(callback) { - var _subscription; - var _topic; - // Gather responses - var responses = []; - async.waterfall([ +exports.main = function (cb) { + var topicName = 'messageCenter'; + var subscriptionName = 'newMessages'; + async.series([ function (cb) { - console.log('create topic...'); - createTopic(cb); + createTopicExample(topicName, cb); }, - function (topic, apiResponse, cb) { - _topic = topic; - responses.push([topic, apiResponse]); - console.log('created topic'); - console.log('create subscription...'); - subscribe(cb); + function (cb) { + subscribeExample(topicName, subscriptionName, cb); }, - function (subscription, apiResponse, cb) { - _subscription = subscription; - responses.push([subscription, apiResponse]); - console.log('created subscription'); - console.log('list all topics...'); - getAllTopics(cb); + function (cb) { + getAllTopicsExample(cb); }, - function (topics, cb) { - responses.push([topics]); - console.log('got all topics'); - console.log('list all subscriptions...'); - getAllSubscriptions(cb); + function (cb) { + getAllSubscriptionsExample(cb); }, - function (subscriptions, cb) { - responses.push([subscriptions]); - console.log('got all subscriptions'); - console.log('publishing a message...'); - publish(cb); + function (cb) { + publishExample(topicName, cb); }, - function (messageIds, apiResponse, cb) { - responses.push([messageIds, apiResponse]); - console.log('published message'); - console.log('pulling messages...'); - pullMessages(cb); + function (cb) { + pullMessagesExample(topicName, subscriptionName, cb); }, - function (messages, cb) { - responses.push([messages]); - console.log('got messages', messages.map(function (message) { - return message.data; - })); - console.log('deleting subscription...'); - _subscription.delete(cb); + function (cb) { + deleteSubscriptionExample(subscriptionName, cb); }, - function (apiResponse, cb) { - console.log('deleted subscription'); - console.log('deleting topic...'); - _topic.delete(cb); - } - ], function (err) { - if (err) { - console.error(err); - } else { - console.log('deleted topic'); - } - if (typeof callback === 'function') { - callback(err, responses); + function (cb) { + deleteTopicExample(topicName, cb); } - }); -} + ], cb || console.log); +}; if (module === require.main) { - runSample(); + exports.main(); } diff --git a/storage/README.md b/storage/README.md index e27f20ba10a..5d7766a8450 100644 --- a/storage/README.md +++ b/storage/README.md @@ -17,7 +17,7 @@ To print available commands: Execute a sample: - npm run -- [arg_1] [arg_2] [arg_n] + npm run -- [args...] Example: diff --git a/storage/authSample.js b/storage/authSample.js index a1b1d839067..253abdc436c 100644 --- a/storage/authSample.js +++ b/storage/authSample.js @@ -36,10 +36,10 @@ var google = require('googleapis'); * responds with the list of buckets. */ // [START list_buckets] -function listBuckets(projectId, cb) { +function listBucketsExample(projectId, callback) { google.auth.getApplicationDefault(function(err, authClient) { if (err) { - return cb(err); + return callback(err); } // Depending on the environment that provides the default credentials @@ -54,32 +54,30 @@ function listBuckets(projectId, cb) { // Create the service object. var storage = google.storage('v1'); + // Make the api call to list the buckets. storage.buckets.list({ auth: authClient, project: projectId - }, cb); + }, function (err, response) { + if (err) { + return callback(err); + } + + console.log('Found ' + response.items.length + ' buckets'); + callback(null, response); + }); }); } // [END list_buckets] -exports.runExample = function (projectId, cb) { - listBuckets( - projectId, - function (err, response) { - if (err) { - console.log(err); - } else if (response && response.items) { - console.log(JSON.stringify(response.items, null, 2)); - } - if (typeof cb === 'function') { - cb(err, response); - } - } - ); +// Run the samples +exports.main = function (projectId, cb) { + listBucketsExample(projectId, cb || function (err, response) { + console.log(err, response.items); + }); }; -// For command-line execution of sample if (module === require.main) { - exports.runExample(process.argv.length > 2 ? process.argv[2] : ''); + exports.main(process.argv.slice(2).shift()); } diff --git a/storage/package.json b/storage/package.json index 0326c705f07..1cbff9007e8 100644 --- a/storage/package.json +++ b/storage/package.json @@ -12,6 +12,6 @@ "authSample": "node authSample.js" }, "dependencies": { - "googleapis": "^2.1.7" + "googleapis": "^4.0.0" } } diff --git a/test/.jshintrc b/test/.jshintrc deleted file mode 100644 index e26f0d4bc76..00000000000 --- a/test/.jshintrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "../.jshintrc", - "mocha": true -} diff --git a/test/appengine/all.test.js b/test/appengine/all.test.js index b3ca3e83e7d..55aaae07e0f 100644 --- a/test/appengine/all.test.js +++ b/test/appengine/all.test.js @@ -13,15 +13,17 @@ 'use strict'; +var test = require('ava'); var spawn = require('child_process').spawn; var request = require('request'); var fs = require('fs'); +var path = require('path'); var async = require('async'); var cwd = process.cwd(); var projectId = process.env.GCLOUD_PROJECT; function getPath(dir) { - return cwd + '/appengine/' + dir; + return path.join(cwd, '/../../appengine/', dir); } function changeScaling(dir) { @@ -271,43 +273,36 @@ function testRequest(url, sample, cb) { }); } -describe('appengine/', function () { - var port = 8080; - sampleTests.forEach(function (sample) { - sample.env = sample.env || {}; - sample.env.PORT = port; - if (sample.dir === 'parse-server') { - sample.env.SERVER_URL = sample.env.SERVER_URL + port + '/parse'; - console.log(sample); - } - port++; - it(sample.dir + ': dependencies should install', function (done) { - // Allow extra time for "npm install" - this.timeout(sample.timeout || 120000); - - testInstallation(sample, done); - }); - - if (sample.TRAVIS && !process.env.TRAVIS) { - return; - } - - it(sample.dir + ' should return 200 and Hello World', function (done) { - testLocalApp(sample, done); - }); +var port = 8080; +sampleTests.forEach(function (sample) { + sample.env = sample.env || {}; + sample.env.PORT = port; + if (sample.dir === 'parse-server') { + sample.env.SERVER_URL = sample.env.SERVER_URL + port + '/parse'; + console.log(sample); + } + port++; + test.cb(sample.dir + ': dependencies should install', function (t) { + testInstallation(sample, t.end); }); - if (!process.env.TRAVIS || !process.env.DEPLOY_TESTS) { + if (sample.TRAVIS && !process.env.TRAVIS) { return; } - it('should deploy all samples', function (done) { + test.cb(sample.dir + ' should return 200 and Hello World', function (t) { + testLocalApp(sample, t.end); + }); +}); + +if (process.env.TRAVIS && process.env.DEPLOY_TESTS) { + test.cb('should deploy all samples', function (t) { // 30 minutes because deployments are slow this.timeout(30 * 60 * 1000); - testDeployments(done); + testDeployments(t.end); }); -}); +} function testInstallation(sample, done) { @@ -350,6 +345,7 @@ function testLocalApp(sample, done) { var opts = { cwd: getPath(sample.dir) }; + console.log('cwd: ' + opts.cwd); if (sample.env) { opts.env = sample.env; for (var key in process.env) { @@ -361,49 +357,55 @@ function testLocalApp(sample, done) { console.log('\t' + sample.dir + ': Start server on port ' + sample.env.PORT); var proc = spawn(sample.cmd, sample.args, opts); - proc.on('error', function (err) { - console.log('\t' + sample.dir + ': ERROR', err.message); - finish(err); - }); + // Exit helper so we don't call "cb" more than once + function finish(err) { + if (!calledDone) { + calledDone = true; + done(err || requestError); + } + } - if (!process.env.TRAVIS) { - proc.stderr.on('data', function (data) { - console.log('stderr: ' + data); + try { + // Give the server time to start up + setTimeout(function () { + console.log('\t' + sample.dir + ': Send test request...'); + // Test that the app is working + var url = 'http://localhost:' + sample.env.PORT; + testRequest(url, sample, function (err, result) { + requestError = err; + if (result) { + console.log('\t' + sample.dir + ': Success!'); + } + console.log('\t' + sample.dir + ': Send shutdown signal...'); + proc.kill('SIGKILL'); + }); + }, 5000); + + proc.on('error', function (err) { + console.log('\t' + sample.dir + ': ERROR', err.message); + finish(err); }); - } - proc.on('exit', function (code, signal) { - if (signal === 'SIGKILL') { - console.log('\t' + sample.dir + ': SIGKILL received!'); - } - if (code !== 0 && signal !== 'SIGKILL') { - console.log('\t' + sample.dir + ': ERROR', code, signal); - return finish(new Error(sample.dir + ': failed to run!')); - } else { - return finish(); + if (!process.env.TRAVIS) { + proc.stderr.on('data', function (data) { + console.log('stderr: ' + data); + }); } - }); - // Give the server time to start up - setTimeout(function () { - console.log('\t' + sample.dir + ': Send test request...'); - // Test that the app is working - var url = 'http://localhost:' + sample.env.PORT; - testRequest(url, sample, function (err, result) { - requestError = err; - if (result) { - console.log('\t' + sample.dir + ': Success!'); + proc.on('exit', function (code, signal) { + if (signal === 'SIGKILL') { + console.log('\t' + sample.dir + ': SIGKILL received!'); + } + if (code !== 0 && signal !== 'SIGKILL') { + console.log('\t' + sample.dir + ': ERROR', code, signal); + return finish(new Error(sample.dir + ': failed to run!')); + } else { + return finish(); } - console.log('\t' + sample.dir + ': Send shutdown signal...'); - proc.kill('SIGKILL'); }); - }, 5000); - - // Exit helper so we don't call "cb" more than once - function finish(err) { - if (!calledDone) { - calledDone = true; - done(err || requestError); + } catch (err) { + if (proc) { + proc.kill('SIGKILL'); } } } diff --git a/test/computeengine/sendgrid.test.js b/test/computeengine/sendgrid.test.js index 30d28f5725e..8c5820164c2 100644 --- a/test/computeengine/sendgrid.test.js +++ b/test/computeengine/sendgrid.test.js @@ -13,28 +13,26 @@ 'use strict'; -var assert = require('assert'); +var test = require('ava'); var proxyquire = require('proxyquire').noPreserveCache(); process.env.SENDGRID_API_KEY = 'foo'; -describe('sendgrid', function () { - it('should send an email', function (done) { - proxyquire('../../computeengine/sendgrid/sendmail.js', { - sendgrid: function (key) { - assert.equal(key, 'foo'); - return { - send: function (payload, cb) { - assert.deepEqual(payload, { - from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', - to: 'EMAIL@EXAMPLE.COM', - subject: 'test email from Node.js on Google Cloud Platform', - text: 'Hello!\n\nThis a test email from Node.js.' - }); - cb('done'); - done(); - } - }; - } - }); +test.cb('should send an email', function (t) { + proxyquire('../../computeengine/sendgrid/sendmail.js', { + sendgrid: function (key) { + t.is(key, 'foo'); + return { + send: function (payload, cb) { + t.same(payload, { + from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', + to: 'EMAIL@EXAMPLE.COM', + subject: 'test email from Node.js on Google Cloud Platform', + text: 'Hello!\n\nThis a test email from Node.js.' + }); + cb('done'); + t.end(); + } + }; + } }); }); diff --git a/test/datastore/.jshintrc b/test/datastore/.jshintrc new file mode 100644 index 00000000000..47401c947e4 --- /dev/null +++ b/test/datastore/.jshintrc @@ -0,0 +1,4 @@ +{ + "extends": "../../.jshintrc", + "mocha": true +} \ No newline at end of file diff --git a/test/functions/helloworld.test.js b/test/functions/helloworld.test.js index 0482d0f3dae..3d3ddfdc52d 100644 --- a/test/functions/helloworld.test.js +++ b/test/functions/helloworld.test.js @@ -13,21 +13,14 @@ 'use strict'; -var assert = require('assert'); - +var test = require('ava'); var helloworldSample = require('../../functions/helloworld'); -describe('functions/helloworld', function () { - it('should return a hello world message', function (done) { - helloworldSample.helloworld({ - success: function (result) { - try { - assert.equal(result, 'Hello World!'); - done(); - } catch (err) { - done(err); - } - } - }); +test.cb('should return a hello world message', function (t) { + helloworldSample.helloworld({ + success: function (result) { + t.is(result, 'Hello World!'); + t.end(); + } }); }); diff --git a/test/functions/log.test.js b/test/functions/log.test.js index 458f07f4319..14e6ee53fa8 100644 --- a/test/functions/log.test.js +++ b/test/functions/log.test.js @@ -13,86 +13,63 @@ 'use strict'; -var assert = require('assert'); - +var test = require('ava'); var logSample = require('../../functions/log'); -describe('functions/log', function () { - it('should write to log', function (done) { - var logMessage = 'I am a log entry!'; - var messageWasPrinted = false; - var originalLog = console.log; +test.cb('should write to log', function (t) { + var logMessage = 'I am a log entry!'; + var messageWasPrinted = false; - console.log = function (data) { - if (data === logMessage) { - messageWasPrinted = true; - } - }; + console.log = function (data) { + if (data === logMessage) { + messageWasPrinted = true; + } + }; - logSample.log({ - success: function (result) { - try { - assert.equal(result, undefined); - if (messageWasPrinted) { - console.log = originalLog; - done(); - } else { - console.log = originalLog; - done('message was not printed!'); - } - } catch (err) { - console.log = originalLog; - done(err); - } + logSample.log({ + success: function (result) { + t.is(result, undefined); + if (messageWasPrinted) { + t.end(); + } else { + t.end('message was not printed!'); } - }); + } }); - it('should write to log 2', function (done) { - var logMessage = 'My GCF Function: foo'; - var messageWasPrinted = false; - var originalLog = console.log; +}); +test.cb('should write to log 2', function (t) { + var logMessage = 'My GCF Function: foo'; + var messageWasPrinted = false; - console.log = function (data) { - if (data === logMessage) { - messageWasPrinted = true; - } - }; + console.log = function (data) { + if (data === logMessage) { + messageWasPrinted = true; + } + }; - logSample.helloworld({ - success: function (result) { - try { - assert.equal(result, undefined); - if (messageWasPrinted) { - console.log = originalLog; - done(); - } else { - console.log = originalLog; - done('message was not printed!'); - } - } catch (err) { - console.log = originalLog; - done(err); - } + logSample.helloworld({ + success: function (result) { + t.is(result, undefined); + if (messageWasPrinted) { + t.end(); + } else { + t.end('message was not printed!'); } - }, - { - message: 'foo' - }); + } + }, + { + message: 'foo' }); - it('should write to log 3', function (done) { - var logMessage = 'My GCF Function: foo'; - logSample.hellohttp({ - success: function (result) { - try { - assert.equal(result, logMessage); - done(); - } catch (err) { - done(err); - } - } - }, - { - message: 'foo' - }); +}); +test.cb('should write to log 3', function (t) { + var logMessage = 'My GCF Function: foo'; + logSample.hellohttp({ + success: function (result) { + t.is(result, logMessage); + t.end(); + } + }, + { + message: 'foo' }); }); diff --git a/test/functions/message.test.js b/test/functions/message.test.js index 667fde0918d..51351a9324e 100644 --- a/test/functions/message.test.js +++ b/test/functions/message.test.js @@ -13,52 +13,37 @@ 'use strict'; -var assert = require('assert'); - +var test = require('ava'); var messageSample = require('../../functions/message'); -describe('functions/message', function () { - it('should print a message', function (done) { - var testMessage = 'test message'; - var messageWasPrinted = false; - var originalLog = console.log; +test.cb('should print a message', function (t) { + var testMessage = 'test message'; + var messageWasPrinted = false; - console.log = function (data) { - if (data === testMessage) { - messageWasPrinted = true; - } - }; + console.log = function (data) { + if (data === testMessage) { + messageWasPrinted = true; + } + }; - messageSample.helloworld({ - success: function (result) { - try { - assert.equal(result, undefined); - if (messageWasPrinted) { - console.log = originalLog; - done(); - } else { - console.log = originalLog; - done('message was not printed!'); - } - } catch (err) { - console.log = originalLog; - done(err); - } + messageSample.helloworld({ + success: function (result) { + t.is(result, undefined); + if (messageWasPrinted) { + t.end(); + } else { + t.end('message was not printed!'); } - }, { - message: testMessage - }); - }); - it('should say no message was providied', function (done) { - messageSample.helloworld({ - failure: function (result) { - try { - assert.equal(result, 'No message defined!'); - done(); - } catch (err) { - done(err); - } - } - }, {}); + } + }, { + message: testMessage }); }); +test.cb('should say no message was providied', function (t) { + messageSample.helloworld({ + failure: function (result) { + t.is(result, 'No message defined!'); + t.end(); + } + }, {}); +}); diff --git a/test/functions/module.test.js b/test/functions/module.test.js index 95b17006f8d..64c73433897 100644 --- a/test/functions/module.test.js +++ b/test/functions/module.test.js @@ -13,21 +13,14 @@ 'use strict'; -var assert = require('assert'); - +var test = require('ava'); var moduleSample = require('../../functions/module'); -describe('functions/module', function () { - it('should return a hello world message', function (done) { - moduleSample.helloworld({ - success: function (result) { - try { - assert.equal(result, 'Hello World!'); - done(); - } catch (err) { - done(err); - } - } - }); +test.cb('should return a hello world message', function (t) { + moduleSample.helloworld({ + success: function (result) { + t.is(result, 'Hello World!'); + t.end(); + } }); }); diff --git a/test/functions/uuid.test.js b/test/functions/uuid.test.js index ea4c9c1e56f..fc25effab4a 100644 --- a/test/functions/uuid.test.js +++ b/test/functions/uuid.test.js @@ -13,22 +13,15 @@ 'use strict'; -var assert = require('assert'); - +var test = require('ava'); var uuidSample = require('../../functions/uuid'); -describe('functions/uuid', function () { - it('should generate a uuid', function (done) { - uuidSample.uuid({ - success: function (uuid) { - try { - assert.equal(typeof uuid, 'string'); - assert.equal(uuid.length, 36); - done(); - } catch (err) { - done(err); - } - } - }); +test.cb('should generate a uuid', function (t) { + uuidSample.uuid({ + success: function (uuid) { + t.is(typeof uuid, 'string'); + t.is(uuid.length, 36); + t.end(); + } }); }); diff --git a/test/logging/export.test.js b/test/logging/export.test.js index 990babb524f..f15d50c6b89 100644 --- a/test/logging/export.test.js +++ b/test/logging/export.test.js @@ -13,18 +13,14 @@ 'use strict'; -var assert = require('assert'); +var test = require('ava'); +var exportExample = require('../../logging/export'); -var logging = require('../../logging/export'); - -describe('logging/export', function () { - it('should list sinks', function (done) { - logging.runExample(function (err, sinks) { - if (err) { - return done(err); - } - assert.ok(Array.isArray(sinks), 'should be an array'); - done(); - }); +test.cb('should list sinks', function (t) { + exportExample.main(function (err, sinks) { + t.ifError(err); + t.ok(sinks, 'should have received sinks'); + t.ok(Array.isArray(sinks), 'sinks should be an array'); + t.end(); }); }); diff --git a/test/logging/list.test.js b/test/logging/list.test.js index 030c1ff9d4f..5a89ec8a703 100644 --- a/test/logging/list.test.js +++ b/test/logging/list.test.js @@ -13,27 +13,16 @@ 'use strict'; -var assert = require('assert'); +var test = require('ava'); +var listExample = require('../../logging/list'); -var logging = require('../../logging/list'); - -describe('logging/list', function () { - it('should list entries', function (done) { - logging.runExample(function (err, entries, apiResponse) { - if (err) { - return done(err); - } - assert.ok(Array.isArray(entries), 'should have got an array'); - assert.ok(apiResponse.orderBy); - assert.ok(apiResponse.pageSize); - assert.ok(Array.isArray(apiResponse.projectIds)); - if (entries.length === 3) { - // obviously this will pass - assert.equal(entries.length, 3, 'should have three entries'); - } else { - console.error('Received correct apiResponse, but no logs. Why not?'); - } - done(); - }); +test.cb('should list entries', function (t) { + listExample.main(undefined, function (err, entries, nextQuery, apiResponse) { + t.ifError(err); + t.ok(entries, 'should have received entries'); + t.ok(Array.isArray(entries), 'entries should be an array'); + t.ok(nextQuery, 'should have received nextQuery'); + t.ok(apiResponse, 'should have received apiResponse'); + t.end(); }); }); diff --git a/test/logging/write.test.js b/test/logging/write.test.js index 877267bd756..95a094148f4 100644 --- a/test/logging/write.test.js +++ b/test/logging/write.test.js @@ -13,24 +13,16 @@ 'use strict'; -var assert = require('assert'); +var test = require('ava'); +var writeExample = require('../../logging/write'); -var logging = require('../../logging/write'); - -describe('logging/write', function () { - it('should write entries', function (done) { - logging.runExample(function (err, result) { - if (err) { - return done(err); - } - try { - assert.equal(result.length, 2, 'should have two results'); - assert.deepEqual(result[0], {}, 'should have correct response'); - assert.deepEqual(result[1], {}, 'should have correct response'); - done(); - } catch (err) { - return done(err); - } - }); +test.cb('should write entries', function (t) { + writeExample.main(function (err, results) { + if (err && err.code === 404) { + return t.end(); + } + t.ifError(err); + t.is(results.length, 2, 'should have two results'); + t.end(); }); }); diff --git a/test/prediction/hostedmodels.test.js b/test/prediction/hostedmodels.test.js index bc3c2dd0cf9..a874393491c 100644 --- a/test/prediction/hostedmodels.test.js +++ b/test/prediction/hostedmodels.test.js @@ -13,31 +13,13 @@ 'use strict'; -var assert = require('assert'); +var test = require('ava'); var hostedmodels = require('../../prediction/hostedmodels'); -var EXPECTED_RESULT = { - kind: 'prediction#output', - id: 'sample.sentiment', - selfLink: 'https://www.googleapis.com/prediction/v1.6/projects/414649711441' + - '/hostedmodels/sample.sentiment/predict', - outputLabel: 'positive', - outputMulti: [ - { label: 'positive', score: '0.784671' }, - { label: 'negative', score: '0.186649' }, - { label: 'neutral', score: '0.028680' } - ] -}; - -describe('prediction/hostedmodels', function () { - it('should predict', function (done) { - this.timeout(30000); - hostedmodels.predict(function (err, result) { - if (err) { - return done(err); - } - assert.deepEqual(result, EXPECTED_RESULT); - done(); - }); +test.cb('should predict', function (t) { + hostedmodels.main('good night', function (err, result) { + t.ifError(err); + t.ok(result); + t.end(); }); }); diff --git a/test/pubsub/iam.test.js b/test/pubsub/iam.test.js index 817f4094286..bc58d74886e 100644 --- a/test/pubsub/iam.test.js +++ b/test/pubsub/iam.test.js @@ -13,51 +13,29 @@ 'use strict'; -var assert = require('assert'); -var projectId = process.env.GCLOUD_PROJECT; - +var test = require('ava'); var iamSample = require('../../pubsub/iam'); -describe('pubsub/iam', function () { - it('should run the sample', function (done) { - this.timeout(30000); - iamSample.runSample(function (err, responses) { - try { - assert.ok(!err); - // topic - var expectedTopic = 'projects/' + projectId + '/topics/messageCenter'; - assert.equal(responses[0][0].name, expectedTopic); - assert.ok(responses[0][0].iam); - // apiResponse - assert.ok(responses[0][1]); - // policy - assert.deepEqual(responses[1][0], { etag: 'ACAB' }); - // permissions - assert.deepEqual(responses[2][0], { - 'pubsub.topics.attachSubscription': true, - 'pubsub.topics.publish': true, - 'pubsub.topics.update': true - }); - // apiResponse - assert.ok(responses[2][1]); - // subscription - assert.ok(responses[3][0].on); - assert.ok(responses[3][0].iam); - // apiResponse - assert.ok(responses[3][1]); - // policy - assert.deepEqual(responses[4][0], { etag: 'ACAB' }); - // permissions - assert.deepEqual(responses[5][0], { - 'pubsub.subscriptions.consume': true, - 'pubsub.subscriptions.update': true - }); - // apiResponse - assert.ok(responses[5][1]); - done(); - } catch (err) { - done(err); - } - }); +test.cb('should run the sample', function (t) { + iamSample.main(function (err, results) { + t.ifError(err); + t.is(results.length, 8); + // Got topic and apiResponse + t.is(results[0].length, 2); + // Got policy and apiResponse + t.is(results[1].length, 2); + // Got permissions and apiResponse + t.is(results[2].length, 2); + // Got subscription and apiResponse + t.is(results[3].length, 2); + // Got policy and apiResponse + t.is(results[4].length, 2); + // Got permissions and apiResponse + t.is(results[5].length, 2); + // Got empty apiResponse + t.same(results[6], {}); + // Got empty apiResponse + t.same(results[7], {}); + t.end(); }); }); diff --git a/test/pubsub/subscription.test.js b/test/pubsub/subscription.test.js index 72289d069a9..6f03cc2727c 100644 --- a/test/pubsub/subscription.test.js +++ b/test/pubsub/subscription.test.js @@ -13,51 +13,29 @@ 'use strict'; -var assert = require('assert'); -var projectId = process.env.GCLOUD_PROJECT; - +var test = require('ava'); var subscriptionSample = require('../../pubsub/subscription'); -describe('pubsub/subscription', function () { - it('should run the sample', function (done) { - this.timeout(30000); - subscriptionSample.runSample(function (err, responses) { - try { - assert.ok(!err); - // topic - var expectedTopic = 'projects/' + projectId + '/topics/messageCenter'; - assert.equal(responses[0][0].name, expectedTopic); - assert.ok(responses[0][0].iam); - // apiResponse - assert.ok(responses[0][1]); - // subscription - assert.ok(responses[1][0].on); - assert.ok(responses[1][0].iam); - // apiResponse - assert.ok(responses[1][1]); - // topics - var foundExpectedTopic = false; - responses[2][0].forEach(function (topic) { - if (topic.name === expectedTopic) { - foundExpectedTopic = true; - return false; - } - }); - assert.ok(foundExpectedTopic, 'topic should have been created!'); - assert.ok(responses[2][0][0].iam); - // subscriptions - assert.ok(responses[3][0][0].on); - assert.ok(responses[3][0][0].iam); - // messageIds - assert.ok(typeof responses[4][0][0] === 'string'); - // apiResponse - assert.ok(responses[4][1]); - // messages - assert.equal(responses[5][0][0].data, 'Hello, world!'); - done(); - } catch (err) { - done(err); - } - }); +test.cb('should run the sample', function (t) { + subscriptionSample.main(function (err, results) { + t.ifError(err); + t.is(results.length, 8); + // Got topic and apiResponse + t.is(results[0].length, 2); + // Got subscription and apiResponse + t.is(results[1].length, 2); + // Got array of topics + t.ok(Array.isArray(results[2])); + // Got array of subscriptions + t.ok(Array.isArray(results[3])); + // Got messageIds and apiResponse + t.is(results[4].length, 2); + // Got array of messages + t.ok(Array.isArray(results[5])); + // Got empty apiResponse + t.same(results[6], {}); + // Got empty apiResponse + t.same(results[7], {}); + t.end(); }); }); diff --git a/test/storage/authSample.test.js b/test/storage/authSample.test.js index 2078e54461d..b64337ea0e6 100644 --- a/test/storage/authSample.test.js +++ b/test/storage/authSample.test.js @@ -16,28 +16,19 @@ */ 'use strict'; -var assert = require('assert'); -var authSample = require('../../storage/authSample'); +var test = require('ava'); +var authSampleExample = require('../../storage/authSample'); var projectId = process.env.GCLOUD_PROJECT; -describe('listBuckets', function () { +test.cb('should return a list of buckets', function (t) { + var bucketName = process.env.TEST_BUCKET_NAME || 'nodejs-docs-samples'; - it('should return a list of buckets', function (done) { - var bucketName = process.env.TEST_BUCKET_NAME || 'nodejs-docs-samples'; - - authSample.runExample( - projectId, - function (err, response) { - if (err) { - return done(err); - } else { - assert(response.items.length > 0, 'There should be some buckets.'); - assert.equal(response.items.filter(function (item) { - return item.name === bucketName; - }).length, 1, 'There should be a bucket named ' + bucketName); - return done(); - } - } - ); + authSampleExample.main(projectId, function (err, response) { + t.ifError(err); + t.ok(response.items.length > 0, 'There should be some buckets.'); + t.is(response.items.filter(function (item) { + return item.name === bucketName; + }).length, 1, 'There should be a bucket named ' + bucketName); + t.end(); }); });