techdocs-node: refactor googleStorage tests to avoid mock-fs and storage mock
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -19,26 +19,21 @@ import { Entity, DEFAULT_NAMESPACE } from '@backstage/catalog-model';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import express from 'express';
|
||||
import request from 'supertest';
|
||||
import mockFs from 'mock-fs';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { Readable } from 'stream';
|
||||
import { GoogleGCSPublish } from './googleStorage';
|
||||
import {
|
||||
storageRootDir,
|
||||
StorageFilesMock,
|
||||
} from '../../testUtils/StorageFilesMock';
|
||||
import { createMockDirectory } from '@backstage/backend-test-utils';
|
||||
|
||||
const mockDir = createMockDirectory();
|
||||
|
||||
jest.mock('@google-cloud/storage', () => {
|
||||
class GCSFile {
|
||||
constructor(
|
||||
private readonly filePath: string,
|
||||
private readonly storage: StorageFilesMock,
|
||||
) {}
|
||||
constructor(private readonly filePath: string) {}
|
||||
|
||||
exists() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (this.storage.fileExists(this.filePath)) {
|
||||
if (fs.pathExistsSync(mockDir.resolve(this.filePath))) {
|
||||
resolve([true]);
|
||||
} else {
|
||||
reject();
|
||||
@@ -51,11 +46,14 @@ jest.mock('@google-cloud/storage', () => {
|
||||
readable._read = () => {};
|
||||
|
||||
process.nextTick(() => {
|
||||
if (this.storage.fileExists(this.filePath)) {
|
||||
if (fs.pathExistsSync(mockDir.resolve(this.filePath))) {
|
||||
if (readable.eventNames().includes('pipe')) {
|
||||
readable.emit('pipe');
|
||||
}
|
||||
readable.emit('data', this.storage.readFile(this.filePath));
|
||||
readable.emit(
|
||||
'data',
|
||||
fs.readFileSync(mockDir.resolve(this.filePath)),
|
||||
);
|
||||
readable.emit('end');
|
||||
} else {
|
||||
readable.emit(
|
||||
@@ -74,10 +72,7 @@ jest.mock('@google-cloud/storage', () => {
|
||||
}
|
||||
|
||||
class Bucket {
|
||||
constructor(
|
||||
private readonly bucketName: string,
|
||||
private readonly storage: StorageFilesMock,
|
||||
) {}
|
||||
constructor(private readonly bucketName: string) {}
|
||||
|
||||
async getMetadata() {
|
||||
if (this.bucketName === 'bad_bucket_name') {
|
||||
@@ -88,7 +83,9 @@ jest.mock('@google-cloud/storage', () => {
|
||||
|
||||
upload(source: string, { destination }: { destination: string }) {
|
||||
return new Promise(async resolve => {
|
||||
this.storage.writeFile(destination, source);
|
||||
mockDir.addContent({
|
||||
[destination]: fs.readFileSync(source, 'utf8'),
|
||||
});
|
||||
resolve(null);
|
||||
});
|
||||
}
|
||||
@@ -97,7 +94,7 @@ jest.mock('@google-cloud/storage', () => {
|
||||
if (this.bucketName === 'delete_stale_files_error') {
|
||||
throw Error('Message');
|
||||
}
|
||||
return new GCSFile(destinationFilePath, this.storage);
|
||||
return new GCSFile(destinationFilePath);
|
||||
}
|
||||
|
||||
getFilesStream() {
|
||||
@@ -119,14 +116,8 @@ jest.mock('@google-cloud/storage', () => {
|
||||
}
|
||||
|
||||
class Storage {
|
||||
storage = new StorageFilesMock();
|
||||
|
||||
constructor() {
|
||||
this.storage.emptyFiles();
|
||||
}
|
||||
|
||||
bucket(bucketName: string) {
|
||||
return new Bucket(bucketName, this.storage);
|
||||
return new Bucket(bucketName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +133,7 @@ const getEntityRootDir = (entity: Entity) => {
|
||||
metadata: { namespace, name },
|
||||
} = entity;
|
||||
|
||||
return path.join(storageRootDir, namespace || DEFAULT_NAMESPACE, kind, name);
|
||||
return mockDir.resolve(namespace || DEFAULT_NAMESPACE, kind, name);
|
||||
};
|
||||
|
||||
const logger = getVoidLogger();
|
||||
@@ -220,15 +211,11 @@ describe('GoogleGCSPublish', () => {
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[directory]: files,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
|
||||
describe('getReadiness', () => {
|
||||
it('should validate correct config', async () => {
|
||||
const publisher = createPublisherFromConfig();
|
||||
@@ -300,8 +287,7 @@ describe('GoogleGCSPublish', () => {
|
||||
});
|
||||
|
||||
it('should fail to publish a directory', async () => {
|
||||
const wrongPathToGeneratedDirectory = path.join(
|
||||
storageRootDir,
|
||||
const wrongPathToGeneratedDirectory = mockDir.resolve(
|
||||
'wrong',
|
||||
'path',
|
||||
'to',
|
||||
@@ -315,13 +301,9 @@ describe('GoogleGCSPublish', () => {
|
||||
directory: wrongPathToGeneratedDirectory,
|
||||
});
|
||||
|
||||
// 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
|
||||
await expect(fails).rejects.toMatchObject({
|
||||
message: expect.stringContaining(
|
||||
`Unable to upload file(s) to Google Cloud Storage. Error: Failed to read template directory: ENOENT, no such file or directory`,
|
||||
),
|
||||
});
|
||||
await expect(fails).rejects.toThrow(
|
||||
`Unable to upload file(s) to Google Cloud Storage. Error: Failed to read template directory: ENOENT: no such file or directory, scandir '${wrongPathToGeneratedDirectory}'`,
|
||||
);
|
||||
|
||||
await expect(fails).rejects.toMatchObject({
|
||||
message: expect.stringContaining(wrongPathToGeneratedDirectory),
|
||||
|
||||
Reference in New Issue
Block a user