1616'use strict' ;
1717
1818const { PubSub} = require ( '@google-cloud/pubsub' ) ;
19+ const assertRejects = require ( 'assert' ) . rejects ;
1920const { assert} = require ( 'chai' ) ;
2021const cp = require ( 'child_process' ) ;
2122const uuid = require ( 'uuid' ) ;
2223
2324const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
25+ const execPromise = cmd =>
26+ new Promise ( ( resolve , reject ) => {
27+ cp . exec ( cmd , { encoding : 'utf-8' } , ( err , stdout , stderr ) => {
28+ if ( err ) {
29+ err . stderr = stderr ;
30+ return reject ( err ) ;
31+ }
32+ resolve ( stdout ) ;
33+ } ) ;
34+ } ) ;
2435
2536describe ( 'subscriptions' , ( ) => {
2637 const projectId = process . env . GCLOUD_PROJECT ;
@@ -58,7 +69,7 @@ describe('subscriptions', () => {
5869 const output = execSync (
5970 `${ cmd } create ${ topicNameOne } ${ subscriptionNameOne } `
6071 ) ;
61- assert . strictEqual ( output , `Subscription ${ subscriptionNameOne } created.` ) ;
72+ assert . include ( output , `Subscription ${ subscriptionNameOne } created.` ) ;
6273 const [ subscriptions ] = await pubsub . topic ( topicNameOne ) . getSubscriptions ( ) ;
6374 assert . strictEqual ( subscriptions [ 0 ] . name , fullSubscriptionNameOne ) ;
6475 } ) ;
@@ -67,7 +78,7 @@ describe('subscriptions', () => {
6778 const output = execSync (
6879 `${ cmd } create-push ${ topicNameOne } ${ subscriptionNameTwo } `
6980 ) ;
70- assert . strictEqual ( output , `Subscription ${ subscriptionNameTwo } created.` ) ;
81+ assert . include ( output , `Subscription ${ subscriptionNameTwo } created.` ) ;
7182 const [ subscriptions ] = await pubsub . topic ( topicNameOne ) . getSubscriptions ( ) ;
7283 assert ( subscriptions . some ( s => s . name === fullSubscriptionNameTwo ) ) ;
7384 } ) ;
@@ -76,7 +87,7 @@ describe('subscriptions', () => {
7687 const output = execSync (
7788 `${ cmd } modify-config ${ topicNameTwo } ${ subscriptionNameTwo } `
7889 ) ;
79- assert . strictEqual (
90+ assert . include (
8091 output ,
8192 `Modified push config for subscription ${ subscriptionNameTwo } .`
8293 ) ;
@@ -89,7 +100,7 @@ describe('subscriptions', () => {
89100 `\nTopic: ${ fullTopicNameOne } ` +
90101 `\nPush config: ` +
91102 `\nAck deadline: 10s` ;
92- assert . strictEqual ( output , expected ) ;
103+ assert . include ( output , expected ) ;
93104 } ) ;
94105
95106 it ( 'should list all subscriptions' , async ( ) => {
@@ -116,7 +127,7 @@ describe('subscriptions', () => {
116127
117128 it ( 'should listen for messages synchronously' , async ( ) => {
118129 pubsub . topic ( topicNameOne ) . publish ( Buffer . from ( `Hello, world!` ) ) ;
119- const output = execSync (
130+ const output = await execPromise (
120131 `${ cmd } sync-pull ${ projectId } ${ subscriptionNameOne } `
121132 ) ;
122133 assert . match ( output , / D o n e ./ ) ;
@@ -184,9 +195,10 @@ describe('subscriptions', () => {
184195 } ) ;
185196
186197 it ( 'should listen for error messages' , async ( ) => {
187- assert . throws ( ( ) => {
188- execSync ( `${ cmd } listen-errors nonexistent-subscription` ) ;
189- } , / R e s o u r c e n o t f o u n d / ) ;
198+ assertRejects (
199+ ( ) => execPromise ( `${ cmd } listen-errors nonexistent-subscription` ) ,
200+ / R e s o u r c e n o t f o u n d /
201+ ) ;
190202 } ) ;
191203
192204 it ( 'should set the IAM policy for a subscription' , async ( ) => {
@@ -212,7 +224,7 @@ describe('subscriptions', () => {
212224 . subscription ( subscriptionNameOne )
213225 . iam . getPolicy ( ) ;
214226 const output = execSync ( `${ cmd } get-policy ${ subscriptionNameOne } ` ) ;
215- assert . strictEqual (
227+ assert . include (
216228 output ,
217229 `Policy for subscription: ${ JSON . stringify ( results [ 0 ] . bindings ) } .`
218230 ) ;
@@ -225,7 +237,7 @@ describe('subscriptions', () => {
225237
226238 it ( 'should delete a subscription' , async ( ) => {
227239 const output = execSync ( `${ cmd } delete ${ subscriptionNameOne } ` ) ;
228- assert . strictEqual ( output , `Subscription ${ subscriptionNameOne } deleted.` ) ;
240+ assert . include ( output , `Subscription ${ subscriptionNameOne } deleted.` ) ;
229241 const [ subscriptions ] = await pubsub . getSubscriptions ( ) ;
230242 assert . ok ( subscriptions ) ;
231243 assert ( subscriptions . every ( s => s . name !== fullSubscriptionNameOne ) ) ;
@@ -235,7 +247,7 @@ describe('subscriptions', () => {
235247 const output = execSync (
236248 `${ cmd } create-flow ${ topicNameTwo } ${ subscriptionNameFour } -m 5 -b 1024`
237249 ) ;
238- assert . strictEqual (
250+ assert . include (
239251 output ,
240252 `Subscription ${ fullSubscriptionNameFour } created with a maximum of 5 unprocessed messages.`
241253 ) ;
0 commit comments