Skip to content

Commit

Permalink
test: fix UTs, add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Aug 7, 2024
1 parent 948d95e commit 274986f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 15 deletions.
20 changes: 16 additions & 4 deletions src/resolve/sourceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,14 @@ export class SourceComponent implements MetadataComponent {
public async parseXml<T extends JsonMap>(xmlFilePath?: string): Promise<T> {
const xml = xmlFilePath ?? this.xml;
if (xml) {
const contents = this.pathContentMap.get(xml) ?? (await this.tree.readFile(xml)).toString();
this.pathContentMap.set(xml, contents);
let contents: string;
if (this.pathContentMap.has(xml)) {
contents = this.pathContentMap.get(xml) as string;
} else {
contents = (await this.tree.readFile(xml)).toString();
this.pathContentMap.set(xml, contents);
}

const replacements = this.replacements?.[xml] ?? this.parent?.replacements?.[xml];
return this.parseAndValidateXML<T>(
replacements ? await replacementIterations(contents, replacements) : contents,
Expand All @@ -202,8 +208,14 @@ export class SourceComponent implements MetadataComponent {
public parseXmlSync<T extends JsonMap>(xmlFilePath?: string): T {
const xml = xmlFilePath ?? this.xml;
if (xml) {
const contents = this.pathContentMap.get(xml) ?? this.tree.readFileSync(xml).toString();
this.pathContentMap.set(xml, contents);
let contents: string;
if (this.pathContentMap.has(xml)) {
contents = this.pathContentMap.get(xml) as string;
} else {
contents = this.tree.readFileSync(xml).toString();
this.pathContentMap.set(xml, contents);
}

return this.parseAndValidateXML(contents, xml);
}
return {} as T;
Expand Down
17 changes: 12 additions & 5 deletions test/convert/convertContext/nonDecomposition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../../mock/type-constants/customlabelsConstant';
import { SourceComponent } from '../../../src';

describe.skip('NonDecomposition', () => {
describe('NonDecomposition', () => {
const env = createSandbox();
afterEach(() => env.restore());

Expand All @@ -41,7 +41,7 @@ describe.skip('NonDecomposition', () => {
} as unknown as SfProject);
});
it('should return WriterFormats for claimed children', async () => {
const component = new SourceComponent(nonDecomposed.COMPONENT_1, TREE);
const component = nonDecomposed.COMPONENT_1;
const context = new ConvertContext();
const writeInfos = [
{
Expand All @@ -58,6 +58,8 @@ describe.skip('NonDecomposition', () => {
};

const result = await context.nonDecomposition.finalize(nonDecomposed.DEFAULT_DIR, TREE);
// @ts-ignore - because the resulting component will have cache info, and the initial one won't
result[0].component.pathContentMap = new Map();
expect(result).to.deep.equal([{ component, writeInfos }]);
});

Expand Down Expand Up @@ -95,7 +97,7 @@ describe.skip('NonDecomposition', () => {
});

it('should merge unclaimed children to default parent component', async () => {
const component = new SourceComponent(nonDecomposed.COMPONENT_1);
const component = nonDecomposed.COMPONENT_1;
const type = component.type;
const context = new ConvertContext();

Expand All @@ -116,7 +118,8 @@ describe.skip('NonDecomposition', () => {
};

const result = await context.nonDecomposition.finalize(nonDecomposed.DEFAULT_DIR, TREE);

// @ts-ignore - because the resulting component will have cache info, and the initial one won't
result[0].component.pathContentMap = new Map();
expect(result).to.deep.equal([{ component, writeInfos }]);
});

Expand Down Expand Up @@ -145,6 +148,8 @@ describe.skip('NonDecomposition', () => {
};

const result = await context.nonDecomposition.finalize(nonDecomposed.DEFAULT_DIR, TREE);
// @ts-ignore - because the resulting component will have cache info, and the initial one won't
result[0].component.pathContentMap = new Map();
expect(result).to.deep.equal([{ component, writeInfos }]);
});

Expand All @@ -164,7 +169,7 @@ describe.skip('NonDecomposition', () => {
},
],
} as unknown as SfProject);
const component = new SourceComponent(nonDecomposed.COMPONENT_2);
const component = nonDecomposed.COMPONENT_2;
const context = new ConvertContext();
const type = component.type;

Expand All @@ -188,6 +193,8 @@ describe.skip('NonDecomposition', () => {
};

const result = await context.nonDecomposition.finalize(nonDecomposed.DEFAULT_DIR, TREE);
// @ts-ignore - because the resulting component will have cache info, and the initial one won't
result[0].component.pathContentMap = new Map();
expect(result).to.deep.equal([{ component, writeInfos }]);
});
});
55 changes: 52 additions & 3 deletions test/convert/convertContext/recomposition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ describe('Recomposition', () => {
});

it('should only read parent xml file once for non-decomposed components with children', async () => {
const component = nonDecomposed.COMPONENT_1;
const readFileSpy = env.spy(component.tree, 'readFile');

const context = new ConvertContext();
context.recomposition.transactionState.set(component.fullName, {
component,
children: new ComponentSet(component.getChildren()),
});

const result = await context.recomposition.finalize();
expect(result).to.deep.equal([
{
component,
writeInfos: [
{
source: new JsToXml({
CustomLabels: {
[XML_NS_KEY]: XML_NS_URL,
labels: [CHILD_1_XML, CHILD_2_XML],
},
}),
output: join('labels', 'CustomLabels.labels'),
},
],
},
]);

expect(readFileSpy.callCount, JSON.stringify(readFileSpy.getCalls(), undefined, 2)).to.equal(0);
});

it('should not read parent once already read', async () => {
const component = nonDecomposed.COMPONENT_1;
const context = new ConvertContext();
context.recomposition.transactionState.set(component.fullName, {
Expand Down Expand Up @@ -201,26 +232,44 @@ describe('Recomposition', () => {
],
},
];
const virtualTree = new VirtualTreeContainer(vDir);
const component = new SourceComponent(
{ name: customLabelsType.name, type: customLabelsType, xml: parentXmlPath1 },
new VirtualTreeContainer(vDir)
virtualTree
);
const component2 = new SourceComponent(
{ name: 'CustomLabels2', type: customLabelsType, xml: parentXmlPath2 },
new VirtualTreeContainer(vDir)
virtualTree
);

it('one main component with multiple parents in transaction state covering all the children', async () => {
const context = new ConvertContext();
const compSet = new ComponentSet();
const readFileSpy = env.spy(virtualTree, 'readFileSync');

component.getChildren().forEach((child) => compSet.add(child));
component2.getChildren().forEach((child) => compSet.add(child));
context.recomposition.transactionState.set(component.fullName, {
component,
children: compSet,
});

const readFileSpy = env.spy(VirtualTreeContainer.prototype, 'readFile');
await context.recomposition.finalize();

expect(readFileSpy.callCount, 'readFile() should only be called twice').to.equal(2);
});

it('once the parent/child file content is cached, it wont read them again', async () => {
const context = new ConvertContext();
const compSet = new ComponentSet();

component.getChildren().forEach((child) => compSet.add(child));
component2.getChildren().forEach((child) => compSet.add(child));
context.recomposition.transactionState.set(component.fullName, {
component,
children: compSet,
});
const readFileSpy = env.spy(virtualTree, 'readFileSync');

await context.recomposition.finalize();

Expand Down
4 changes: 3 additions & 1 deletion test/resolve/adapters/decomposedSourceAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { registry, RegistryAccess, SourceComponent, VirtualTreeContainer } from
import { RegistryTestUtil } from '../registryTestUtil';
import { META_XML_SUFFIX } from '../../../src/common';

describe.skip('DecomposedSourceAdapter', () => {
describe('DecomposedSourceAdapter', () => {
const registryAccess = new RegistryAccess();
const type = registry.types.customobject;
const tree = new VirtualTreeContainer(decomposed.DECOMPOSED_VIRTUAL_FS);
Expand Down Expand Up @@ -80,6 +80,8 @@ describe.skip('DecomposedSourceAdapter', () => {
assert(decomposed.DECOMPOSED_CHILD_COMPONENT_1.xml);
const result = adapter.getComponent(decomposed.DECOMPOSED_CHILD_COMPONENT_1.xml);

// @ts-ignore - this only failed when running 'yarn test' - parent has cache info the result won't
component.parent.pathContentMap = new Map();
expect(result).to.deep.equal(component);
});

Expand Down
6 changes: 4 additions & 2 deletions test/resolve/sourceComponent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { join } from 'node:path';
import { assert, expect } from 'chai';
import { assert, config, expect } from 'chai';
import { createSandbox } from 'sinon';
import { Messages, SfError } from '@salesforce/core';
import { decomposed, matchingContentFile, mixedContentDirectory, xmlInFolder } from '../mock';
Expand Down Expand Up @@ -41,12 +41,14 @@ import { DE_METAFILE } from '../mock/type-constants/digitalExperienceBundleConst
import { XML_NS_KEY, XML_NS_URL } from '../../src/common';
import { RegistryTestUtil } from './registryTestUtil';

config.truncateThreshold = 0;

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr');

const env = createSandbox();

describe.skip('SourceComponent', () => {
describe('SourceComponent', () => {
it('should return correct fullName for components without a parent', () => {
expect(DECOMPOSED_COMPONENT.fullName).to.equal(DECOMPOSED_COMPONENT.name);
});
Expand Down

2 comments on commit 274986f

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 274986f Previous: f5f844c Ratio
eda-componentSetCreate-linux 183 ms 237 ms 0.77
eda-sourceToMdapi-linux 2205 ms 2369 ms 0.93
eda-sourceToZip-linux 1864 ms 1854 ms 1.01
eda-mdapiToSource-linux 2920 ms 2962 ms 0.99
lotsOfClasses-componentSetCreate-linux 364 ms 422 ms 0.86
lotsOfClasses-sourceToMdapi-linux 3654 ms 3737 ms 0.98
lotsOfClasses-sourceToZip-linux 3036 ms 3159 ms 0.96
lotsOfClasses-mdapiToSource-linux 3471 ms 3591 ms 0.97
lotsOfClassesOneDir-componentSetCreate-linux 643 ms 760 ms 0.85
lotsOfClassesOneDir-sourceToMdapi-linux 6352 ms 6513 ms 0.98
lotsOfClassesOneDir-sourceToZip-linux 5589 ms 5646 ms 0.99
lotsOfClassesOneDir-mdapiToSource-linux 6303 ms 6492 ms 0.97

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 274986f Previous: f5f844c Ratio
eda-componentSetCreate-win32 398 ms 736 ms 0.54
eda-sourceToMdapi-win32 4145 ms 4814 ms 0.86
eda-sourceToZip-win32 2844 ms 3285 ms 0.87
eda-mdapiToSource-win32 6001 ms 6640 ms 0.90
lotsOfClasses-componentSetCreate-win32 903 ms 1309 ms 0.69
lotsOfClasses-sourceToMdapi-win32 7900 ms 8704 ms 0.91
lotsOfClasses-sourceToZip-win32 4907 ms 5684 ms 0.86
lotsOfClasses-mdapiToSource-win32 7885 ms 8831 ms 0.89
lotsOfClassesOneDir-componentSetCreate-win32 1466 ms 2388 ms 0.61
lotsOfClassesOneDir-sourceToMdapi-win32 13001 ms 14641 ms 0.89
lotsOfClassesOneDir-sourceToZip-win32 8367 ms 9192 ms 0.91
lotsOfClassesOneDir-mdapiToSource-win32 13158 ms 14382 ms 0.91

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.