From 4509e8eea4343994172642f0c9b308e98d089be4 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Wed, 24 Apr 2019 14:05:10 -0700 Subject: [PATCH] docs: improve the quickstart sample (#172) --- .../google-cloud-oslogin/samples/package.json | 14 ++++---- .../samples/quickstart.js | 33 +++++++------------ .../samples/test/.eslintrc.yml | 3 ++ .../samples/test/sample.test.js | 29 ++++++++++++++++ 4 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 packages/google-cloud-oslogin/samples/test/.eslintrc.yml create mode 100644 packages/google-cloud-oslogin/samples/test/sample.test.js diff --git a/packages/google-cloud-oslogin/samples/package.json b/packages/google-cloud-oslogin/samples/package.json index 013c525842c..e3d3fa5c481 100644 --- a/packages/google-cloud-oslogin/samples/package.json +++ b/packages/google-cloud-oslogin/samples/package.json @@ -1,25 +1,23 @@ { "name": "nodejs-docs-samples-os-login", - "version": "0.0.1", "license": "Apache-2.0", "author": "Google Inc.", "engines": { "node": ">=8" }, + "files": [ + "*.js" + ], "repository": "googleapis/nodejs-os-login", "private": true, - "semistandard": { - "ignore": [ - "node_modules" - ] - }, "scripts": { - "test": "node -e 'console.log(`no tests`)'" + "test": "mocha" }, "dependencies": { "@google-cloud/os-login": "^0.3.2" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0" + "chai": "^4.2.0", + "mocha": "^6.1.4" } } diff --git a/packages/google-cloud-oslogin/samples/quickstart.js b/packages/google-cloud-oslogin/samples/quickstart.js index 0a7c931c862..e1b0231764b 100644 --- a/packages/google-cloud-oslogin/samples/quickstart.js +++ b/packages/google-cloud-oslogin/samples/quickstart.js @@ -16,30 +16,19 @@ async function main() { // [START oslogin_quickstart] - if ( - !process.env.GCLOUD_PROJECT || - !process.env.GOOGLE_APPLICATION_CREDENTIALS - ) { - throw new Error( - 'Usage: GCLOUD_PROJECT= GOOGLE_APPLICATION_CREDENTIALS= node #{$0}' - ); + const {OsLoginServiceClient} = require('@google-cloud/os-login'); + const client = new OsLoginServiceClient(); + + async function quickstart() { + const [loginProfile] = await client.getLoginProfile({ + name: 'users/beckwith@google.com', + }); + console.log('Login Profile:'); + console.log(loginProfile); } - const oslogin = require('@google-cloud/os-login'); - - const projectId = process.env.GCLOUD_PROJECT; - - const client = new oslogin.OsLoginServiceClient({ - projectId: projectId, - }); - - const request = { - name: 'users/1234abcd', - }; - - const [loginProfile] = await client.getLoginProfile(request); - console.log(loginProfile); + quickstart(); // [END oslogin_quickstart] } -main().catch(console.error); +main(); diff --git a/packages/google-cloud-oslogin/samples/test/.eslintrc.yml b/packages/google-cloud-oslogin/samples/test/.eslintrc.yml new file mode 100644 index 00000000000..6db2a46c535 --- /dev/null +++ b/packages/google-cloud-oslogin/samples/test/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +env: + mocha: true diff --git a/packages/google-cloud-oslogin/samples/test/sample.test.js b/packages/google-cloud-oslogin/samples/test/sample.test.js new file mode 100644 index 00000000000..738cf170229 --- /dev/null +++ b/packages/google-cloud-oslogin/samples/test/sample.test.js @@ -0,0 +1,29 @@ +// Copyright 2019, Google LLC All rights reserved. +// +// 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 {assert} = require('chai'); +const {execSync} = require('child_process'); + +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); + +describe('sample tests', () => { + // This test current fails because it requires setup in the google-cloud-node + // project, running VMs, and a fair amount of infrastructure. + it.skip('should run the quickstart', () => { + const output = exec('node quickstart'); + assert.include(output, 'Login Profile:'); + }); +});