Merge pull request #6962 from backstage/fix-azure-tests

[TechdocsCommon]: Fix Cloud providers Posix Paths
This commit is contained in:
Camila Belo
2021-08-26 14:30:03 +02:00
committed by GitHub
7 changed files with 54 additions and 22 deletions
@@ -278,7 +278,7 @@ describe('AwsS3Publish', () => {
name: 'path',
};
const techDocsMetadaFilePath = path.join(
const techDocsMetadaFilePath = path.posix.join(
...Object.values(invalidEntityName),
'techdocs_metadata.json',
);
@@ -297,7 +297,7 @@ describe('AzureBlobStoragePublish', () => {
name: 'path',
};
const techDocsMetadaFilePath = path.join(
const techDocsMetadaFilePath = path.posix.join(
...Object.values(invalidEntityName),
'techdocs_metadata.json',
);
@@ -276,7 +276,7 @@ describe('GoogleGCSPublish', () => {
name: 'path',
};
const techDocsMetadaFilePath = path.join(
const techDocsMetadaFilePath = path.posix.join(
...Object.values(invalidEntityName),
'techdocs_metadata.json',
);
@@ -23,6 +23,7 @@ import {
getCloudPathForLocalPath,
getHeadersForFileExtension,
bulkStorageOperation,
lowerCaseEntityTriplet,
lowerCaseEntityTripletInStoragePath,
} from './helpers';
@@ -82,13 +83,15 @@ describe('getFileTreeRecursively', () => {
});
});
describe('lowerCaseEntityTripletInStoragePath', () => {
describe('lowerCaseEntityTriplet', () => {
it('returns lower-cased entity triplet path', () => {
const originalPath = 'default/Component/backstage/index.html';
const actualPath = lowerCaseEntityTripletInStoragePath(originalPath);
const actualPath = lowerCaseEntityTriplet(originalPath);
expect(actualPath).toBe('default/component/backstage/index.html');
});
});
describe('lowerCaseEntityTripletInStoragePath', () => {
it('does not lowercase beyond the triplet', () => {
const originalPath = 'default/Component/backstage/assets/IMAGE.png';
const actualPath = lowerCaseEntityTripletInStoragePath(originalPath);
@@ -87,7 +87,8 @@ export const getFileTreeRecursively = async (
};
/**
* Returns a lower-cased version of entity's triplet in the form of a path.
* Takes a posix path and returns a lower-cased version of entity's triplet
* with the remaining path in posix.
*
* Path must not include a starting slash.
*
@@ -95,39 +96,48 @@ export const getFileTreeRecursively = async (
* lowerCaseEntityTriplet('default/Component/backstage')
* // return default/component/backstage
*/
export const lowerCaseEntityTriplet = (originalPath: string): string => {
const [namespace, kind, name, ...parts] = originalPath.split('/');
export const lowerCaseEntityTriplet = (posixPath: string): string => {
const [namespace, kind, name, ...rest] = posixPath.split(path.posix.sep);
const lowerNamespace = namespace.toLowerCase();
const lowerKind = kind.toLowerCase();
const lowerName = name.toLowerCase();
return [lowerNamespace, lowerKind, lowerName, ...parts].join('/');
return [lowerNamespace, lowerKind, lowerName, ...rest].join(path.posix.sep);
};
/**
* Returns the version of an object's storage path where the first three parts
* of the path (the entity triplet of namespace, kind, and name) are
* lower-cased.
* Takes either a win32 or posix path and returns a lower-cased version of entity's triplet
* with the remaining path in posix.
*
* Path must not include a starting slash.
* Starting slashes will be trimmed.
*
* Throws an error if the path does not appear to be an entity triplet.
*
* @example
* lowerCaseEntityTripletInStoragePath('default/Component/backstage/file.txt')
* lowerCaseEntityTripletInStoragePath('/default/Component/backstage/file.txt')
* // return default/component/backstage/file.txt
*/
export const lowerCaseEntityTripletInStoragePath = (
originalPath: string,
): string => {
const trimmedPath =
originalPath[0] === '/' ? originalPath.substring(1) : originalPath;
const matches = trimmedPath.match(/\//g) || [];
if (matches.length <= 2) {
let posixPath = originalPath;
if (originalPath.includes(path.win32.sep)) {
posixPath = originalPath.split(path.win32.sep).join(path.posix.sep);
}
// remove leading slash
const parts = posixPath.split(path.posix.sep);
if (parts[0] === '') {
parts.shift();
}
// check if all parts of the entity exist (name, namespace, kind) plus filename
if (parts.length <= 3) {
throw new Error(
`Encountered file unmanaged by TechDocs ${originalPath}. Skipping.`,
);
}
return lowerCaseEntityTriplet(originalPath);
return lowerCaseEntityTriplet(parts.join(path.posix.sep));
};
// Only returns the files that existed previously and are not present anymore.
@@ -161,7 +171,7 @@ export const getCloudPathForLocalPath = (
const relativeFilePathTriplet = `${entityRootDir}/${relativeFilePathPosix}`;
const destination = useLegacyPathCasing
? relativeFilePathTriplet
: lowerCaseEntityTripletInStoragePath(relativeFilePathTriplet);
: lowerCaseEntityTriplet(relativeFilePathTriplet);
return destination; // Remote storage file relative path
};
@@ -62,6 +62,20 @@ const getEntityRootDir = (entity: Entity) => {
return path.join(rootDir, namespace || ENTITY_DEFAULT_NAMESPACE, kind, name);
};
const getPosixEntityRootDir = (entity: Entity) => {
const {
kind,
metadata: { namespace, name },
} = entity;
return path.posix.join(
'/rootDir',
namespace || ENTITY_DEFAULT_NAMESPACE,
kind,
name,
);
};
const logger = getVoidLogger();
let publisher: PublisherBase;
@@ -267,12 +281,12 @@ describe('OpenStackSwiftPublish', () => {
it('should return an error if the techdocs_metadata.json file is not present', async () => {
const entityNameMock = createMockEntityName();
const entity = createMockEntity();
const entityRootDir = getEntityRootDir(entity);
const entityRootDir = getPosixEntityRootDir(entity);
const fails = publisher.fetchTechDocsMetadata(entityNameMock);
await expect(fails).rejects.toMatchObject({
message: `TechDocs metadata fetch failed, The file ${path.join(
message: `TechDocs metadata fetch failed, The file ${path.posix.join(
entityRootDir,
'techdocs_metadata.json',
)} does not exist !`,