Skip to content

Commit

Permalink
fix: prevent empty parent xml overwrites (#1308)
Browse files Browse the repository at this point in the history
* test: snapshot for single field retrieve

* test: get folder, tmpDir set up

* test: getting paths fixed up

* test: testDir cleanup

* tests: snapshotTests (broken)

* fix: don't blank parent objects that have properties

* test: bump deps for xNuts

* test: ignore scripts in install
  • Loading branch information
mshanemc authored May 10, 2024
1 parent d3ee7b2 commit b8a8c7f
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/perfScaleNut.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
command: npm install @salesforce/cli@nightly -g
timeout_minutes: 60
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
with:
ignore-scripts: true
- run: |
yarn mocha test/nuts/scale/eda.nut.ts --timeout 500000
yarn mocha test/nuts/scale/lotsOfClasses.nut.ts --timeout 500000
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@salesforce/core": "^7.3.1",
"@salesforce/core": "^7.3.5",
"@salesforce/kit": "^3.1.1",
"@salesforce/ts-types": "^2.0.9",
"fast-levenshtein": "^3.0.0",
Expand All @@ -40,7 +40,7 @@
},
"devDependencies": {
"@jsforce/jsforce-node": "^3.2.0",
"@salesforce/cli-plugins-testkit": "^5.3.1",
"@salesforce/cli-plugins-testkit": "^5.3.2",
"@salesforce/dev-scripts": "^9.0.0",
"@types/deep-equal-in-any-order": "^1.0.1",
"@types/fast-levenshtein": "^0.0.4",
Expand Down
12 changes: 9 additions & 3 deletions src/convert/transformers/decomposedMetadataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,20 @@ const getWriteInfosFromMerge =
(parentXmlObject: XmlObj) =>
(parentComponent: SourceComponent): WriteInfo[] => {
const writeInfo = { source: new JsToXml(parentXmlObject), output: getOutputFile(parentComponent, mergeWith) };
const parentHasRealValues = objectHasSomeRealValues(parentComponent.type)(parentXmlObject);

if (mergeWith?.xml) {
// mark the component as found
stateSetter(parentComponent, { foundMerge: true });
} else if (objectHasSomeRealValues(parentComponent.type)(parentXmlObject)) {
return objectHasSomeRealValues(parentComponent.type)(mergeWith.parseXmlSync()) && !parentHasRealValues
? [] // the target file has values but this process doesn't, so we don't want to overwrite it
: [writeInfo];
}
if (objectHasSomeRealValues(parentComponent.type)(parentXmlObject)) {
// set the state but don't return any writeInfo to avoid writing "empty" (ns-only) parent files
stateSetter(parentComponent, { writeInfo });
}
return mergeWith.xml ? [writeInfo] : [];
return [];
};

/**
Expand Down Expand Up @@ -248,7 +253,7 @@ const tagToChildTypeId = ({ tagKey, type }: { tagKey: string; type: MetadataType
const objectHasSomeRealValues =
(type: MetadataType) =>
(obj: XmlObj): boolean =>
Object.keys(obj[type.name]).length > 1;
Object.keys(obj[type.name] ?? {}).length > 1;

const hasChildTypeId = (cm: ComposedMetadata): cm is Required<ComposedMetadata> => !!cm.childTypeId;

Expand All @@ -266,6 +271,7 @@ type ComposedMetadataWithChildType = ComposedMetadata & { childType: MetadataTyp
type InfoContainer = {
entryName?: string;
childComponent: MetadataComponent;
/** the parsed xml */
value: JsonMap;
parentComponent: SourceComponent;
mergeWith?: SourceComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<actionOverrides>
<actionName>Accept</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>CancelEdit</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Clone</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Delete</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Edit</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>List</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>New</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>SaveEdit</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Tab</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>View</actionName>
<type>Default</type>
</actionOverrides>
<allowInChatterGroups>false</allowInChatterGroups>
<compactLayoutAssignment>Broker_Compact</compactLayoutAssignment>
<deploymentStatus>Deployed</deploymentStatus>
<enableActivities>false</enableActivities>
<enableBulkApi>true</enableBulkApi>
<enableFeeds>false</enableFeeds>
<enableHistory>false</enableHistory>
<enableReports>true</enableReports>
<enableSearch>true</enableSearch>
<enableSharing>true</enableSharing>
<enableStreamingApi>true</enableStreamingApi>
<label>Broker</label>
<nameField>
<label>Broker Name</label>
<type>Text</type>
</nameField>
<pluralLabel>Brokers</pluralLabel>
<searchLayouts></searchLayouts>
<sharingModel>ReadWrite</sharingModel>
<visibility>Public</visibility>
</CustomObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Broker_Id__c</fullName>
<externalId>true</externalId>
<label>Broker Id</label>
<precision>18</precision>
<required>false</required>
<scale>0</scale>
<trackTrending>false</trackTrending>
<type>Number</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Email__c</fullName>
<externalId>false</externalId>
<label>Email</label>
<required>false</required>
<trackTrending>false</trackTrending>
<type>Email</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<actionOverrides>
<actionName>Accept</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>CancelEdit</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Clone</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Delete</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Edit</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>List</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>New</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>SaveEdit</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>Tab</actionName>
<type>Default</type>
</actionOverrides>
<actionOverrides>
<actionName>View</actionName>
<type>Default</type>
</actionOverrides>
<allowInChatterGroups>false</allowInChatterGroups>
<compactLayoutAssignment>Broker_Compact</compactLayoutAssignment>
<deploymentStatus>Deployed</deploymentStatus>
<enableActivities>false</enableActivities>
<enableBulkApi>true</enableBulkApi>
<enableFeeds>false</enableFeeds>
<enableHistory>false</enableHistory>
<enableReports>true</enableReports>
<enableSearch>true</enableSearch>
<enableSharing>true</enableSharing>
<enableStreamingApi>true</enableStreamingApi>
<label>Broker</label>
<nameField>
<label>Broker Name</label>
<type>Text</type>
</nameField>
<pluralLabel>Brokers</pluralLabel>
<searchLayouts></searchLayouts>
<sharingModel>ReadWrite</sharingModel>
<visibility>Public</visibility>
</CustomObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Broker_Id__c</fullName>
<externalId>true</externalId>
<label>Broker Id</label>
<precision>18</precision>
<required>false</required>
<scale>0</scale>
<trackTrending>false</trackTrending>
<type>Number</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<fields>
<fullName>Email__c</fullName>
<externalId>false</externalId>
<label>Email</label>
<required>false</required>
<trackTrending>false</trackTrending>
<type>Email</type>
<unique>false</unique>
</fields>
</CustomObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Broker__c.Email__c</members>
<name>CustomField</name>
</types>
<version>59.0</version>
</Package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"packageDirectories": [
{
"default": true,
"path": "force-app"
}
],
"sourceApiVersion": "59.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { MetadataConverter } from '../../../../src/convert/metadataConverter';
import { ComponentSetBuilder } from '../../../../src/collections/componentSetBuilder';
import { FORCE_APP, MDAPI_OUT, fileSnap, dirEntsToPaths } from '../../helper/conversions';

// we don't want failing tests outputting over each other
/* eslint-disable no-await-in-loop */

const folder = 'singleCustomFieldRetrieve';
const tmpFolder = `${folder}Tmp`;
const testOriginalDir = path.join('test', 'snapshot', 'sampleProjects', folder);
const testDir = testOriginalDir.replace(folder, tmpFolder);

/**
* retrieving a single field retrieves the object, removes the field from it, and leaves a blank object.
* That blank object should NOT overwrite the existing object if it exists in the merge target
*/
describe('a single field in a CustomObject xml does not overwrite (blank) the existing Object', () => {
before(async () => {
// because we're applying changes over the existing source, move it to a new place
await fs.promises.cp(testOriginalDir, testDir, {
recursive: true,
force: true,
filter: (src) => !src.includes('__snapshots__'),
});
});
it('merge a single retrieved CustomField (Email__c) into project', async () => {
// SDR should match the original source
const cs = await ComponentSetBuilder.build({
sourcepath: [path.join(testDir, MDAPI_OUT)],
});
// a CS from the destination
const mergeWith = (
await ComponentSetBuilder.build({
sourcepath: [path.join(testDir, FORCE_APP)],
})
).getSourceComponents();

const converter = new MetadataConverter();

await converter.convert(cs, 'source', {
type: 'merge',
mergeWith,
defaultDirectory: path.resolve(path.join(testDir, FORCE_APP)),
});
});

it(`verify ${FORCE_APP}`, async () => {
await validateSourceDir(FORCE_APP);
});

after(async () => {
await fs.promises.rm(testDir, { recursive: true, force: true });
});
});

const validateSourceDir = async (dir: string): Promise<void> => {
const sourceFiles = dirEntsToPaths(
await fs.promises.readdir(path.join(testDir, dir), {
recursive: true,
withFileTypes: true,
})
);
for (const file of sourceFiles) {
await fileSnap(file, testOriginalDir, testDir);
}
};
Loading

0 comments on commit b8a8c7f

Please sign in to comment.