From b9de0d2fcead05877f9a644e77a8df4ed9d9e189 Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Fri, 24 Aug 2018 15:51:15 -0700 Subject: [PATCH 1/3] Added sample for Using Environment Variables. --- .gitignore | 1 + functions/env_vars/README.md | 27 ++++++++++++++++++ functions/env_vars/index.js | 23 +++++++++++++++ functions/env_vars/package.json | 30 ++++++++++++++++++++ functions/env_vars/test/index.test.js | 41 +++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 functions/env_vars/README.md create mode 100644 functions/env_vars/index.js create mode 100644 functions/env_vars/package.json create mode 100644 functions/env_vars/test/index.test.js diff --git a/.gitignore b/.gitignore index a27c60b771..23737c39bd 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ logs/ .nyc_output yarn.lock package-lock.json +.DS_Store diff --git a/functions/env_vars/README.md b/functions/env_vars/README.md new file mode 100644 index 0000000000..3fd2fffcc8 --- /dev/null +++ b/functions/env_vars/README.md @@ -0,0 +1,27 @@ +Google Cloud Platform logo + +# Google Cloud Functions - Using Environment Variables sample + +See: + +* [Cloud Functions Using Environment Variables tutorial][tutorial] +* [Cloud Functions Using Environment Variables sample source code][code] + +[tutorial]: https://cloud.google.com/functions/docs/env-var +[code]: index.js + +## Deploy and run the sample + +See the [Cloud Functions Using Environment Variables tutorial][tutorial]. + +## Run the tests + +1. Read and follow the [prerequisites](../../#how-to-run-the-tests). + +2. Install dependencies: + + npm install + +3. Run the tests: + + npm test diff --git a/functions/env_vars/index.js b/functions/env_vars/index.js new file mode 100644 index 0000000000..20593368cc --- /dev/null +++ b/functions/env_vars/index.js @@ -0,0 +1,23 @@ +/** + * Copyright 2016, Google, LLC. + * 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 functions_env_vars] +exports.envVar = (req, res) => { + // Sends 'bar' as response + res.send(process.env.Foo); +}; +// [END functions_env_vars] diff --git a/functions/env_vars/package.json b/functions/env_vars/package.json new file mode 100644 index 0000000000..8205d53e3d --- /dev/null +++ b/functions/env_vars/package.json @@ -0,0 +1,30 @@ +{ + "name": "nodejs-docs-samples-env-vars", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=4.3.2" + }, + "scripts": { + "lint": "repo-tools lint", + "pretest": "npm run lint", + "test": "ava -T 20s --verbose test/*.test.js" + }, + "dependencies": {}, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "2.2.1", + "ava": "0.25.0", + "semistandard": "^12.0.1", + "sinon": "4.4.2" + }, + "cloud-repo-tools": { + "requiresKeyFile": true, + "requiresProjectId": true + } +} diff --git a/functions/env_vars/test/index.test.js b/functions/env_vars/test/index.test.js new file mode 100644 index 0000000000..57469ac4cd --- /dev/null +++ b/functions/env_vars/test/index.test.js @@ -0,0 +1,41 @@ +/** + * Copyright 2016, Google, LLC. + * 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 sinon = require(`sinon`); +const test = require(`ava`); +const functions = require(`../`); + +function getMocks () { + const req = {}; + const res = { + send: sinon.stub().returnsThis() + }; + + return { + req: req, + res: res + }; +} + +test.serial(`should read env vars`, (t) => { + const mocks = getMocks(); + process.env['Foo'] = 'bar'; + + functions.envVar(mocks.req, mocks.res); + + t.true(mocks.res.send.calledWith('bar')); +}); From f40834d8752afdf5071ab851a2292c4fbd4c4b29 Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Fri, 24 Aug 2018 16:15:24 -0700 Subject: [PATCH 2/3] Minor fix. --- functions/env_vars/index.js | 2 +- functions/env_vars/test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/env_vars/index.js b/functions/env_vars/index.js index 20593368cc..51f5fd5c48 100644 --- a/functions/env_vars/index.js +++ b/functions/env_vars/index.js @@ -18,6 +18,6 @@ // [START functions_env_vars] exports.envVar = (req, res) => { // Sends 'bar' as response - res.send(process.env.Foo); + res.send(process.env.FOO); }; // [END functions_env_vars] diff --git a/functions/env_vars/test/index.test.js b/functions/env_vars/test/index.test.js index 57469ac4cd..6953dc02cd 100644 --- a/functions/env_vars/test/index.test.js +++ b/functions/env_vars/test/index.test.js @@ -33,7 +33,7 @@ function getMocks () { test.serial(`should read env vars`, (t) => { const mocks = getMocks(); - process.env['Foo'] = 'bar'; + process.env['FOO'] = 'bar'; functions.envVar(mocks.req, mocks.res); From 86c0930dcbb6815655a0d2b68276824976f1a8ad Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 27 Aug 2018 10:29:49 -0700 Subject: [PATCH 3/3] Remove .DS_Store --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 23737c39bd..a27c60b771 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,3 @@ logs/ .nyc_output yarn.lock package-lock.json -.DS_Store