diff --git a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts index 6633a71b2c..7157f9da60 100644 --- a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts +++ b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts @@ -13,11 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import fs from 'fs'; import type { BlobUploadCommonResponse, ContainerGetPropertiesResponse, } from '@azure/storage-blob'; +import fs from 'fs-extra'; +import os from 'os'; +import path from 'path'; + +const rootDir = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; +/** + * @param sourceFile contains either / or \ as file separator depending upon OS. + */ +const checkFileExists = async (sourceFile: string): Promise => { + // sourceFile will always have / as file separator irrespective of OS since S3 expects /. + // Normalize sourceFile to OS specific path before checking if file exists. + const relativeFilePath = sourceFile.split(path.posix.sep).join(path.sep); + const filePath = path.join(rootDir, sourceFile); + + try { + await fs.access(filePath, fs.constants.F_OK); + return true; + } catch (err) { + return false; + } +}; export class BlockBlobClient { private readonly blobName; @@ -39,7 +59,7 @@ export class BlockBlobClient { } exists() { - return Promise.resolve(fs.existsSync(this.blobName)); + return checkFileExists(this.blobName); } } diff --git a/packages/techdocs-common/__mocks__/@google-cloud/storage.ts b/packages/techdocs-common/__mocks__/@google-cloud/storage.ts index 503ee397b4..9f19e8bf99 100644 --- a/packages/techdocs-common/__mocks__/@google-cloud/storage.ts +++ b/packages/techdocs-common/__mocks__/@google-cloud/storage.ts @@ -32,8 +32,6 @@ const checkFileExists = async (sourceFile: string): Promise => { await fs.access(filePath, fs.constants.F_OK); return true; } catch (err) { - console.log("File doesn't exist"); - console.log(filePath); return false; } }; diff --git a/packages/techdocs-common/src/stages/publish/awsS3.test.ts b/packages/techdocs-common/src/stages/publish/awsS3.test.ts index 4f586119e5..0c2a33dc03 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.test.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.test.ts @@ -22,6 +22,8 @@ import * as winston from 'winston'; import { AwsS3Publish } from './awsS3'; import { PublisherBase, TechDocsMetadata } from './types'; +// NOTE: /packages/techdocs-common/__mocks__ is being used to mock aws-sdk client library + const createMockEntity = (annotations = {}): Entity => { return { apiVersion: 'version', diff --git a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts index e43f9112f5..dfb32b9ce8 100644 --- a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts @@ -17,9 +17,13 @@ import { getVoidLogger } from '@backstage/backend-common'; import type { Entity } from '@backstage/catalog-model'; import { ConfigReader } from '@backstage/config'; import mockFs from 'mock-fs'; +import os from 'os'; +import path from 'path'; import { AzureBlobStoragePublish } from './azureBlobStorage'; import { PublisherBase } from './types'; +// NOTE: /packages/techdocs-common/__mocks__ is being used to mock Azure client library + const createMockEntity = (annotations = {}) => { return { apiVersion: 'version', @@ -34,13 +38,14 @@ const createMockEntity = (annotations = {}) => { }; }; +const rootDir = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; const getEntityRootDir = (entity: Entity) => { const { kind, metadata: { namespace, name }, } = entity; - const entityRootDir = `${namespace}/${kind}/${name}`; - return entityRootDir; + + return path.join(rootDir, namespace as string, kind, name); }; function createLogger() { @@ -106,7 +111,14 @@ describe('publishing with valid credentials', () => { }); it('should fail to publish a directory', async () => { - const wrongPathToGeneratedDirectory = 'wrong/path/to/generatedDirectory'; + const wrongPathToGeneratedDirectory = path.join( + rootDir, + 'wrong', + 'path', + 'to', + 'generatedDirectory', + ); + const entity = createMockEntity(); await publisher @@ -115,8 +127,8 @@ describe('publishing with valid credentials', () => { directory: wrongPathToGeneratedDirectory, }) .catch(error => - expect(error.message).toContain( - 'Unable to upload file(s) to Azure Blob Storage. Error: Failed to read template directory: ENOENT, no such file or directory', + expect(error.message).toBe( + `Unable to upload file(s) to Azure Blob Storage. Error: Failed to read template directory: ENOENT, no such file or directory '${wrongPathToGeneratedDirectory}'`, ), ); mockFs.restore(); @@ -227,7 +239,10 @@ describe('error reporting', () => { expect(logger.error).toHaveBeenCalledWith( expect.stringContaining( - `Unable to upload file(s) to Azure Blob Storage. Error: Upload failed for test-namespace/TestKind/test-component-name/index.html with status code 500`, + `Unable to upload file(s) to Azure Blob Storage. Error: Upload failed for ${path.join( + entityRootDir, + 'index.html', + )} with status code 500`, ), ); diff --git a/packages/techdocs-common/src/stages/publish/googleStorage.test.ts b/packages/techdocs-common/src/stages/publish/googleStorage.test.ts index 9d4904b223..1069b7b9a4 100644 --- a/packages/techdocs-common/src/stages/publish/googleStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/googleStorage.test.ts @@ -22,6 +22,8 @@ import path from 'path'; import { GoogleGCSPublish } from './googleStorage'; import { PublisherBase } from './types'; +// NOTE: /packages/techdocs-common/__mocks__ is being used to mock Google Cloud Storage client library + const createMockEntity = (annotations = {}): Entity => { return { apiVersion: 'version',