diff --git a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts index 3af2dc4a30..1d63e7266a 100644 --- a/packages/techdocs-common/__mocks__/@azure/storage-blob.ts +++ b/packages/techdocs-common/__mocks__/@azure/storage-blob.ts @@ -18,8 +18,9 @@ import { ContainerGetPropertiesResponse, } from '@azure/storage-blob'; import { EventEmitter } from 'events'; +import { IStorageFilesMock } from '../../src/testUtils/types'; -const storage = global.storageFilesMock; +const storage = global.storageFilesMock as IStorageFilesMock; export class BlockBlobClient { private readonly blobName; diff --git a/packages/techdocs-common/__mocks__/@google-cloud/storage.ts b/packages/techdocs-common/__mocks__/@google-cloud/storage.ts index a48e8c1273..e87ccb69c1 100644 --- a/packages/techdocs-common/__mocks__/@google-cloud/storage.ts +++ b/packages/techdocs-common/__mocks__/@google-cloud/storage.ts @@ -14,8 +14,9 @@ * limitations under the License. */ import { Readable } from 'stream'; +import { IStorageFilesMock } from '../../src/testUtils/types'; -const storage = global.storageFilesMock; +const storage = global.storageFilesMock as IStorageFilesMock; class GCSFile { private readonly path: string; diff --git a/packages/techdocs-common/__mocks__/aws-sdk.ts b/packages/techdocs-common/__mocks__/aws-sdk.ts index e4ae44c94e..ebb3c6840b 100644 --- a/packages/techdocs-common/__mocks__/aws-sdk.ts +++ b/packages/techdocs-common/__mocks__/aws-sdk.ts @@ -15,10 +15,11 @@ */ import { EventEmitter } from 'events'; import { ReadStream } from 'fs'; +import { IStorageFilesMock } from '../src/testUtils/types'; export { Credentials } from 'aws-sdk'; -const storage = global.storageFilesMock; +const storage = global.storageFilesMock as IStorageFilesMock; export class S3 { constructor() { diff --git a/packages/techdocs-common/__mocks__/globals.d.ts b/packages/techdocs-common/__mocks__/globals.d.ts deleted file mode 100644 index ad293d2ebe..0000000000 --- a/packages/techdocs-common/__mocks__/globals.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2020 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -declare module NodeJS { - interface Global { - rootDir: string; - storageFilesMock: IStorageFilesMock; - } -} diff --git a/packages/techdocs-common/src/globals.d.ts b/packages/techdocs-common/src/globals.d.ts deleted file mode 100644 index ad293d2ebe..0000000000 --- a/packages/techdocs-common/src/globals.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2020 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -declare module NodeJS { - interface Global { - rootDir: string; - storageFilesMock: IStorageFilesMock; - } -} diff --git a/packages/techdocs-common/src/setupTests.ts b/packages/techdocs-common/src/setupTests.ts index 9a13e57633..d342cfb26a 100644 --- a/packages/techdocs-common/src/setupTests.ts +++ b/packages/techdocs-common/src/setupTests.ts @@ -14,47 +14,7 @@ * limitations under the License. */ -import os from 'os'; -import path from 'path'; -import fs from 'fs-extra'; +import { StorageFilesMock } from './testUtils/StorageFilesMock'; -const rootDir: string = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; - -const encoding = 'utf8'; - -class StorageFilesMock implements IStorageFilesMock { - private files: Record; - - constructor() { - this.files = {}; - } - - public emptyFiles(): void { - this.files = {}; - } - - public fileExists(targetPath: string): boolean { - const filePath = path.join(rootDir, targetPath); - const posixPath = filePath.split(path.posix.sep).join(path.sep); - return this.files[posixPath] !== undefined; - } - - public readFile(targetPath: string): Buffer { - const filePath = path.join(rootDir, targetPath); - return Buffer.from(this.files[filePath] ?? '', encoding); - } - - public writeFile(targetPath: string, sourcePath: string): void; - public writeFile(targetPath: string, sourceBuffer: Buffer): void; - public writeFile(targetPath: string, source: string | Buffer): void { - const filePath = path.join(rootDir, targetPath); - if (typeof source === 'string') { - this.files[filePath] = fs.readFileSync(source).toString(encoding); - } else { - this.files[filePath] = source.toString(encoding); - } - } -} - -global.rootDir = rootDir; -global.storageFilesMock = new StorageFilesMock(); +(global as any).rootDir = StorageFilesMock.rootDir; +(global as any).storageFilesMock = new StorageFilesMock(); diff --git a/packages/techdocs-common/src/stages/publish/awsS3.test.ts b/packages/techdocs-common/src/stages/publish/awsS3.test.ts index 35bc80438b..3534c54cf0 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.test.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.test.ts @@ -26,7 +26,7 @@ import { AwsS3Publish } from './awsS3'; // NOTE: /packages/techdocs-common/__mocks__ is being used to mock aws-sdk client library -const rootDir = global.rootDir; +const rootDir = (global as any).rootDir; // Set by setupTests.ts const getEntityRootDir = (entity: Entity) => { const { diff --git a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts index cace6caae8..cb7405accd 100644 --- a/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/azureBlobStorage.test.ts @@ -26,7 +26,7 @@ import { AzureBlobStoragePublish } from './azureBlobStorage'; // NOTE: /packages/techdocs-common/__mocks__ is being used to mock Azure client library -const rootDir = global.rootDir; +const rootDir = (global as any).rootDir; // Set by setupTests.ts const getEntityRootDir = (entity: Entity) => { const { diff --git a/packages/techdocs-common/src/stages/publish/googleStorage.test.ts b/packages/techdocs-common/src/stages/publish/googleStorage.test.ts index 0190128e6c..a04f61ac11 100644 --- a/packages/techdocs-common/src/stages/publish/googleStorage.test.ts +++ b/packages/techdocs-common/src/stages/publish/googleStorage.test.ts @@ -26,7 +26,7 @@ import { GoogleGCSPublish } from './googleStorage'; // NOTE: /packages/techdocs-common/__mocks__ is being used to mock Google Cloud Storage client library -const rootDir = global.rootDir; +const rootDir = (global as any).rootDir; // Set by setupTests.ts const getEntityRootDir = (entity: Entity) => { const { diff --git a/packages/techdocs-common/src/testUtils/StorageFilesMock.ts b/packages/techdocs-common/src/testUtils/StorageFilesMock.ts new file mode 100644 index 0000000000..4a0e183bdb --- /dev/null +++ b/packages/techdocs-common/src/testUtils/StorageFilesMock.ts @@ -0,0 +1,60 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import os from 'os'; +import path from 'path'; +import fs from 'fs-extra'; +import { IStorageFilesMock } from './types'; + +const rootDir: string = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; + +const encoding = 'utf8'; + +export class StorageFilesMock implements IStorageFilesMock { + static rootDir = rootDir; + + private files: Record; + + constructor() { + this.files = {}; + } + + public emptyFiles(): void { + this.files = {}; + } + + public fileExists(targetPath: string): boolean { + const filePath = path.join(rootDir, targetPath); + const posixPath = filePath.split(path.posix.sep).join(path.sep); + return this.files[posixPath] !== undefined; + } + + public readFile(targetPath: string): Buffer { + const filePath = path.join(rootDir, targetPath); + return Buffer.from(this.files[filePath] ?? '', encoding); + } + + public writeFile(targetPath: string, sourcePath: string): void; + public writeFile(targetPath: string, sourceBuffer: Buffer): void; + public writeFile(targetPath: string, source: string | Buffer): void { + const filePath = path.join(rootDir, targetPath); + if (typeof source === 'string') { + this.files[filePath] = fs.readFileSync(source).toString(encoding); + } else { + this.files[filePath] = source.toString(encoding); + } + } +} diff --git a/packages/techdocs-common/__mocks__/types.d.ts b/packages/techdocs-common/src/testUtils/types.ts similarity index 96% rename from packages/techdocs-common/__mocks__/types.d.ts rename to packages/techdocs-common/src/testUtils/types.ts index e085b95851..b58e004ab8 100644 --- a/packages/techdocs-common/__mocks__/types.d.ts +++ b/packages/techdocs-common/src/testUtils/types.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -interface IStorageFilesMock { +export interface IStorageFilesMock { emptyFiles(): void; fileExists(targetPath: string): boolean; readFile(targetPath: string): Buffer; diff --git a/packages/techdocs-common/src/types.d.ts b/packages/techdocs-common/src/types.d.ts deleted file mode 100644 index e085b95851..0000000000 --- a/packages/techdocs-common/src/types.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2020 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -interface IStorageFilesMock { - emptyFiles(): void; - fileExists(targetPath: string): boolean; - readFile(targetPath: string): Buffer; - writeFile(targetPath: string, sourcePath: string): void; - writeFile(targetPath: string, sourceBuffer: Buffer): void; - writeFile(targetPath: string, source: string | Buffer): void; -}