Skip to content

Commit

Permalink
fix: convert folder components to source format correctly (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Powell authored and sfsholden committed Feb 24, 2021
1 parent cc2ae34 commit 821600e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/convert/transformers/defaultMetadataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,19 @@ export class DefaultMetadataTransformer extends BaseMetadataTransformer {

// quirks:
// - append or strip the -meta.xml suffix to the path if there's no content
// - remove file extension but preserve -meta.xml suffix if folder type
// for folder components:
// - remove file extension but preserve -meta.xml suffix if folder type and to 'metadata format'
// - insert file extension behind the -meta.xml suffix if folder type and to 'source format'
if (!component.content) {
const { folderContentType, suffix } = component.type;
if (targetFormat === 'metadata') {
const { folderContentType, suffix } = component.type;
xmlDestination = folderContentType
? xmlDestination.replace(`.${suffix}`, '')
: xmlDestination.slice(0, xmlDestination.lastIndexOf(META_XML_SUFFIX));
} else {
xmlDestination = `${xmlDestination}${META_XML_SUFFIX}`;
xmlDestination = folderContentType
? xmlDestination.replace(META_XML_SUFFIX, `.${suffix}${META_XML_SUFFIX}`)
: `${xmlDestination}${META_XML_SUFFIX}`;
}
}

Expand Down
24 changes: 22 additions & 2 deletions test/convert/transformers/defaultMetadataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { expect } from 'chai';
import { DEFAULT_PACKAGE_ROOT_SFDX, META_XML_SUFFIX } from '../../../src/common';
import { SourceComponent, VirtualTreeContainer } from '../../../src';
import { GENE_COMPONENT, GENE_XML_NAME } from '../../mock/registry/geneConstants';
import { TINA_FOLDER_COMPONENT } from '../../mock/registry/tinaConstants';
import {
TINA_FOLDER_COMPONENT,
TINA_FOLDER_COMPONENT_MD_FORMAT,
} from '../../mock/registry/tinaConstants';

const env = createSandbox();

Expand Down Expand Up @@ -132,7 +135,7 @@ describe('DefaultMetadataTransformer', () => {
expect(await transformer.toSourceFormat(component)).to.deep.equal(expectedInfos);
});

it('should handle folder type components with no content', async () => {
it('should handle components in folders with no content', async () => {
const component = SourceComponent.createVirtualComponent(
kathy.KATHY_MD_FORMAT_COMPONENTS[0],
[]
Expand All @@ -154,6 +157,23 @@ describe('DefaultMetadataTransformer', () => {
expect(await transformer.toSourceFormat(component)).to.deep.equal(expectedInfos);
});

it('should handle folder components', async () => {
const component = TINA_FOLDER_COMPONENT_MD_FORMAT;
const { directoryName } = mockRegistry.getTypeByName(component.type.folderContentType);
const expectedInfos: WriteInfo[] = [
{
output: join(
DEFAULT_PACKAGE_ROOT_SFDX,
directoryName,
`${component.fullName}.${component.type.suffix}${META_XML_SUFFIX}`
),
source: component.tree.stream(component.xml),
},
];

expect(await transformer.toSourceFormat(component)).to.deep.equal(expectedInfos);
});

it('should merge output with merge component when content is a directory', async () => {
const root = join('path', 'to', 'another', 'simons', 'a');
const component = SourceComponent.createVirtualComponent(
Expand Down
14 changes: 14 additions & 0 deletions test/mock/registry/tinaConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const type = mockRegistryData.types.tinafey;
export const TINA_DIR = join('path', 'to', 'tinas');
export const TINA_FOLDER = join(TINA_DIR, 'A_Folder');
export const TINA_FOLDER_XML = join(TINA_DIR, 'A_Folder.tinafeyFolder-meta.xml');
export const TINA_FOLDER_XML_MD_FORMAT = join(TINA_DIR, 'A_Folder-meta.xml');
export const TINA_XML_NAMES = ['a.tina-meta.xml', 'b.tina-meta.xml'];
export const TINA_XML_PATHS = TINA_XML_NAMES.map((n) => join(TINA_FOLDER, n));
export const TINA_SOURCE_NAMES = ['a.x', 'b.y'];
Expand All @@ -31,6 +32,19 @@ export const TINA_FOLDER_COMPONENT = SourceComponent.createVirtualComponent(
},
]
);
export const TINA_FOLDER_COMPONENT_MD_FORMAT = SourceComponent.createVirtualComponent(
{
name: 'A_Folder',
type: mockRegistryData.types.tinafeyfolder,
xml: TINA_FOLDER_XML_MD_FORMAT,
},
[
{
dirPath: TINA_FOLDER,
children: [basename(TINA_FOLDER_XML)],
},
]
);
export const TINA_COMPONENTS = [
new SourceComponent({
name: 'A_Folder/a',
Expand Down

0 comments on commit 821600e

Please sign in to comment.