From 0f40f1934f81cb2f9a123af644e6f8d1769bddb7 Mon Sep 17 00:00:00 2001 From: Remi Date: Tue, 5 Jan 2021 18:35:23 +0100 Subject: [PATCH 1/2] fix(techdocs): path delimiters --- .../src/stages/publish/awsS3.test.ts | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/techdocs-common/src/stages/publish/awsS3.test.ts b/packages/techdocs-common/src/stages/publish/awsS3.test.ts index 501c171c2e..907da5148c 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.test.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ import mockFs from 'mock-fs'; +import path from 'path'; import * as winston from 'winston'; import { ConfigReader } from '@backstage/config'; import { AwsS3Publish } from './awsS3'; @@ -67,8 +68,9 @@ describe('AwsS3Publish', () => { kind, metadata: { namespace, name }, } = entity; + const directoryPath = path.join(namespace, kind, name); mockFs({ - [`${namespace}/${kind}/${name}`]: { + [directoryPath]: { 'index.html': '', '404.html': '', assets: { @@ -80,23 +82,29 @@ describe('AwsS3Publish', () => { expect( await publisher.publish({ entity, - directory: `${namespace}/${kind}/${name}`, + directory: directoryPath, }), ).toBeUndefined(); mockFs.restore(); }); it('should fail to publish a directory', async () => { - const wrongPathToGeneratedDirectory = '/wrong/path/to/generatedDirectory'; + const wrongPathToGeneratedDirectory = path.join( + 'wrong', + 'path', + 'to', + 'generatedDirectory', + ); const entity = createMockEntity(); const { kind, metadata: { namespace, name }, } = entity; + const directoryPath = path.join(namespace, kind, name); mockFs({ - [`${namespace}/${kind}/${name}`]: { + [directoryPath]: { 'index.html': '', '404.html': '', assets: { @@ -127,11 +135,15 @@ describe('AwsS3Publish', () => { apiVersion: 'apiVersion', kind: 'kind', metadata: { - namespace: '/namespace', + namespace: 'namespace', name: 'name', }, }; - const entityRootDir = `${entityMock.metadata.namespace}/${entityMock.kind}/${entityMock.metadata.name}`; + const entityRootDir = path.join( + entityMock.metadata.namespace, + entityMock.kind, + entityMock.metadata.name, + ); mockFs({ [entityRootDir]: { 'index.html': 'file-content', @@ -160,10 +172,14 @@ describe('AwsS3Publish', () => { it('should return tech docs metadata', async () => { const entityNameMock = { name: 'name', - namespace: '/namespace', + namespace: 'namespace', kind: 'kind', }; - const entityRootDir = `${entityNameMock.namespace}/${entityNameMock.kind}/${entityNameMock.name}`; + const entityRootDir = path.join( + entityNameMock.namespace, + entityNameMock.kind, + entityNameMock.name, + ); mockFs({ [entityRootDir]: { 'techdocs_metadata.json': 'file-content', @@ -182,7 +198,11 @@ describe('AwsS3Publish', () => { namespace: 'namespace', kind: 'kind', }; - const entityRootDir = `${entityNameMock.namespace}/${entityNameMock.kind}/${entityNameMock.name}`; + const entityRootDir = path.join( + entityNameMock.namespace, + entityNameMock.kind, + entityNameMock.name, + ); await publisher .fetchTechDocsMetadata(entityNameMock) .catch(error => From a87fc11aca708766b91c613758657de926969531 Mon Sep 17 00:00:00 2001 From: Remi Date: Tue, 5 Jan 2021 21:31:18 +0100 Subject: [PATCH 2/2] tests(techdocs): refactor --- .../src/stages/publish/awsS3.test.ts | 95 ++++++++----------- 1 file changed, 37 insertions(+), 58 deletions(-) diff --git a/packages/techdocs-common/src/stages/publish/awsS3.test.ts b/packages/techdocs-common/src/stages/publish/awsS3.test.ts index 907da5148c..a1d52be85b 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.test.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.test.ts @@ -19,8 +19,9 @@ import * as winston from 'winston'; import { ConfigReader } from '@backstage/config'; import { AwsS3Publish } from './awsS3'; import { PublisherBase } from './types'; +import type { Entity, EntityName } from '@backstage/catalog-model'; -const createMockEntity = (annotations = {}) => { +const createMockEntity = (annotations = {}): Entity => { return { apiVersion: 'version', kind: 'TestKind', @@ -34,6 +35,21 @@ const createMockEntity = (annotations = {}) => { }; }; +const createMockEntityName = (): EntityName => ({ + kind: 'TestKind', + name: 'test-component-name', + namespace: 'test-namespace', +}); + +const getEntityRootDir = (entity: Entity) => { + const { + kind, + metadata: { namespace, name }, + } = entity; + const entityRootDir = path.join(namespace as string, kind, name); + return entityRootDir; +}; + const logger = winston.createLogger(); jest.spyOn(logger, 'info').mockReturnValue(logger); jest.spyOn(logger, 'error').mockReturnValue(logger); @@ -64,13 +80,10 @@ describe('AwsS3Publish', () => { describe('publish', () => { it('should publish a directory', async () => { const entity = createMockEntity(); - const { - kind, - metadata: { namespace, name }, - } = entity; - const directoryPath = path.join(namespace, kind, name); + const entityRootDir = getEntityRootDir(entity); + mockFs({ - [directoryPath]: { + [entityRootDir]: { 'index.html': '', '404.html': '', assets: { @@ -82,7 +95,7 @@ describe('AwsS3Publish', () => { expect( await publisher.publish({ entity, - directory: directoryPath, + directory: entityRootDir, }), ).toBeUndefined(); mockFs.restore(); @@ -96,15 +109,10 @@ describe('AwsS3Publish', () => { 'generatedDirectory', ); const entity = createMockEntity(); - - const { - kind, - metadata: { namespace, name }, - } = entity; - const directoryPath = path.join(namespace, kind, name); + const entityRootDir = getEntityRootDir(entity); mockFs({ - [directoryPath]: { + [entityRootDir]: { 'index.html': '', '404.html': '', assets: { @@ -131,55 +139,32 @@ describe('AwsS3Publish', () => { describe('hasDocsBeenGenerated', () => { it('should return true if docs has been generated', async () => { - const entityMock = { - apiVersion: 'apiVersion', - kind: 'kind', - metadata: { - namespace: 'namespace', - name: 'name', - }, - }; - const entityRootDir = path.join( - entityMock.metadata.namespace, - entityMock.kind, - entityMock.metadata.name, - ); + const entity = createMockEntity(); + const entityRootDir = getEntityRootDir(entity); + mockFs({ [entityRootDir]: { 'index.html': 'file-content', }, }); - expect(await publisher.hasDocsBeenGenerated(entityMock)).toBe(true); + expect(await publisher.hasDocsBeenGenerated(entity)).toBe(true); mockFs.restore(); }); it('should return false if docs has not been generated', async () => { - const entityMock = { - apiVersion: 'apiVersion', - kind: 'kind', - metadata: { - namespace: 'namespace', - name: 'name', - }, - }; + const entity = createMockEntity(); - expect(await publisher.hasDocsBeenGenerated(entityMock)).toBe(false); + expect(await publisher.hasDocsBeenGenerated(entity)).toBe(false); }); }); describe('fetchTechDocsMetadata', () => { it('should return tech docs metadata', async () => { - const entityNameMock = { - name: 'name', - namespace: 'namespace', - kind: 'kind', - }; - const entityRootDir = path.join( - entityNameMock.namespace, - entityNameMock.kind, - entityNameMock.name, - ); + const entityNameMock = createMockEntityName(); + const entity = createMockEntity(); + const entityRootDir = getEntityRootDir(entity); + mockFs({ [entityRootDir]: { 'techdocs_metadata.json': 'file-content', @@ -193,16 +178,10 @@ describe('AwsS3Publish', () => { }); it('should return an error if the techdocs_metadata.json file is not present', async () => { - const entityNameMock = { - name: 'name', - namespace: 'namespace', - kind: 'kind', - }; - const entityRootDir = path.join( - entityNameMock.namespace, - entityNameMock.kind, - entityNameMock.name, - ); + const entityNameMock = createMockEntityName(); + const entity = createMockEntity(); + const entityRootDir = getEntityRootDir(entity); + await publisher .fetchTechDocsMetadata(entityNameMock) .catch(error =>