TechDocs/GCS: Use async/await and add more test

This commit is contained in:
Himanshu Mishra
2021-02-18 21:33:53 +01:00
parent 4b3500021c
commit 9ff646b6a8
4 changed files with 80 additions and 38 deletions
@@ -82,11 +82,7 @@ beforeEach(() => {
describe('AwsS3Publish', () => {
describe('publish', () => {
afterEach(() => {
mockFs.restore();
});
it('should publish a directory', async () => {
beforeEach(() => {
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
@@ -99,6 +95,15 @@ describe('AwsS3Publish', () => {
},
},
});
});
afterEach(() => {
mockFs.restore();
});
it('should publish a directory', async () => {
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
expect(
await publisher.publish({
@@ -116,18 +121,14 @@ describe('AwsS3Publish', () => {
'to',
'generatedDirectory',
);
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
mockFs({
[entityRootDir]: {
'index.html': '',
'404.html': '',
assets: {
'main.css': '',
},
},
});
const entity = createMockEntity();
await expect(
publisher.publish({
entity,
directory: wrongPathToGeneratedDirectory,
}),
).rejects.toThrowError();
await publisher
.publish({
@@ -139,7 +140,7 @@ describe('AwsS3Publish', () => {
// Can not do exact error message match due to mockFs adding unexpected characters in the path when throwing the error
// Issue reported https://github.com/tschaub/mock-fs/issues/118
expect.stringContaining(
`Unable to upload file(s) to AWS S3. Error Failed to read template directory: ENOENT, no such file or directory`,
`Unable to upload file(s) to AWS S3. Error: Failed to read template directory: ENOENT, no such file or directory`,
),
);
expect(error.message).toEqual(
@@ -172,7 +172,7 @@ export class AwsS3Publish implements PublisherBase {
);
return;
} catch (e) {
const errorMessage = `Unable to upload file(s) to AWS S3. Error ${e.message}`;
const errorMessage = `Unable to upload file(s) to AWS S3. ${e}`;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
@@ -71,11 +71,7 @@ beforeEach(async () => {
});
describe('GoogleGCSPublish', () => {
afterEach(() => {
mockFs.restore();
});
it('should publish a directory', async () => {
beforeEach(() => {
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
@@ -88,6 +84,15 @@ describe('GoogleGCSPublish', () => {
},
},
});
});
afterEach(() => {
mockFs.restore();
});
it('should publish a directory', async () => {
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
expect(
await publisher.publish({
@@ -97,4 +102,43 @@ describe('GoogleGCSPublish', () => {
).toBeUndefined();
mockFs.restore();
});
it('should fail to publish a directory', async () => {
const wrongPathToGeneratedDirectory = path.join(
rootDir,
'wrong',
'path',
'to',
'generatedDirectory',
);
const entity = createMockEntity();
await expect(
publisher.publish({
entity,
directory: wrongPathToGeneratedDirectory,
}),
).rejects.toThrowError();
await publisher
.publish({
entity,
directory: wrongPathToGeneratedDirectory,
})
.catch(error => {
expect(error.message).toEqual(
// Can not do exact error message match due to mockFs adding unexpected characters in the path when throwing the error
// Issue reported https://github.com/tschaub/mock-fs/issues/118
expect.stringContaining(
`Unable to upload file(s) to Google Cloud Storage. Error: Failed to read template directory: ENOENT, no such file or directory`,
),
);
expect(error.message).toEqual(
expect.stringContaining(wrongPathToGeneratedDirectory),
);
});
mockFs.restore();
});
});
@@ -97,8 +97,8 @@ export class GoogleGCSPublish implements PublisherBase {
* Upload all the files from the generated `directory` to the GCS bucket.
* Directory structure used in the bucket is - entityNamespace/entityKind/entityName/index.html
*/
publish({ entity, directory }: PublishRequest): Promise<void> {
return new Promise(async (resolve, reject) => {
async publish({ entity, directory }: PublishRequest): Promise<void> {
try {
// Note: GCS manages creation of parent directories if they do not exist.
// So collecting path of only the files is good enough.
const allFilesToUpload = await getFileTreeRecursively(directory);
@@ -130,19 +130,16 @@ export class GoogleGCSPublish implements PublisherBase {
uploadPromises.push(uploadFile);
});
Promise.all(uploadPromises)
.then(() => {
this.logger.info(
`Successfully uploaded all the generated files for Entity ${entity.metadata.name}. Total number of files: ${allFilesToUpload.length}`,
);
resolve(undefined);
})
.catch((err: Error) => {
const errorMessage = `Unable to upload file(s) to Google Cloud Storage. Error ${err}`;
this.logger.error(errorMessage);
reject(errorMessage);
});
});
await Promise.all(uploadPromises);
this.logger.info(
`Successfully uploaded all the generated files for Entity ${entity.metadata.name}. Total number of files: ${allFilesToUpload.length}`,
);
} catch (e) {
const errorMessage = `Unable to upload file(s) to Google Cloud Storage. ${e}`;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
}
fetchTechDocsMetadata(entityName: EntityName): Promise<TechDocsMetadata> {