@@ -9,11 +9,14 @@ import * as path from 'node:path';
99import {
1010 FORCE_APP ,
1111 MDAPI_OUT ,
12+ dirEntsToPaths ,
1213 dirsAreIdentical ,
1314 fileSnap ,
1415 mdapiToSource ,
1516 sourceToMdapi ,
1617} from '../../helper/conversions' ;
18+ import { MetadataConverter } from '../../../../src/convert/metadataConverter' ;
19+ import { ComponentSetBuilder } from '../../../../src/collections/componentSetBuilder' ;
1720
1821// we don't want failing tests outputting over each other
1922/* eslint-disable no-await-in-loop */
@@ -49,3 +52,138 @@ describe('Custom objects and children', () => {
4952 ] ) ;
5053 } ) ;
5154} ) ;
55+
56+ /** Return only the files involved in the conversion */
57+ const getConvertedFilePaths = async ( outputDir : string ) : Promise < string [ ] > =>
58+ dirEntsToPaths (
59+ await fs . promises . readdir ( outputDir , {
60+ recursive : true ,
61+ withFileTypes : true ,
62+ } )
63+ ) ;
64+
65+ describe ( 'CustomField with empty CustomObject - retrieve' , ( ) => {
66+ const testDir = path . join ( 'test' , 'snapshot' , 'sampleProjects' , 'customObjects-and-children' ) ;
67+
68+ // The directory of snapshots containing expected conversion results
69+ const snapshotsDir = path . join ( testDir , '__snapshots__' ) ;
70+
71+ // MDAPI format of the original source
72+ const sourceDir = path . join ( testDir , 'originalMdapi2' ) ;
73+
74+ // The directory where metadata is converted as part of retrieve testing
75+ const retrieveOutput = path . join ( testDir , 'retrieveOutput' ) ;
76+
77+ // This test verifies that 1 custom field with an empty parent
78+ // does not omit the parent from the package manifest for a retrieve when
79+ // converting from source to mdapi.
80+ it ( 'verify md files retrieve' , async ( ) => {
81+ const cs = await ComponentSetBuilder . build ( {
82+ metadata : {
83+ metadataEntries : [ 'CustomObject:Broker__c' ] ,
84+ directoryPaths : [ sourceDir ] ,
85+ } ,
86+ projectDir : testDir ,
87+ } ) ;
88+
89+ const sourceOutputDir = path . join ( retrieveOutput , 'source' ) ;
90+ const mdOutputDir = path . join ( retrieveOutput , 'mdapi' ) ;
91+
92+ await new MetadataConverter ( ) . convert ( cs , 'source' , {
93+ type : 'directory' ,
94+ outputDirectory : sourceOutputDir ,
95+ genUniqueDir : false ,
96+ } ) ;
97+
98+ const cs2 = await ComponentSetBuilder . build ( {
99+ metadata : {
100+ metadataEntries : [ 'CustomObject:Broker__c' ] ,
101+ directoryPaths : [ sourceOutputDir ] ,
102+ } ,
103+ projectDir : testDir ,
104+ } ) ;
105+
106+ // @ts -expect-error modifying private property
107+ cs2 . forRetrieve = true ;
108+
109+ await new MetadataConverter ( ) . convert ( cs2 , 'metadata' , {
110+ type : 'directory' ,
111+ outputDirectory : mdOutputDir ,
112+ genUniqueDir : false ,
113+ } ) ;
114+
115+ const convertedFiles = await getConvertedFilePaths ( mdOutputDir ) ;
116+ for ( const file of convertedFiles ) {
117+ await fileSnap ( file , testDir ) ;
118+ }
119+ const expectedOutputDir = path . join ( snapshotsDir , 'verify-md-files-retrieve.expected' , 'retrieveOutput' , 'mdapi' ) ;
120+ await dirsAreIdentical ( expectedOutputDir , mdOutputDir ) ;
121+ } ) ;
122+
123+ after ( async ( ) => {
124+ await Promise . all ( [ fs . promises . rm ( retrieveOutput , { recursive : true , force : true } ) ] ) ;
125+ } ) ;
126+ } ) ;
127+
128+ describe ( 'CustomField with empty CustomObject - deploy' , ( ) => {
129+ const testDir = path . join ( 'test' , 'snapshot' , 'sampleProjects' , 'customObjects-and-children' ) ;
130+
131+ // The directory of snapshots containing expected conversion results
132+ const snapshotsDir = path . join ( testDir , '__snapshots__' ) ;
133+
134+ // MDAPI format of the original source
135+ const sourceDir = path . join ( testDir , 'originalMdapi2' ) ;
136+
137+ // The directory where metadata is converted as part of deploy testing
138+ const deployOutput = path . join ( testDir , 'deployOutput' ) ;
139+
140+ // This test verifies that 1 custom field with an empty parent
141+ // omits the parent from the package manifest for a deploy when
142+ // converting from source to mdapi.
143+ it ( 'verify md files deploy' , async ( ) => {
144+ const cs = await ComponentSetBuilder . build ( {
145+ metadata : {
146+ metadataEntries : [ 'CustomObject:Broker__c' ] ,
147+ directoryPaths : [ sourceDir ] ,
148+ } ,
149+ projectDir : testDir ,
150+ } ) ;
151+
152+ const sourceOutputDir = path . join ( deployOutput , 'source' ) ;
153+ const mdOutputDir = path . join ( deployOutput , 'mdapi' ) ;
154+
155+ await new MetadataConverter ( ) . convert ( cs , 'source' , {
156+ type : 'directory' ,
157+ outputDirectory : sourceOutputDir ,
158+ genUniqueDir : false ,
159+ } ) ;
160+
161+ const cs2 = await ComponentSetBuilder . build ( {
162+ metadata : {
163+ metadataEntries : [ 'CustomObject:Broker__c' ] ,
164+ directoryPaths : [ sourceOutputDir ] ,
165+ } ,
166+ projectDir : testDir ,
167+ } ) ;
168+
169+ // @ts -expect-error modifying private property
170+ cs2 . forDeploy = true ;
171+
172+ await new MetadataConverter ( ) . convert ( cs2 , 'metadata' , {
173+ type : 'directory' ,
174+ outputDirectory : mdOutputDir ,
175+ genUniqueDir : false ,
176+ } ) ;
177+
178+ const convertedFiles = await getConvertedFilePaths ( mdOutputDir ) ;
179+ for ( const file of convertedFiles ) {
180+ await fileSnap ( file , testDir ) ;
181+ }
182+ const expectedOutputDir = path . join ( snapshotsDir , 'verify-md-files-deploy.expected' , 'deployOutput' , 'mdapi' ) ;
183+ await dirsAreIdentical ( expectedOutputDir , mdOutputDir ) ;
184+ } ) ;
185+
186+ after ( async ( ) => {
187+ await Promise . all ( [ fs . promises . rm ( deployOutput , { recursive : true , force : true } ) ] ) ;
188+ } ) ;
189+ } ) ;
0 commit comments