-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpackageBundleVersionCreate.test.ts
More file actions
363 lines (330 loc) · 14.3 KB
/
Copy pathpackageBundleVersionCreate.test.ts
File metadata and controls
363 lines (330 loc) · 14.3 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
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup';
import { Config } from '@oclif/core';
import { assert, expect } from 'chai';
import { PackageBundleVersion, BundleSObjects } from '@salesforce/packaging';
import sinon from 'sinon';
import { SfCommand } from '@salesforce/sf-plugins-core';
import { PackageBundlesCreate } from '../../../src/commands/package/bundle/version/create.js';
const pkgBundleVersionCreateErrorResult: BundleSObjects.PackageBundleVersionCreateRequestResult = {
Id: '08c3i000000fylXXXX',
RequestStatus: BundleSObjects.PkgBundleVersionCreateReqStatus.error,
PackageBundleId: '0Ho3i000000TNHXXXX',
PackageBundleVersionId: '',
VersionName: 'TestBundle@1.0',
MajorVersion: '1',
MinorVersion: '0',
BundleVersionComponents: '[]',
Error: [
'PropertyController: Invalid type: Schema.Property__c',
'SampleDataController: Invalid type: Schema.Property__c',
'SampleDataController: Invalid type: Schema.Broker__c',
],
CreatedDate: '2022-11-03 09:21',
CreatedById: '0053i000001ZIyXXXX',
Ancestor: null,
};
const pkgBundleVersionCreateSuccessResult: BundleSObjects.PackageBundleVersionCreateRequestResult = {
Id: '08c3i000000fylgAAA',
RequestStatus: BundleSObjects.PkgBundleVersionCreateReqStatus.success,
PackageBundleId: '0Ho3i000000TNHYCA4',
PackageBundleVersionId: '05i3i000000fxw1AAA',
VersionName: 'TestBundle@1.0',
MajorVersion: '1',
MinorVersion: '0',
BundleVersionComponents: '[{"packageId": "0Ho3i000000TNHYCA4", "versionNumber": "1.0.0"}]',
Error: [],
CreatedDate: '2022-11-03 09:46',
CreatedById: '0053i000001ZIyGAAW',
Ancestor: null,
};
const pkgBundleVersionCreateQueuedResult: BundleSObjects.PackageBundleVersionCreateRequestResult = {
Id: '08c3i000000fylgBBB',
RequestStatus: BundleSObjects.PkgBundleVersionCreateReqStatus.queued,
PackageBundleId: '0Ho3i000000TNHYCA4',
PackageBundleVersionId: '',
VersionName: 'TestBundle@1.1',
MajorVersion: '1',
MinorVersion: '1',
BundleVersionComponents: '[{"packageId": "0Ho3i000000TNHYCA4", "versionNumber": "1.1.0"}]',
Error: [],
CreatedDate: '2022-11-03 10:00',
CreatedById: '0053i000001ZIyGAAW',
Ancestor: null,
};
describe('package:bundle:version:create - tests', () => {
const $$ = new TestContext();
const testOrg = new MockTestOrgData();
let createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create');
const config = new Config({ root: import.meta.url });
// stubs
let logStub: sinon.SinonStub;
let warnStub: sinon.SinonStub;
const stubSpinner = (cmd: PackageBundlesCreate) => {
$$.SANDBOX.stub(cmd.spinner, 'start');
$$.SANDBOX.stub(cmd.spinner, 'stop');
};
before(async () => {
await $$.stubAuths(testOrg);
await config.load();
});
beforeEach(async () => {
logStub = $$.SANDBOX.stub(SfCommand.prototype, 'log');
warnStub = $$.SANDBOX.stub(SfCommand.prototype, 'warn');
});
afterEach(() => {
$$.restore();
});
describe('package:bundle:version:create', () => {
it('should create a new package bundle version', async () => {
createStub.resolves(pkgBundleVersionCreateSuccessResult);
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', 'path/to/definition.json', '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
const res = await cmd.run();
expect(res).to.deep.equal({
Id: '08c3i000000fylgAAA',
RequestStatus: 'Success',
PackageBundleId: '0Ho3i000000TNHYCA4',
PackageBundleVersionId: '05i3i000000fxw1AAA',
VersionName: 'TestBundle@1.0',
MajorVersion: '1',
MinorVersion: '0',
BundleVersionComponents: '[{"packageId": "0Ho3i000000TNHYCA4", "versionNumber": "1.0.0"}]',
Error: [],
CreatedDate: '2022-11-03 09:46',
CreatedById: '0053i000001ZIyGAAW',
Ancestor: null,
});
expect(warnStub.callCount).to.equal(0);
expect(logStub.callCount).to.equal(1);
expect(logStub.args[0]).to.deep.equal(['Successfully created bundle version with ID 05i3i000000fxw1AAA']);
});
it('should create a new package bundle version with wait option', async () => {
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create');
createStub.resolves(pkgBundleVersionCreateSuccessResult);
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', 'path/to/definition.json', '-w', '10', '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
const res = await cmd.run();
expect(res).to.deep.equal({
Id: '08c3i000000fylgAAA',
RequestStatus: 'Success',
PackageBundleId: '0Ho3i000000TNHYCA4',
PackageBundleVersionId: '05i3i000000fxw1AAA',
VersionName: 'TestBundle@1.0',
MajorVersion: '1',
MinorVersion: '0',
BundleVersionComponents: '[{"packageId": "0Ho3i000000TNHYCA4", "versionNumber": "1.0.0"}]',
Error: [],
CreatedDate: '2022-11-03 09:46',
CreatedById: '0053i000001ZIyGAAW',
Ancestor: null,
});
expect(warnStub.callCount).to.equal(0);
expect(logStub.callCount).to.equal(1);
expect(logStub.args[0]).to.deep.equal(['Successfully created bundle version with ID 05i3i000000fxw1AAA']);
});
it('should create a new package bundle version with verbose option', async () => {
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create');
createStub.resolves(pkgBundleVersionCreateSuccessResult);
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', 'path/to/definition.json', '--verbose', '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
const res = await cmd.run();
expect(res).to.deep.equal({
Id: '08c3i000000fylgAAA',
RequestStatus: 'Success',
PackageBundleId: '0Ho3i000000TNHYCA4',
PackageBundleVersionId: '05i3i000000fxw1AAA',
VersionName: 'TestBundle@1.0',
MajorVersion: '1',
MinorVersion: '0',
BundleVersionComponents: '[{"packageId": "0Ho3i000000TNHYCA4", "versionNumber": "1.0.0"}]',
Error: [],
CreatedDate: '2022-11-03 09:46',
CreatedById: '0053i000001ZIyGAAW',
Ancestor: null,
});
expect(warnStub.callCount).to.equal(0);
expect(logStub.callCount).to.equal(1);
expect(logStub.args[0]).to.deep.equal(['Successfully created bundle version with ID 05i3i000000fxw1AAA']);
});
it('should handle queued status', async () => {
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create');
createStub.resolves(pkgBundleVersionCreateQueuedResult);
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', 'path/to/definition.json', '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
const res = await cmd.run();
expect(res).to.deep.equal({
Id: '08c3i000000fylgBBB',
RequestStatus: 'Queued',
PackageBundleId: '0Ho3i000000TNHYCA4',
PackageBundleVersionId: '',
VersionName: 'TestBundle@1.1',
MajorVersion: '1',
MinorVersion: '1',
BundleVersionComponents: '[{"packageId": "0Ho3i000000TNHYCA4", "versionNumber": "1.1.0"}]',
Error: [],
CreatedDate: '2022-11-03 10:00',
CreatedById: '0053i000001ZIyGAAW',
Ancestor: null,
});
expect(warnStub.callCount).to.equal(0);
expect(logStub.callCount).to.equal(1);
expect(logStub.args[0]).to.deep.equal([
'Package bundle version creation is Queued. Use "sf package bundle version create report -i 08c3i000000fylgBBB" to check the status later.',
]);
});
it('should report multiple errors', async () => {
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create');
createStub.resolves(pkgBundleVersionCreateErrorResult);
try {
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', 'path/to/definition.json', '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
await cmd.run();
assert.fail('the above should throw multiple errors');
} catch (e) {
const msg = (e as Error).message.replace(/\r\n/g, '\n');
expect(msg).to.equal(
'The following errors occurred during package bundle version creation:\n' +
'PropertyController: Invalid type: Schema.Property__c\n' +
'SampleDataController: Invalid type: Schema.Property__c\n' +
'SampleDataController: Invalid type: Schema.Broker__c'
);
}
});
it('should normalize 15-character package version IDs to 18-character format', async () => {
// Capture the file content inside the stub, before the command cleans up temp files
let capturedContent: string | undefined;
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create').callsFake(
async (opts: { BundleVersionComponentsPath: string }) => {
capturedContent = await fs.promises.readFile(opts.BundleVersionComponentsPath, 'utf8');
return pkgBundleVersionCreateSuccessResult;
}
);
// Create a temporary definition file with 15-char IDs
const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'test-bundle-'));
const definitionFile = path.join(tempDir, 'definition.json');
const definitionContent = {
components: [
{ packageVersion: '04t5f000000WM9y' }, // 15-char ID
{ packageVersion: '04t5f000000WM9yAAG' }, // 18-char ID (should not change)
],
};
await fs.promises.writeFile(definitionFile, JSON.stringify(definitionContent, null, 2), 'utf8');
try {
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', definitionFile, '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
await cmd.run();
expect(createStub.callCount).to.equal(1);
assert(capturedContent, 'Expected file content to be captured');
const usedJson = JSON.parse(capturedContent) as typeof definitionContent;
// Verify that the 15-char ID was converted to 18-char
expect(usedJson.components[0].packageVersion).to.equal('04t5f000000WM9yAAG');
// Verify that the 18-char ID remained unchanged
expect(usedJson.components[1].packageVersion).to.equal('04t5f000000WM9yAAG');
} finally {
// Clean up
await fs.promises.rm(tempDir, { recursive: true, force: true });
}
});
it('should handle nested packageVersion fields in definition file', async () => {
// Capture the file content inside the stub, before the command cleans up temp files
let capturedContent: string | undefined;
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create').callsFake(
async (opts: { BundleVersionComponentsPath: string }) => {
capturedContent = await fs.promises.readFile(opts.BundleVersionComponentsPath, 'utf8');
return pkgBundleVersionCreateSuccessResult;
}
);
// Create a temporary definition file with nested structure
const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'test-bundle-'));
const definitionFile = path.join(tempDir, 'definition.json');
const definitionContent = {
bundle: {
components: [
{ packageVersion: '04t5f000000WM9y' }, // 15-char ID
{ nested: { packageVersion: '04t5f000000WM9z' } }, // nested 15-char ID
],
},
};
await fs.promises.writeFile(definitionFile, JSON.stringify(definitionContent, null, 2), 'utf8');
try {
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', definitionFile, '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
await cmd.run();
assert(capturedContent, 'Expected file content to be captured');
const usedJson = JSON.parse(capturedContent) as typeof definitionContent;
// Verify that both 15-char IDs were converted
expect(usedJson.bundle.components[0].packageVersion).to.equal('04t5f000000WM9yAAG');
expect(usedJson.bundle.components[1].nested!.packageVersion).to.equal('04t5f000000WM9zAAG');
} finally {
// Clean up
await fs.promises.rm(tempDir, { recursive: true, force: true });
}
});
it('should not modify definition file when no 15-char IDs are present', async () => {
createStub = $$.SANDBOX.stub(PackageBundleVersion, 'create');
createStub.resolves(pkgBundleVersionCreateSuccessResult);
// Create a temporary definition file with only 18-char IDs
const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'test-bundle-'));
const definitionFile = path.join(tempDir, 'definition.json');
const definitionContent = {
components: [{ packageVersion: '04t5f000000WM9yAAG' }], // 18-char ID
};
await fs.promises.writeFile(definitionFile, JSON.stringify(definitionContent, null, 2), 'utf8');
try {
const cmd = new PackageBundlesCreate(
['-b', 'TestBundle', '-p', definitionFile, '--target-dev-hub', 'test@hub.org'],
config
);
stubSpinner(cmd);
await cmd.run();
// Get the options passed to create
const createOptions = createStub.firstCall.args[0] as { BundleVersionComponentsPath: string };
const usedDefinitionPath = createOptions.BundleVersionComponentsPath;
// The path should be the original file since no normalization was needed
expect(usedDefinitionPath).to.equal(definitionFile);
} finally {
// Clean up
await fs.promises.rm(tempDir, { recursive: true, force: true });
}
});
});
});