From 7eef84a0404e43aebf440749fd25f8ec171e205e Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Tue, 3 Aug 2021 10:02:30 +0200 Subject: [PATCH] test(techdocs-common): cover getCloudPathForLocalPath helper Signed-off-by: Camila Belo --- .../src/stages/publish/helpers.test.ts | 44 +++++++++++++++++++ .../src/stages/publish/helpers.ts | 6 ++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/techdocs-common/src/stages/publish/helpers.test.ts b/packages/techdocs-common/src/stages/publish/helpers.test.ts index 63b4e0aeec..54761cf311 100644 --- a/packages/techdocs-common/src/stages/publish/helpers.test.ts +++ b/packages/techdocs-common/src/stages/publish/helpers.test.ts @@ -16,9 +16,11 @@ import mockFs from 'mock-fs'; import * as os from 'os'; import * as path from 'path'; +import { Entity, ENTITY_DEFAULT_NAMESPACE } from '@backstage/catalog-model'; import { getStaleFiles, getFileTreeRecursively, + getCloudPathForLocalPath, getHeadersForFileExtension, lowerCaseEntityTripletInStoragePath, } from './helpers'; @@ -124,3 +126,45 @@ describe('getStaleFiles', () => { expect(staleFiles).toEqual(expect.arrayContaining(['stale_file.png'])); }); }); + +describe('getCloudPathForLocalPath', () => { + const entity: Entity = { + apiVersion: 'version', + metadata: { namespace: 'default', name: 'backstage' }, + kind: 'Component', + }; + + it('should compose a remote bucket path including entity information', () => { + const remoteBucket = getCloudPathForLocalPath(entity); + expect(remoteBucket).toBe( + `${entity.metadata.namespace}/${entity.kind}/${entity.metadata.name}/`, + ); + }); + + it('should compose a remote filename including entity information', () => { + const localPath = 'index.html'; + const remoteBucket = getCloudPathForLocalPath(entity, localPath); + expect(remoteBucket).toBe( + `${entity.metadata.namespace}/${entity.kind}/${entity.metadata.name}/${localPath}`, + ); + }); + + it('should use the default namespace when it is undefined', () => { + const localPath = 'index.html'; + const { + kind, + metadata: { name }, + } = entity; + const remoteBucket = getCloudPathForLocalPath( + { kind, metadata: { name } } as Entity, + localPath, + ); + expect(remoteBucket).toBe( + `${ENTITY_DEFAULT_NAMESPACE}/${entity.kind}/${entity.metadata.name}/${localPath}`, + ); + }); + + it('should throw error when entity is invalid', () => { + expect(() => getCloudPathForLocalPath({} as Entity)).toThrow(); + }); +}); diff --git a/packages/techdocs-common/src/stages/publish/helpers.ts b/packages/techdocs-common/src/stages/publish/helpers.ts index 6f6eb7dfa5..fda03e7415 100644 --- a/packages/techdocs-common/src/stages/publish/helpers.ts +++ b/packages/techdocs-common/src/stages/publish/helpers.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Entity } from '@backstage/catalog-model'; +import { Entity, ENTITY_DEFAULT_NAMESPACE } from '@backstage/catalog-model'; import mime from 'mime-types'; import path from 'path'; import createLimiter from 'p-limit'; @@ -138,7 +138,9 @@ export const getCloudPathForLocalPath = ( const relativeFilePathPosix = localPath.split(path.sep).join(path.posix.sep); // The / delimiter is intentional since it represents the cloud storage and not the local file system. - const entityRootDir = `${entity.metadata.namespace}/${entity.kind}/${entity.metadata.name}`; + const entityRootDir = `${ + entity.metadata?.namespace ?? ENTITY_DEFAULT_NAMESPACE + }/${entity.kind}/${entity.metadata.name}`; return `${entityRootDir}/${relativeFilePathPosix}`; // GCS Bucket file relative path };