@@ -61,19 +61,22 @@ describe('Tests that do not require a GCS bucket', function () {
6161} ) ;
6262
6363describe ( 'Tests that require a GCS bucket' , function ( ) {
64- // Create the Google Cloud Storage bucket to run the tests.
65- // Caution: The 'after' hook deletes the bucket. To prevent deleting user data, the 'before' hook
66- // fails if the bucket already exists.
67- let bucketCreated = false ;
64+ // Create the Google Cloud Storage bucket if it doesn't exist.
6865 before ( 'Create GCS bucket.' , function ( done ) {
69- bucket . create ( {
70- location : storageLocation ,
71- storageClass : storageClass
72- } ) . then ( function ( data ) {
73- bucketCreated = true ;
74- done ( ) ;
75- } ) . catch ( error => {
76- done ( error ) ;
66+ bucket . exists ( ) . then ( function ( data ) {
67+ const exists = data [ 0 ] ;
68+ if ( ! exists ) {
69+ bucket . create ( {
70+ location : storageLocation ,
71+ storageClass : storageClass
72+ } ) . then ( function ( data ) {
73+ done ( ) ;
74+ } ) . catch ( error => {
75+ done ( error ) ;
76+ } ) ;
77+ } else {
78+ done ( ) ;
79+ }
7780 } ) ;
7881 } ) ;
7982 it ( 'should return a successful response.' , function ( done ) {
@@ -100,10 +103,7 @@ describe('Tests that require a GCS bucket', function () {
100103 } )
101104 . expect ( / " t r a n s c r i p t i o n " : " t h i s i s a t e s t p l e a s e t r a n s l a t e t h i s m e s s a g e " / , done ) ;
102105 } ) ;
103- after ( 'Delete GCS bucket, files, and other test artifacts.' , function ( done ) {
104- if ( ! bucketCreated ) {
105- done ( new Error ( 'The before hook did not create the bucket, abort cleanup' ) ) ;
106- }
106+ after ( 'Delete created files in the bucket and other test artifacts.' , function ( done ) {
107107 // Load the response from the successful test.
108108 const responseBody = JSON . parse ( fs . readFileSync ( 'responseBody.test.json' ) ) ;
109109
@@ -116,9 +116,6 @@ describe('Tests that require a GCS bucket', function () {
116116 }
117117
118118 Promise . all ( deletedFiles ) . then ( ( ) => {
119- // Delete the empty bucket.
120- return bucket . delete ( ) ;
121- } ) . then ( ( ) => {
122119 // Delete the file that stores the response from the successful test.
123120 fs . unlinkSync ( 'responseBody.test.json' ) ;
124121 done ( ) ;
0 commit comments