Skip to content

Commit e8b99e5

Browse files
committed
Test creates GCS bucket if it doesn't exist.
To prevent accidentally deleting user files, the `after` hook no longer removes the bucket. It only removes files created during the test.
1 parent 04cd760 commit e8b99e5

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

functions/speech-to-speech/test/sample.integration.http.test.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,22 @@ describe('Tests that do not require a GCS bucket', function () {
6161
});
6262

6363
describe('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(/"transcription":"this is a test please translate this message"/, 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

Comments
 (0)