TechDocs/Azure: Update mock library tests to work with Windows

This commit is contained in:
Himanshu Mishra
2021-02-18 22:16:48 +01:00
parent 837ce612de
commit b0fc3549e6
5 changed files with 47 additions and 10 deletions
@@ -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<boolean> => {
// 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);
}
}
@@ -32,8 +32,6 @@ const checkFileExists = async (sourceFile: string): Promise<boolean> => {
await fs.access(filePath, fs.constants.F_OK);
return true;
} catch (err) {
console.log("File doesn't exist");
console.log(filePath);
return false;
}
};
@@ -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',
@@ -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`,
),
);
@@ -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',