generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstart.test.ts
More file actions
466 lines (433 loc) · 16.8 KB
/
start.test.ts
File metadata and controls
466 lines (433 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (c) 2020, 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 { resolve } from 'node:path';
import fs from 'node:fs/promises';
import { Stats } from 'node:fs';
import sinon from 'sinon';
import { expect } from 'chai';
import {
ComponentLike,
ComponentSet,
ComponentSetBuilder,
ComponentSetOptions,
MetadataType,
RequestStatus,
RetrieveOptions,
} from '@salesforce/source-deploy-retrieve';
import { Messages, SfProject } from '@salesforce/core';
import { stubMethod, stubInterface } from '@salesforce/ts-sinon';
import { stubSfCommandUx, stubSpinner, stubUx } from '@salesforce/sf-plugins-core';
import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup';
import { RetrieveResultFormatter } from '../../../src/formatters/retrieveResultFormatter.js';
import { getRetrieveResult } from '../../utils/retrieveResponse.js';
import { RetrieveResultJson } from '../../../src/utils/types.js';
import { exampleSourceComponent } from '../../utils/testConsts.js';
import RetrieveMetadata from '../../../src/commands/project/retrieve/start.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-deploy-retrieve', 'retrieve.start');
describe('project retrieve start', () => {
const $$ = new TestContext();
const testOrg = new MockTestOrgData();
testOrg.tracksSource = true;
let sfCommandUxStubs: ReturnType<typeof stubSfCommandUx>;
let uxStubs: ReturnType<typeof stubUx>;
testOrg.username = 'retrieve-test@org.com';
const packageXml = 'package.xml';
const defaultPackagePath = 'defaultPackagePath';
const retrieveResult = getRetrieveResult('success');
const expectedResults: RetrieveResultJson = {
fileProperties: retrieveResult.response.fileProperties,
id: '09S21000002jxznEAA',
status: RequestStatus.Succeeded,
success: true,
done: true,
files: retrieveResult.getFileResponses(),
};
// Stubs
let buildComponentSetStub: sinon.SinonStub;
let retrieveStub: sinon.SinonStub;
let pollStub: sinon.SinonStub;
let expectedDirectoryPath: string;
beforeEach(async () => {
await $$.stubAuths(testOrg);
await $$.stubConfig({ 'target-org': testOrg.username });
// the 2 oclif flags should act as if the dir/file is there and ok
$$.SANDBOX.stub(fs, 'stat').resolves(
stubInterface<Stats>($$.SANDBOX, {
isDirectory: () => true,
isFile: () => true,
})
);
sfCommandUxStubs = stubSfCommandUx($$.SANDBOX);
uxStubs = stubUx($$.SANDBOX);
stubSpinner($$.SANDBOX);
$$.setConfigStubContents('SfProjectJson', {
contents: {
packageDirectories: [
{
path: defaultPackagePath,
fullPath: defaultPackagePath,
default: true,
},
],
},
});
expectedDirectoryPath = SfProject.getInstance().getDefaultPackage().fullPath;
pollStub = $$.SANDBOX.stub().resolves(retrieveResult);
retrieveStub = $$.SANDBOX.stub().resolves({
onUpdate: pollStub,
onFinish: pollStub,
pollStatus: pollStub,
onCancel: () => {},
onError: () => {},
retrieveId: retrieveResult.response.id,
});
buildComponentSetStub = stubMethod($$.SANDBOX, ComponentSetBuilder, 'build').resolves({
retrieve: retrieveStub,
getPackageXml: () => packageXml,
toArray: () => [exampleSourceComponent],
has: () => false,
});
});
// Ensure SourceCommand.createComponentSet() args
const ensureCreateComponentSetArgs = (overrides?: Partial<ComponentSetOptions>) => {
const defaultArgs = {
packagenames: undefined,
sourcepath: undefined,
manifest: undefined,
metadata: undefined,
apiversion: undefined,
sourceapiversion: undefined,
};
const expectedArgs = { ...defaultArgs, ...overrides };
expect(buildComponentSetStub.calledOnce).to.equal(true);
expect(JSON.parse(JSON.stringify(buildComponentSetStub.firstCall.args[0]))).to.deep.include(
JSON.parse(JSON.stringify(expectedArgs))
);
};
// Ensure ComponentSet.retrieve() args
const ensureRetrieveArgs = (overrides?: Partial<RetrieveOptions>) => {
const defaultRetrieveArgs = {
usernameOrConnection: testOrg.username,
merge: true,
output: SfProject.getInstance().getDefaultPackage().fullPath,
packageOptions: undefined,
};
const expectedRetrieveArgs = { ...defaultRetrieveArgs, ...overrides };
expect(retrieveStub.calledOnce).to.equal(true);
expect(retrieveStub.firstCall.args[0]).to.deep.equal(expectedRetrieveArgs);
};
it('should pass along sourcepath', async () => {
const sourcepath = ['somepath'];
const result = await RetrieveMetadata.run(['--source-dir', sourcepath[0], '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({ sourcepath });
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along retrievetargetdir', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const renameStub = $$.SANDBOX.stub(RetrieveMetadata.prototype, 'moveResultsForRetrieveTargetDir').resolves();
const sourcepath = ['somepath'];
const metadata = ['ApexClass:MyClass'];
const result = await RetrieveMetadata.run(['--output-dir', sourcepath[0], '--metadata', metadata[0], '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
sourcepath: undefined,
metadata: {
directoryPaths: [],
metadataEntries: ['ApexClass:MyClass'],
},
});
ensureRetrieveArgs({ output: resolve(sourcepath[0]), format: 'source' });
expect(renameStub.calledOnce).to.be.true;
});
it('should pass along metadata', async () => {
const metadata = ['ApexClass:MyClass'];
const result = await RetrieveMetadata.run(['--metadata', metadata[0], '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
metadata: {
metadataEntries: metadata,
directoryPaths: [expectedDirectoryPath],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along metadata and org for wildcard matching', async () => {
const metadata = ['ApexClass:MyC*'];
const result = await RetrieveMetadata.run(['--metadata', metadata[0], '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
metadata: {
metadataEntries: metadata,
directoryPaths: [expectedDirectoryPath],
},
org: {
username: testOrg.username,
exclude: [],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along metadata and org for pseudo-type matching', async () => {
const metadata = ['Agent:My_Agent'];
const result = await RetrieveMetadata.run(['--metadata', metadata[0], '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
metadata: {
metadataEntries: metadata,
directoryPaths: [expectedDirectoryPath],
},
org: {
username: testOrg.username,
exclude: [],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along metadata and org for pseudo-type wildcard matching', async () => {
const metadata = ['ApexClass', 'Agent'];
const result = await RetrieveMetadata.run(['--metadata', metadata[0], '--metadata', metadata[1], '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
metadata: {
metadataEntries: metadata,
directoryPaths: [expectedDirectoryPath],
},
org: {
username: testOrg.username,
exclude: [],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along manifest', async () => {
const manifest = 'package.xml';
const result = await RetrieveMetadata.run(['--manifest', manifest, '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
manifest: {
manifestPath: manifest,
directoryPaths: [expectedDirectoryPath],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along apiversion', async () => {
const manifest = 'package.xml';
const apiversion = '50.0';
const result = await RetrieveMetadata.run(['--manifest', manifest, '--api-version', apiversion, '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
apiversion,
manifest: {
manifestPath: manifest,
directoryPaths: [expectedDirectoryPath],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along sourceapiversion', async () => {
const sourceApiVersion = '50.0';
const manifest = 'package.xml';
$$.SANDBOX.stub(SfProject.prototype, 'resolveProjectConfig').resolves({ sourceApiVersion });
const result = await RetrieveMetadata.run(['--manifest', manifest, '--json']);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
sourceapiversion: sourceApiVersion,
manifest: {
manifestPath: manifest,
directoryPaths: [expectedDirectoryPath],
},
});
ensureRetrieveArgs({ format: 'source' });
});
it('should pass along packagenames', async () => {
const manifest = 'package.xml';
const packagenames = ['package1'];
await RetrieveMetadata.run(['--manifest', manifest, '--package-name', packagenames[0], '--json']);
ensureCreateComponentSetArgs({
packagenames,
manifest: {
manifestPath: manifest,
directoryPaths: [expectedDirectoryPath],
},
});
ensureRetrieveArgs({ packageOptions: packagenames, format: 'source' });
});
it('should display warning when using package-name flag', async () => {
await RetrieveMetadata.run(['--package-name', 'package1']);
expect(uxStubs.warn.firstCall.args[0]).to.equal(
'Metadata from retrieved packages is meant for your reference only, not development.'
);
});
it('should pass along multiple packagenames', async () => {
const manifest = 'package.xml';
const packagenames = ['package1', 'package2'];
const result = await RetrieveMetadata.run([
'--manifest',
manifest,
'--package-name',
'package1',
'--package-name',
'package2',
'--json',
]);
expect(result).to.deep.equal(expectedResults);
ensureCreateComponentSetArgs({
packagenames,
manifest: {
manifestPath: manifest,
directoryPaths: [expectedDirectoryPath],
},
});
ensureRetrieveArgs({ packageOptions: packagenames, format: 'source' });
});
it('should display output with no --json', async () => {
const displayStub = $$.SANDBOX.stub(RetrieveResultFormatter.prototype, 'display');
const getJsonStub = $$.SANDBOX.stub(RetrieveResultFormatter.prototype, 'getJson');
await RetrieveMetadata.run(['--source-dir', 'somepath']);
expect(displayStub.calledOnce).to.equal(true);
expect(getJsonStub.calledOnce).to.equal(true);
});
it('should NOT display output with --json', async () => {
const displayStub = $$.SANDBOX.stub(RetrieveResultFormatter.prototype, 'display');
const getJsonStub = $$.SANDBOX.stub(RetrieveResultFormatter.prototype, 'getJson');
await RetrieveMetadata.run(['--source-dir', 'somepath', '--json']);
expect(displayStub.calledOnce).to.equal(false);
expect(getJsonStub.calledOnce).to.equal(true);
});
it('should warn users when retrieving CustomField with --metadata', async () => {
const metadata = 'CustomField';
buildComponentSetStub.restore();
buildComponentSetStub = stubMethod($$.SANDBOX, ComponentSetBuilder, 'build').resolves({
retrieve: retrieveStub,
getPackageXml: () => packageXml,
toArray: () => [exampleSourceComponent],
add: (component: ComponentLike) => {
expect(component)
.to.be.a('object')
.and.to.have.property('type')
.and.to.deep.include({ id: 'customobject', name: 'CustomObject' });
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
},
has: (component: ComponentLike) => {
expect(component).to.be.a('object').and.to.have.property('type');
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
const type = component.type as MetadataType;
if (type.name === 'CustomField') {
return true;
}
if (type.name === 'CustomObject') {
return false;
}
},
});
await RetrieveMetadata.run(['--metadata', metadata]);
expect(sfCommandUxStubs.warn.called).to.be.true;
expect(
sfCommandUxStubs.warn
.getCalls()
.flatMap((call) => call.args)
.includes(messages.getMessage('wantsToRetrieveCustomFields'))
);
});
it('should not warn users when retrieving CustomField,CustomObject with --metadata', async () => {
const metadata = 'CustomField,CustomObject';
buildComponentSetStub.restore();
buildComponentSetStub = stubMethod($$.SANDBOX, ComponentSetBuilder, 'build').resolves({
retrieve: retrieveStub,
getPackageXml: () => packageXml,
toArray: () => [exampleSourceComponent],
add: (component: ComponentLike) => {
expect(component)
.to.be.a('object')
.and.to.have.property('type')
.and.to.deep.equal({ id: 'customobject', name: 'CustomObject' });
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
},
has: (component: ComponentLike) => {
expect(component).to.be.a('object').and.to.have.property('type');
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
const type = component.type as MetadataType;
if (type.name === 'CustomField') {
return true;
}
if (type.name === 'CustomObject') {
return true;
}
},
});
await RetrieveMetadata.run(['--metadata', metadata]);
expect(sfCommandUxStubs.warn.called).to.be.false;
});
it('should warn users when retrieving CustomField with --manifest', async () => {
const manifest = 'package.xml';
buildComponentSetStub.restore();
buildComponentSetStub = stubMethod($$.SANDBOX, ComponentSetBuilder, 'build').resolves({
retrieve: retrieveStub,
getPackageXml: () => packageXml,
toArray: () => [exampleSourceComponent],
add: (component: ComponentLike) => {
expect(component)
.to.be.a('object')
.and.to.have.property('type')
.and.to.deep.include({ id: 'customobject', name: 'CustomObject' });
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
},
has: (component: ComponentLike) => {
expect(component).to.be.a('object').and.to.have.property('type');
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
const type = component.type as MetadataType;
if (type.name === 'CustomField') {
return true;
}
if (type.name === 'CustomObject') {
return false;
}
},
});
await RetrieveMetadata.run(['--manifest', manifest]);
expect(sfCommandUxStubs.warn.called).to.be.true;
expect(
sfCommandUxStubs.warn
.getCalls()
.flatMap((call) => call.args)
.includes(messages.getMessage('wantsToRetrieveCustomFields'))
);
});
it('should not be warn users when retrieving CustomField,CustomObject with --manifest', async () => {
const manifest = 'package.xml';
buildComponentSetStub.restore();
buildComponentSetStub = stubMethod($$.SANDBOX, ComponentSetBuilder, 'build').resolves({
retrieve: retrieveStub,
getPackageXml: () => packageXml,
toArray: () => [exampleSourceComponent],
add: (component: ComponentLike) => {
expect(component)
.to.be.a('object')
.and.to.have.property('type')
.and.to.deep.equal({ id: 'customobject', name: 'CustomObject' });
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
},
has: (component: ComponentLike) => {
expect(component).to.be.a('object').and.to.have.property('type');
expect(component).and.to.have.property('fullName').and.to.be.equal(ComponentSet.WILDCARD);
const type = component.type as MetadataType;
if (type.name === 'CustomField') {
return true;
}
if (type.name === 'CustomObject') {
return true;
}
},
});
await RetrieveMetadata.run(['--manifest', manifest]);
expect(sfCommandUxStubs.warn.called).to.be.false;
});
});