diff --git a/package.json b/package.json index 8428d0d55..d8cb19c49 100644 --- a/package.json +++ b/package.json @@ -30,21 +30,19 @@ "storage" ], "scripts": { - "codecov": "nyc report --reporter=json && codecov -f .coverage/*.json", "docs": "jsdoc -c .jsdoc.js", "generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json", "system-test": "mocha build/system-test --timeout 600000", "presystem-test": "npm run compile", - "test-only": "nyc mocha build/test", - "test": "npm run test-only", - "pretest-only": "npm run compile", + "test": "nyc mocha build/test", + "pretest": "npm run compile", "lint": "eslint samples/ && gts check", "samples-test": "npm link && cd samples/ && npm link ../ && npm test && cd ../", "all-test": "npm test && npm run system-test && npm run samples-test", "check": "gts check", "clean": "gts clean", "compile": "tsc -p .", - "fix": "gts fix && eslint --fix samples/**/*.js", + "fix": "gts fix && eslint --fix '**/*.js'", "prepare": "npm run compile" }, "dependencies": { diff --git a/samples/package.json b/samples/package.json index 991ce361f..834be36f4 100644 --- a/samples/package.json +++ b/samples/package.json @@ -1,7 +1,6 @@ { "name": "@google-cloud/storage-samples", "description": "Samples for the Cloud Storage Client Library for Node.js.", - "version": "0.0.1", "license": "Apache-2.0", "author": "Google Inc.", "engines": { @@ -9,28 +8,22 @@ }, "repository": "googleapis/nodejs-storage", "private": true, - "nyc": { - "exclude": [ - "**/*.test.js" - ] - }, + "files": [ + "*.js" + ], "scripts": { "cleanup": "node scripts/cleanup", - "mocha": "mocha system-test/*.js --timeout 600000", - "cover": "nyc --reporter=lcov --cache mocha system-test/*.js --timeout 600000 && nyc report", - "test": "npm run cover" + "test": "mocha system-test/*.js --timeout 600000" }, "dependencies": { - "@google-cloud/pubsub": "*", + "@google-cloud/pubsub": "^0.22.2", "@google-cloud/storage": "^2.3.3", "uuid": "^3.3.2", "yargs": "^12.0.0" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", - "mocha": "^5.2.0", - "nyc": "^13.0.0", - "proxyquire": "^2.0.1", - "sinon": "^7.0.0" + "chai": "^4.2.0", + "execa": "^1.0.0", + "mocha": "^5.2.0" } } diff --git a/samples/quickstart.js b/samples/quickstart.js index bae414e35..8ca9ca62c 100644 --- a/samples/quickstart.js +++ b/samples/quickstart.js @@ -16,29 +16,21 @@ 'use strict'; // [START storage_quickstart] -// Imports the Google Cloud client library -const {Storage} = require('@google-cloud/storage'); +async function quickstart( + projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID + bucketName = 'my-new-bucket' // The name for the new bucket +) { + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); -// Your Google Cloud Platform project ID -const projectId = 'YOUR_PROJECT_ID'; + // Creates a client + const storage = new Storage({projectId}); -// Creates a client -const storage = new Storage({ - projectId: projectId, -}); - -// The name for the new bucket -const bucketName = 'my-new-bucket'; - -// Creates the new bucket -async function createBucket() { + // Creates the new bucket await storage.createBucket(bucketName); console.log(`Bucket ${bucketName} created.`); } - -try { - createBucket(); -} catch (err) { - console.error('ERROR:', err); -} // [END storage_quickstart] + +const args = process.argv.slice(2); +quickstart(...args).catch(console.error); diff --git a/samples/system-test/.eslintrc.yml b/samples/system-test/.eslintrc.yml index df8f4a1de..c675d12ae 100644 --- a/samples/system-test/.eslintrc.yml +++ b/samples/system-test/.eslintrc.yml @@ -1,6 +1,3 @@ --- env: - mocha : true -rules: - node/no-unpublished-require: off - no-empty: off + mocha : true diff --git a/samples/system-test/acl.test.js b/samples/system-test/acl.test.js index d05f2e950..e98f150fa 100644 --- a/samples/system-test/acl.test.js +++ b/samples/system-test/acl.test.js @@ -15,179 +15,131 @@ 'use strict'; -const path = require(`path`); -const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const path = require('path'); const storage = new Storage(); - -const cwd = path.join(__dirname, `..`); const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const bucket = storage.bucket(bucketName); -const userEmail = `jdobry@google.com`; -const fileName = `test.txt`; -const filePath = path.join(__dirname, `../resources`, fileName); -const cmd = `node acl.js`; +const userEmail = 'jdobry@google.com'; +const fileName = 'test.txt'; +const filePath = path.join(__dirname, '..', 'resources', fileName); +const cmd = 'node acl.js'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; -before(tools.checkCredentials); before(async () => { await bucket.create(); await bucket.upload(filePath); }); after(async () => { - // Try deleting all files twice try { await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error + } catch (err) { + // ignore error + } try { await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error + } catch (err) { + // ignore error + } try { await bucket.delete(); - } catch (err) {} // ignore error + } catch (err) { + // ignore error + } }); -it(`should print acl for a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} print-bucket-acl ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`OWNER: project-editors-`), - true - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`OWNER: project-owners-`), - true - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`READER: project-viewers-`), - true - ); +it('should print acl for a bucket', async () => { + const out = await exec(`${cmd} print-bucket-acl ${bucketName}`); + assert.match(out, /OWNER: project-editors-/); + assert.match(out, /OWNER: project-owners-/); + assert.match(out, /READER: project-viewers-/); }); -it(`should print acl for a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} print-file-acl ${bucketName} ${fileName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`OWNER: project-editors-`), - true - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`OWNER: project-owners-`), - true - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`READER: project-viewers-`), - true - ); +it('should print acl for a file', async () => { + const out = await exec(`${cmd} print-file-acl ${bucketName} ${fileName}`); + assert.match(out, /OWNER: project-editors-/); + assert.match(out, /OWNER: project-owners-/); + assert.match(out, /READER: project-viewers-/); }); -it(`should print a user's acl for a bucket`, async () => { +it('should print a users acl for a bucket', async () => { await bucket.acl.readers.addUser(userEmail); - const results = await tools.runAsyncWithIO( - `${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`READER: user-${userEmail}`), - true + const out = await exec( + `${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}` ); + assert.match(out, new RegExp(`READER: user-${userEmail}`)); await bucket.acl.readers.deleteUser(userEmail); }); -it(`should add a user as an owner on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} add-bucket-owner ${bucketName} ${userEmail}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Added user ${userEmail} as an owner on bucket ${bucketName}.` - ), - true +it('should add a user as an owner on a bucket', async () => { + const out = await exec(`${cmd} add-bucket-owner ${bucketName} ${userEmail}`); + assert.match( + out, + new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`) ); }); -it(`should remove a user from a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} remove-bucket-owner ${bucketName} ${userEmail}`, - cwd +it('should remove a user from a bucket', async () => { + const out = await exec( + `${cmd} remove-bucket-owner ${bucketName} ${userEmail}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Removed user ${userEmail} from bucket ${bucketName}.` - ), - true + assert.match( + out, + new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`) ); }); -it(`should add a user as a default owner on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`, - cwd +it('should add a user as a default owner on a bucket', async () => { + const out = await exec( + `${cmd} add-bucket-default-owner ${bucketName} ${userEmail}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Added user ${userEmail} as an owner on bucket ${bucketName}.` - ), - true + assert.match( + out, + new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`) ); }); -it(`should remove a default user from a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`, - cwd +it('should remove a default user from a bucket', async () => { + const out = await exec( + `${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Removed user ${userEmail} from bucket ${bucketName}.` - ), - true + assert.match( + out, + new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`) ); }); -it(`should print a user's acl for a file`, async () => { +it('should print a users acl for a file', async () => { await bucket.file(fileName).acl.readers.addUser(userEmail); - const results = await tools.runAsyncWithIO( - `${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`READER: user-${userEmail}`), - true + const out = await exec( + `${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}` ); + assert.match(out, new RegExp(`READER: user-${userEmail}`)); await bucket.file(fileName).acl.readers.deleteUser(userEmail); }); -it(`should add a user as an owner on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`, - cwd +it('should add a user as an owner on a bucket', async () => { + const out = await exec( + `${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Added user ${userEmail} as an owner on file ${fileName}.` - ), - true + assert.match( + out, + new RegExp(`Added user ${userEmail} as an owner on file ${fileName}.`) ); }); -it(`should remove a user from a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`, - cwd +it('should remove a user from a bucket', async () => { + const out = await exec( + `${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Removed user ${userEmail} from file ${fileName}.` - ), - true + assert.match( + out, + new RegExp(`Removed user ${userEmail} from file ${fileName}.`) ); }); diff --git a/samples/system-test/bucketLock.test.js b/samples/system-test/bucketLock.test.js index 1f5b74407..261cd5c81 100644 --- a/samples/system-test/bucketLock.test.js +++ b/samples/system-test/bucketLock.test.js @@ -15,184 +15,133 @@ 'use strict'; -const path = require(`path`); -const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const path = require('path'); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); const storage = new Storage(); -const cwd = path.join(__dirname, `..`); -const cmd = `node bucketLock.js`; +const cwd = path.join(__dirname, '..'); +const cmd = 'node bucketLock.js'; const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const bucket = storage.bucket(bucketName); -const fileName = `test.txt`; +const fileName = 'test.txt'; -const uploadFilePath = path.join(cwd, `resources`, fileName); +const uploadFilePath = path.join(cwd, 'resources', fileName); +const exec = async cmd => (await execa.shell(cmd)).stdout; -before(tools.checkCredentials); before(async () => { await bucket.create(); -}); -before(async () => { await bucket.upload(uploadFilePath); }); after(async () => { try { await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error + } catch (err) { + // ignore error + } try { await bucket.delete(); - } catch (err) {} // ignore error + } catch (err) { + // ignore error + } }); -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); - -it(`should set a retention policy on a bucket`, async () => { +it('should set a retention policy on a bucket', async () => { const retentionPeriod = 5; - const results = await tools.runAsyncWithIO( - `${cmd} set-retention-policy ${bucketName} ${retentionPeriod}`, - cwd + const output = await exec( + `${cmd} set-retention-policy ${bucketName} ${retentionPeriod}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( + assert.match( + output, + new RegExp( `Bucket ${bucketName} retention period set for ${retentionPeriod} seconds.` - ), - true + ) ); }); -it(`should get a retention policy on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} get-retention-policy ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`A retention policy exists!`), - true - ); +it('should get a retention policy on a bucket', async () => { + const output = await exec(`${cmd} get-retention-policy ${bucketName}`); + assert.match(output, /A retention policy exists!/); }); -it(`should enable default event-based hold on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} enable-default-event-based-hold ${bucketName}`, - cwd +it('should enable default event-based hold on a bucket', async () => { + const output = await exec( + `${cmd} enable-default-event-based-hold ${bucketName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Default event-based hold was enabled for ${bucketName}.` - ), - true + assert.match( + output, + new RegExp(`Default event-based hold was enabled for ${bucketName}.`) ); }); -it(`should get default event-based hold on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} get-default-event-based-hold ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Default event-based hold: true.` - ), - true +it('should get default event-based hold on a bucket', async () => { + const output = await exec( + `${cmd} get-default-event-based-hold ${bucketName}` ); + assert.match(output, /Default event-based hold: true./); }); -it(`should disable default event-based hold on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} disable-default-event-based-hold ${bucketName}`, - cwd +it('should disable default event-based hold on a bucket', async () => { + const output = await exec( + `${cmd} disable-default-event-based-hold ${bucketName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Default event-based hold was disabled for ${bucketName}.` - ), - true + assert.match( + output, + new RegExp(`Default event-based hold was disabled for ${bucketName}.`) ); }); -it(`should set an event-based hold on a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} set-event-based-hold ${bucketName} ${fileName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Event-based hold was set for ${fileName}.` - ), - true +it('should set an event-based hold on a file', async () => { + const output = await exec( + `${cmd} set-event-based-hold ${bucketName} ${fileName}` ); + assert.match(output, new RegExp(`Event-based hold was set for ${fileName}.`)); }); -it(`should release an event-based hold on a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} release-event-based-hold ${bucketName} ${fileName}`, - cwd +it('should release an event-based hold on a file', async () => { + const output = await exec( + `${cmd} release-event-based-hold ${bucketName} ${fileName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Event-based hold was released for ${fileName}.` - ), - true + assert.match( + output, + new RegExp(`Event-based hold was released for ${fileName}.`) ); }); -it(`should remove a retention policy on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} remove-retention-policy ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Removed bucket ${bucketName} retention policy.` - ), - true +it('should remove a retention policy on a bucket', async () => { + const output = await exec(`${cmd} remove-retention-policy ${bucketName}`); + assert.match( + output, + new RegExp(`Removed bucket ${bucketName} retention policy.`) ); }); -it(`should set an temporary hold on a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} set-temporary-hold ${bucketName} ${fileName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Temporary hold was set for ${fileName}.` - ), - true +it('should set an temporary hold on a file', async () => { + const output = await exec( + `${cmd} set-temporary-hold ${bucketName} ${fileName}` ); + assert.match(output, new RegExp(`Temporary hold was set for ${fileName}.`)); }); -it(`should release an temporary hold on a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} release-temporary-hold ${bucketName} ${fileName}`, - cwd +it('should release an temporary hold on a file', async () => { + const output = await exec( + `${cmd} release-temporary-hold ${bucketName} ${fileName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Temporary hold was released for ${fileName}.` - ), - true + assert.match( + output, + new RegExp(`Temporary hold was released for ${fileName}.`) ); }); -it(`should lock a bucket with a retention policy`, async () => { +it('should lock a bucket with a retention policy', async () => { const retentionPeriod = 5; - await tools.runAsyncWithIO( - `${cmd} set-retention-policy ${bucketName} ${retentionPeriod}`, - cwd - ); - const results = await tools.runAsyncWithIO( - `${cmd} lock-retention-policy ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Retention policy for ${bucketName} is now locked.` - ), - true + await exec(`${cmd} set-retention-policy ${bucketName} ${retentionPeriod}`); + const output = await exec(`${cmd} lock-retention-policy ${bucketName}`); + assert.match( + output, + new RegExp(`Retention policy for ${bucketName} is now locked.`) ); }); diff --git a/samples/system-test/buckets.test.js b/samples/system-test/buckets.test.js index 3f4f7ab11..4026a879c 100644 --- a/samples/system-test/buckets.test.js +++ b/samples/system-test/buckets.test.js @@ -15,69 +15,44 @@ 'use strict'; -const path = require(`path`); const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); const storage = new Storage(); -const cwd = path.join(__dirname, `..`); const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA; const bucket = storage.bucket(bucketName); -const cmd = `node buckets.js`; +const cmd = 'node buckets.js'; +const exec = async cmd => (await execa.shell(cmd)).stdout; -before(tools.checkCredentials); after(async () => { - try { - await bucket.delete(); - } catch (err) {} // ignore error + return bucket.delete().catch(console.error); }); -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); - -it(`should create a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} create ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`Bucket ${bucketName} created.`), - true - ); +it('should create a bucket', async () => { + const output = await exec(`${cmd} create ${bucketName}`); + assert.match(output, new RegExp(`Bucket ${bucketName} created.`)); const [exists] = await bucket.exists(); assert.strictEqual(exists, true); }); -it(`should list buckets`, async () => { - await tools - .tryTest(async assert => { - const results = await tools.runAsyncWithIO(`${cmd} list`, cwd); - const output = results.stdout + results.stderr; - assert( - output.includes(`Buckets:`), - `"${output}" should include "Buckets:"` - ); - assert( - output.includes(bucketName), - `"${output}" should include "${bucketName}"` - ); - }) - .start(); +it('should list buckets', async () => { + const output = await exec(`${cmd} list`); + assert.match(output, /Buckets:/); + assert.match(output, new RegExp(bucketName)); }); -it(`should set a bucket's default KMS key`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} enable-default-kms-key ${bucketName} ${defaultKmsKeyName}`, - cwd +it('should set a buckets default KMS key', async () => { + const output = await exec( + `${cmd} enable-default-kms-key ${bucketName} ${defaultKmsKeyName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( + assert.match( + output, + new RegExp( `Default KMS key for ${bucketName} was set to ${defaultKmsKeyName}.` - ), - true + ) ); const metadata = await bucket.getMetadata(); assert.strictEqual( @@ -86,15 +61,9 @@ it(`should set a bucket's default KMS key`, async () => { ); }); -it(`should delete a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} delete ${bucketName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes(`Bucket ${bucketName} deleted.`), - true - ); +it('should delete a bucket', async () => { + const output = await exec(`${cmd} delete ${bucketName}`); + assert.match(output, new RegExp(`Bucket ${bucketName} deleted.`)); const [exists] = await bucket.exists(); assert.strictEqual(exists, false); }); diff --git a/samples/system-test/encryption.test.js b/samples/system-test/encryption.test.js index 6ed8313fa..9f226ec88 100644 --- a/samples/system-test/encryption.test.js +++ b/samples/system-test/encryption.test.js @@ -18,12 +18,13 @@ const fs = require('fs'); const path = require('path'); const {Storage} = require('@google-cloud/storage'); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const {assert} = require('chai'); +const execa = require('execa'); const uuid = require('uuid'); +const {promisify} = require('util'); +const exec = async cmd => (await execa.shell(cmd)).stdout; const storage = new Storage(); -const cwd = path.join(__dirname, `..`); const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const bucket = storage.bucket(bucketName); const cmd = `node encryption.js`; @@ -34,87 +35,56 @@ const downloadFilePath = path.join(__dirname, `../resources/downloaded.txt`); let key; -before(tools.checkCredentials); before(async () => { await bucket.create(bucketName); }); after(async () => { - try { - // Delete the downloaded file - fs.unlinkSync(downloadFilePath); - } catch (err) { - // Swallow error - } + promisify(fs.unlink)(downloadFilePath).catch(console.error); // Try deleting all files twice, just to make sure - try { - await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error + await bucket.deleteFiles({force: true}).catch(console.error); + await bucket.deleteFiles({force: true}).catch(console.error); + await bucket.delete().catch(console.error); }); -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); - -it(`should generate a key`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} generate-encryption-key`, - cwd - ); - const output = results.stdout + results.stderr; - assert.strictEqual(output.includes(`Base 64 encoded encryption key:`), true); +it('should generate a key', async () => { + const output = await exec(`${cmd} generate-encryption-key`); + assert.match(output, /Base 64 encoded encryption key:/); const test = /^Base 64 encoded encryption key: (.+)$/; key = output.match(test)[1]; }); -it(`should upload a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} upload ${bucketName} ${filePath} ${fileName} ${key}`, - cwd +it('should upload a file', async () => { + const output = await exec( + `${cmd} upload ${bucketName} ${filePath} ${fileName} ${key}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `File ${filePath} uploaded to gs://${bucketName}/${fileName}.` - ), - true + assert.match( + output, + new RegExp(`File ${filePath} uploaded to gs://${bucketName}/${fileName}.`) ); const [exists] = await bucket.file(fileName).exists(); assert.strictEqual(exists, true); }); -it(`should download a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} download ${bucketName} ${fileName} ${downloadFilePath} ${key}`, - cwd +it('should download a file', async () => { + const output = await exec( + `${cmd} download ${bucketName} ${fileName} ${downloadFilePath} ${key}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `File ${fileName} downloaded to ${downloadFilePath}.` - ), - true + assert.match( + output, + new RegExp(`File ${fileName} downloaded to ${downloadFilePath}.`) ); fs.statSync(downloadFilePath); }); -it(`should rotate keys`, async () => { +it('should rotate keys', async () => { // Generate a new key - const generateKeyResults = await tools.runAsyncWithIO( - `${cmd} generate-encryption-key`, - cwd - ); - const output = generateKeyResults.stdout + generateKeyResults.stderr; - assert.strictEqual(output.includes(`Base 64 encoded encryption key:`), true); + let output = await exec(`${cmd} generate-encryption-key`); + assert.match(output, /Base 64 encoded encryption key:/); const test = /^Base 64 encoded encryption key: (.+)$/; const newKey = output.match(test)[1]; - - const results = await tools.runAsyncWithIO( - `${cmd} rotate ${bucketName} ${fileName} ${key} ${newKey}`, - cwd + output = await exec( + `${cmd} rotate ${bucketName} ${fileName} ${key} ${newKey}` ); - assert.strictEqual(results.stdout, 'Encryption key rotated successfully.'); + assert.strictEqual(output, 'Encryption key rotated successfully.'); }); diff --git a/samples/system-test/files.test.js b/samples/system-test/files.test.js index 1fd678c0b..ec49e5b6b 100644 --- a/samples/system-test/files.test.js +++ b/samples/system-test/files.test.js @@ -15,208 +15,148 @@ 'use strict'; -const fs = require(`fs`); -const path = require(`path`); -const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - +const fs = require('fs'); +const path = require('path'); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const {promisify} = require('util'); + +const exec = async cmd => (await execa.shell(cmd)).stdout; const storage = new Storage(); -const cwd = path.join(__dirname, `..`); +const cwd = path.join(__dirname, '..'); const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const bucket = storage.bucket(bucketName); -const fileName = `test.txt`; -const movedFileName = `test2.txt`; -const copiedFileName = `test3.txt`; +const fileName = 'test.txt'; +const movedFileName = 'test2.txt'; +const copiedFileName = 'test3.txt'; const kmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_US; -const filePath = path.join(__dirname, `../resources`, fileName); -const downloadFilePath = path.join(__dirname, `../resources/downloaded.txt`); +const filePath = path.join(cwd, 'resources', fileName); +const downloadFilePath = path.join(cwd, 'downloaded.txt'); const cmd = `node files.js`; -before(tools.checkCredentials); before(async () => { await bucket.create(); }); after(async () => { - try { - fs.unlinkSync(downloadFilePath); - } catch (err) { - console.log(err); - } + await promisify(fs.unlink)(downloadFilePath).catch(console.error); // Try deleting all files twice, just to make sure - try { - await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error + await bucket.deleteFiles({force: true}).catch(console.error); + await bucket.deleteFiles({force: true}).catch(console.error); + await bucket.delete().catch(console.error); }); -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); - -it(`should upload a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} upload ${bucketName} ${filePath}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `${filePath} uploaded to ${bucketName}.` - ), - true - ); +it('should upload a file', async () => { + const output = await exec(`${cmd} upload ${bucketName} ${filePath}`); + assert.match(output, new RegExp(`${filePath} uploaded to ${bucketName}.`)); const [exists] = await bucket.file(fileName).exists(); assert.strictEqual(exists, true); }); -it(`should upload a file with a kms key`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} upload-with-kms-key ${bucketName} ${filePath} ${kmsKeyName}`, - cwd +it('should upload a file with a kms key', async () => { + const output = await exec( + `${cmd} upload-with-kms-key ${bucketName} ${filePath} ${kmsKeyName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `${filePath} uploaded to ${bucketName} using ${kmsKeyName}.` - ), - true + assert.match( + output, + new RegExp(`${filePath} uploaded to ${bucketName} using ${kmsKeyName}.`) ); const [exists] = await bucket.file(fileName).exists(); assert.strictEqual(exists, true); }); -it(`should download a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} download ${bucketName} ${fileName} ${downloadFilePath}`, - cwd +it('should download a file', async () => { + const output = await exec( + `${cmd} download ${bucketName} ${fileName} ${downloadFilePath}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( + assert.match( + output, + new RegExp( `gs://${bucketName}/${fileName} downloaded to ${downloadFilePath}.` - ), - true + ) ); fs.statSync(downloadFilePath); }); -it(`should move a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} move ${bucketName} ${fileName} ${movedFileName}`, - cwd +it('should move a file', async () => { + const output = await exec( + `${cmd} move ${bucketName} ${fileName} ${movedFileName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( + assert.match( + output, + new RegExp( `gs://${bucketName}/${fileName} moved to gs://${bucketName}/${movedFileName}.` - ), - true + ) ); const [exists] = await bucket.file(movedFileName).exists(); assert.strictEqual(exists, true); }); -it(`should copy a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} copy ${bucketName} ${movedFileName} ${bucketName} ${copiedFileName}`, - cwd +it('should copy a file', async () => { + const output = await exec( + `${cmd} copy ${bucketName} ${movedFileName} ${bucketName} ${copiedFileName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( + assert.match( + output, + new RegExp( `gs://${bucketName}/${movedFileName} copied to gs://${bucketName}/${copiedFileName}.` - ), - true + ) ); const [exists] = await bucket.file(copiedFileName).exists(); assert.strictEqual(exists, true); }); -it(`should list files`, async () => { - await tools - .tryTest(async assert => { - const results = await tools.runAsyncWithIO( - `${cmd} list ${bucketName}`, - cwd - ); - const output = results.stdout + results.stderr; - assert(output.includes(`Files:`), `"${output}" should include "Files:"`); - assert( - output.includes(movedFileName), - `"${output}" should include "${movedFileName}"` - ); - assert( - output.includes(copiedFileName), - `"${output}" should include "${copiedFileName}"` - ); - }) - .start(); +it('should list files', async () => { + const output = await exec(`${cmd} list ${bucketName}`); + assert.match(output, /Files:/); + assert.match(output, new RegExp(movedFileName)); + assert.match(output, new RegExp(copiedFileName)); }); -it(`should list files by a prefix`, async () => { - let results = await tools.runAsyncWithIO( - `${cmd} list ${bucketName} test "/"`, - cwd - ); - let output = results.stdout + results.stderr; - assert.strictEqual(output.includes(`Files:`), true); - assert.strictEqual(output.includes(movedFileName), true); - assert.strictEqual(output.includes(copiedFileName), true); - results = await tools.runAsyncWithIO(`${cmd} list ${bucketName} foo`, cwd); - output = results.stdout + results.stderr; - assert.strictEqual(output.includes(`Files:`), true); - assert.strictEqual(output.includes(movedFileName), false); - assert.strictEqual(output.includes(copiedFileName), false); +it('should list files by a prefix', async () => { + let output = await exec(`${cmd} list ${bucketName} test "/"`); + assert.match(output, /Files:/); + assert.match(output, new RegExp(movedFileName)); + assert.match(output, new RegExp(copiedFileName)); + + output = await exec(`${cmd} list ${bucketName} foo`); + assert.match(output, /Files:/); + assert.notMatch(output, new RegExp(movedFileName)); + assert.notMatch(output, new RegExp(copiedFileName)); }); -it(`should make a file public`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} make-public ${bucketName} ${copiedFileName}`, - cwd +it('should make a file public', async () => { + const output = await exec( + `${cmd} make-public ${bucketName} ${copiedFileName}` ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `gs://${bucketName}/${copiedFileName} is now public.` - ), - true + assert.match( + output, + new RegExp(`gs://${bucketName}/${copiedFileName} is now public.`) ); }); -it(`should generate a signed URL for a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} generate-signed-url ${bucketName} ${copiedFileName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `The signed url for ${copiedFileName} is ` - ), - true +it('should generate a signed URL for a file', async () => { + const output = await exec( + `${cmd} generate-signed-url ${bucketName} ${copiedFileName}` ); + assert.match(output, new RegExp(`The signed url for ${copiedFileName} is `)); }); -it(`should get metadata for a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} get-metadata ${bucketName} ${copiedFileName}`, - cwd +it('should get metadata for a file', async () => { + const output = await exec( + `${cmd} get-metadata ${bucketName} ${copiedFileName}` ); - const output = results.stdout + results.stderr; - assert.strictEqual(output.includes(`File: ${copiedFileName}`), true); - assert.strictEqual(output.includes(`Bucket: ${bucketName}`), true); + assert.match(output, new RegExp(`File: ${copiedFileName}`)); + assert.match(output, new RegExp(`Bucket: ${bucketName}`)); }); -it(`should delete a file`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} delete ${bucketName} ${copiedFileName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `gs://${bucketName}/${copiedFileName} deleted.` - ), - true +it('should delete a file', async () => { + const output = await exec(`${cmd} delete ${bucketName} ${copiedFileName}`); + assert.match( + output, + new RegExp(`gs://${bucketName}/${copiedFileName} deleted.`) ); const [exists] = await bucket.file(copiedFileName).exists(); assert.strictEqual(exists, false); diff --git a/samples/system-test/iam.test.js b/samples/system-test/iam.test.js index c031e0b68..4de75f7ec 100644 --- a/samples/system-test/iam.test.js +++ b/samples/system-test/iam.test.js @@ -15,69 +15,57 @@ 'use strict'; -const path = require(`path`); -const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const exec = async cmd => (await execa.shell(cmd)).stdout; const storage = new Storage(); -const cwd = path.join(__dirname, `..`); const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const bucket = storage.bucket(bucketName); -const userEmail = `test@example.com`; -const cmd = `node iam.js`; -const roleName = `roles/storage.objectViewer`; +const userEmail = 'test@example.com'; +const cmd = 'node iam.js'; +const roleName = 'roles/storage.objectViewer'; -before(tools.checkCredentials); before(async () => { await bucket.create(); }); after(async () => { - try { - await bucket.delete(); - } catch (err) {} // ignore error + await bucket.delete().catch(console.error); }); -it(`should add multiple members to a role on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} add-members ${bucketName} ${roleName} "user:${userEmail}"`, - cwd +it('should add multiple members to a role on a bucket', async () => { + const output = await exec( + `${cmd} add-members ${bucketName} ${roleName} "user:${userEmail}"` ); - const output = results.stdout + results.stderr; - assert.strictEqual( + assert.ok( output.includes( `Added the following member(s) with role ${roleName} to ${bucketName}:` - ), - true + ) ); - assert.strictEqual(output.includes(`user:${userEmail}`), true); + assert.match(output, new RegExp(`user:${userEmail}`)); }); -it(`should list members of a role on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} view-members ${bucketName} "user:${userEmail}"`, - cwd +it('should list members of a role on a bucket', async () => { + const output = await exec( + `${cmd} view-members ${bucketName} "user:${userEmail}"` ); - const output = results.stdout + results.stderr; - assert.strictEqual(output.includes(`Roles for bucket ${bucketName}:`), true); - assert.strictEqual(output.includes(`Role: ${roleName}`), true); - assert.strictEqual(output.includes(`Members:`), true); - assert.strictEqual(output.includes(`user:${userEmail}`), true); + assert.match(output, new RegExp(`Roles for bucket ${bucketName}:`)); + assert.match(output, new RegExp(`Role: ${roleName}`)); + assert.match(output, new RegExp(`Members:`)); + assert.match(output, new RegExp(`user:${userEmail}`)); }); -it(`should remove multiple members from a role on a bucket`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} remove-members ${bucketName} ${roleName} "user:${userEmail}"`, - cwd +it('should remove multiple members from a role on a bucket', async () => { + const output = await exec( + `${cmd} remove-members ${bucketName} ${roleName} "user:${userEmail}"` ); - const output = results.stdout + results.stderr; - assert.strictEqual( + assert.ok( output.includes( `Removed the following member(s) with role ${roleName} from ${bucketName}:` - ), - true + ) ); - assert.strictEqual(output.includes(`user:${userEmail}`), true); + assert.match(output, new RegExp(`user:${userEmail}`)); }); diff --git a/samples/system-test/notifications.test.js b/samples/system-test/notifications.test.js index fc3454edf..348802153 100644 --- a/samples/system-test/notifications.test.js +++ b/samples/system-test/notifications.test.js @@ -15,15 +15,14 @@ 'use strict'; -const path = require(`path`); const {PubSub} = require('@google-cloud/pubsub'); -const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const exec = async cmd => (await execa.shell(cmd)).stdout; const storage = new Storage(); -const cwd = path.join(__dirname, `..`); const bucketName = `nodejs-storage-samples-${uuid.v4()}`; const bucket = storage.bucket(bucketName); const notificationId = '1'; @@ -31,7 +30,7 @@ const notification = bucket.notification(notificationId); const topicName = `nodejs-storage-samples-${uuid.v4()}`; const pubsub = new PubSub(); const topic = pubsub.topic(topicName); -const cmd = `node notifications.js`; +const cmd = 'node notifications.js'; before(async () => { await bucket.create(); @@ -47,133 +46,50 @@ before(async () => { }); after(async () => { - try { - await bucket.delete(); - } catch (e) {} - - try { - await topic.delete(); - } catch (e) {} + await bucket.delete().catch(console.error); + await topic.delete().catch(console.error); }); -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); - -it(`should create a notification`, async () => { - const results = await tools.runAsyncWithIO( - `${cmd} create ${bucketName} ${topicName}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Notification subscription created.` - ), - true - ); +it('should create a notification', async () => { + const output = await exec(`${cmd} create ${bucketName} ${topicName}`); + assert.match(output, /Notification subscription created./); const [exists] = await notification.exists(); assert.strictEqual(exists, true); }); -it(`should list notifications`, async () => { - await tools - .tryTest(async assert => { - const results = await tools.runAsyncWithIO( - `${cmd} list ${bucketName}`, - cwd - ); - const output = results.stdout + results.stderr; - assert( - output.includes(`Notifications:`), - `"${output}" should include "Notifications:"` - ); - assert( - output.includes(notificationId), - `"${output}" should include "${notificationId}"` - ); - }) - .start(); +it('should list notifications', async () => { + const output = await exec(`${cmd} list ${bucketName}`); + assert.match(output, /Notifications:/); + assert.match(output, new RegExp(notificationId)); }); it('should get metadata', async () => { - await tools - .tryTest(async assert => { - const metadata = await notification.getMetadata(); - const results = await tools.runAsyncWithIO( - `${cmd} get-metadata ${bucketName} ${notificationId}`, - cwd - ); - const output = results.stdout + results.stderr; - assert(output.includes(`ID:`), `"${output}" should include "ID:"`); - assert( - output.includes(metadata.id), - `"${output}" should include "${metadata.id}"` - ); - assert(output.includes(`Topic:`), `"${output}" should include "Topic:"`); - assert( - output.includes(metadata.topic), - `"${output}" should include "${metadata.topic}"` - ); - assert( - output.includes(`Event Types:`), - `"${output}" should include "Event Types:"` - ); - assert( - output.includes(metadata.event_types), - `"${output}" should include "${metadata.event_types}"` - ); - assert( - output.includes(`Custom Attributes:`), - `"${output}" should include "Custom Attributes:"` - ); - assert( - output.includes(metadata.custom_attributes), - `"${output}" should include "${metadata.custom_attributes}"` - ); - assert( - output.includes(`Payload Format:`), - `"${output}" should include "Payload Format:"` - ); - assert( - output.includes(metadata.payload_format), - `"${output}" should include "${metadata.payload_format}"` - ); - assert( - output.includes(`Object Name Prefix:`), - `"${output}" should include "Object Name Prefix:"` - ); - assert( - output.includes(metadata.object_name_prefix), - `"${output}" should include "${metadata.object_name_prefix}"` - ); - assert(output.includes(`Etag:`), `"${output}" should include "Etag:"`); - assert( - output.includes(`Self Link:`), - `"${output}" should include "Self Link:"` - ); - assert( - output.includes(metadata.selfLink), - `"${output}" should include "${metadata.selfLink}"` - ); - assert(output.includes(`Kind:`), `"${output}" should include "Kind:"`); - assert( - output.includes(metadata.kind), - `"${output}" should include "${metadata.kind}"` - ); - }) - .start(); + const metadata = await notification.getMetadata(); + const output = await exec( + `${cmd} get-metadata ${bucketName} ${notificationId}` + ); + assert.match(output, /ID:/); + assert.match(output, new RegExp(metadata.id)); + assert.match(output, /Topic:/); + assert.match(output, new RegExp(metadata.topic)); + assert.match(output, /Event Types:/); + assert.match(output, new RegExp(metadata.event_types)); + assert.match(output, /Custom Attributes:/); + assert.match(output, new RegExp(metadata.custom_attributes)); + assert.match(output, /Payload Format:/); + assert.match(output, new RegExp(metadata.payload_format)); + assert.match(output, /Object Name Prefix:/); + assert.match(output, new RegExp(metadata.object_name_prefix)); + assert.match(output, /Etag:/); + assert.match(output, /Self Link:/); + assert.match(output, new RegExp(metadata.selfLink)); + assert.match(output, /Kind:/); + assert.match(output, new RegExp(metadata.kind)); }); it('should delete a notification', async () => { - const results = await tools.runAsyncWithIO( - `${cmd} delete ${bucketName} ${notificationId}`, - cwd - ); - assert.strictEqual( - (results.stdout + results.stderr).includes( - `Notification ${notificationId} deleted.` - ), - true - ); + const output = await exec(`${cmd} delete ${bucketName} ${notificationId}`); + assert.match(output, new RegExp(`Notification ${notificationId} deleted.`)); const [exists] = await notification.exists(); assert.strictEqual(exists, false); }); diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 26a244d0a..704796dcb 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -15,51 +15,23 @@ 'use strict'; -const proxyquire = require(`proxyquire`).noPreserveCache(); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const {Storage} = proxyquire(`@google-cloud/storage`, {}); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); +const projectId = process.env.GCLOUD_PROJECT; const bucketName = `nodejs-storage-samples-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); -before(tools.stubConsole); after(async () => { - tools.restoreConsole(); - try { - await bucket.delete(); - } catch (err) {} // ignore error + const bucket = storage.bucket(bucketName); + await bucket.delete().catch(console.error); }); -it(`should create a bucket`, async () => { - const expectedBucketName = `my-new-bucket`; - - const StorageMock = class { - createBucket(_bucketName) { - assert.strictEqual(_bucketName, expectedBucketName); - - return bucket.create().then(([bucket]) => { - assert.notStrictEqual(bucket, undefined); - assert.strictEqual(bucket.name, bucketName); - - setTimeout(() => { - assert.strictEqual(console.log.calledOnce, true); - assert.deepStrictEqual(console.log.firstCall.args, [ - `Bucket ${expectedBucketName} created.`, - ]); - }, 200); - - return [bucket]; - }); - } - }; - - proxyquire(`../quickstart`, { - '@google-cloud/storage': { - Storage: StorageMock, - }, - }); +it('should run the quickstart', async () => { + const {stdout} = await execa.shell( + `node quickstart ${projectId} ${bucketName}` + ); + assert.match(stdout, /Bucket .* created./); }); diff --git a/samples/system-test/requesterPays.test.js b/samples/system-test/requesterPays.test.js index 1a410a89d..6c8ce38cd 100644 --- a/samples/system-test/requesterPays.test.js +++ b/samples/system-test/requesterPays.test.js @@ -15,103 +15,89 @@ 'use strict'; -const fs = require(`fs`); -const path = require(`path`); -const {Storage} = require(`@google-cloud/storage`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const fs = require('fs'); +const {Storage} = require('@google-cloud/storage'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const path = require('path'); +const {promisify} = require('util'); +const exec = async cmd => (await execa.shell(cmd)).stdout; const storage = new Storage(); -const cwd = path.join(__dirname, `..`); -const cmd = `node requesterPays.js`; +const cwd = path.join(__dirname, '..'); +const cmd = 'node requesterPays.js'; const bucketName = `nodejs-storage-samples-${uuid.v4()}`; -const fileName = `test.txt`; +const fileName = 'test.txt'; const bucket = storage.bucket(bucketName); -const uploadFilePath = path.join(cwd, `resources`, fileName); +const uploadFilePath = path.join(cwd, 'resources', fileName); const downloadFilePath = path.join(__dirname, `test_${uuid.v4()}.txt`); before(async () => { - tools.checkCredentials(); await bucket.create(); - // Upload a test file (to download later) await bucket.upload(uploadFilePath); }); + after(async () => { - try { - fs.unlinkSync(downloadFilePath); - } catch (err) { - console.log(err); - } + await promisify(fs.unlink)(downloadFilePath).catch(console.error); // Try deleting all files twice, just to make sure - try { - await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({force: true}); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error + await bucket.deleteFiles({force: true}).catch(console.error); + await bucket.deleteFiles({force: true}).catch(console.error); + await bucket.delete().catch(console.error); }); it.skip(`should error on requester-pays requests if they are disabled`, async () => { - const result = await tools.runAsyncWithIO( - `${cmd} download ${bucketName} ${fileName} ${downloadFilePath}`, - cwd + const result = await execa.shell( + `${cmd} download ${bucketName} ${fileName} ${downloadFilePath}` ); - console.log(result); assert.ok(result.stderr); - assert.strictEqual( - result.stderr.includes( - `User project prohibited for non requester pays bucket` - ), - true + assert.match( + result.stderr, + /User project prohibited for non requester pays bucket/ ); }); it(`should fetch requester-pays status on a default bucket`, async () => { - const output = await tools.runAsync(`${cmd} get-status ${bucketName}`, cwd); + const out = await exec(`${cmd} get-status ${bucketName}`); assert.strictEqual( - output, + out, `Requester-pays requests are disabled for bucket ${bucketName}.` ); }); it(`should enable requester-pays requests`, async () => { - const output = await tools.runAsync(`${cmd} enable ${bucketName}`, cwd); + const out = await exec(`${cmd} enable ${bucketName}`); assert.strictEqual( - output, + out, `Requester-pays requests have been enabled for bucket ${bucketName}.` ); }); it(`should fetch requester-pays status on a modified bucket`, async () => { - const output = await tools.runAsync(`${cmd} get-status ${bucketName}`, cwd); + const out = await exec(`${cmd} get-status ${bucketName}`); assert.strictEqual( - output, + out, `Requester-pays requests are enabled for bucket ${bucketName}.` ); }); it(`should download a file using requester-pays requests`, async () => { - const output = await tools.runAsync( - `${cmd} download ${bucketName} ${fileName} ${downloadFilePath}`, - cwd + const out = await exec( + `${cmd} download ${bucketName} ${fileName} ${downloadFilePath}` ); assert.strictEqual( - output, + out, `gs://${bucketName}/${fileName} downloaded to ${downloadFilePath} using requester-pays requests.` ); fs.statSync(downloadFilePath); }); it(`should disable requester-pays requests`, async () => { - const output = await tools.runAsync(`${cmd} disable ${bucketName}`, cwd); + const out = await exec(`${cmd} disable ${bucketName}`); assert.strictEqual( - output, + out, `Requester-pays requests have been disabled for bucket ${bucketName}.` ); });