-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add package1VersionCreate command, refactor tests, allow .test.ts
- Loading branch information
1 parent
5a7982f
commit aa34e8c
Showing
8 changed files
with
259 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"test": { | ||
"testsPath": "test/**/*Test.ts" | ||
"testsPath": "test/**/*est.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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 * as os from 'os'; | ||
import { Connection, Lifecycle, Messages, PollingClient } from '@salesforce/core'; | ||
import { Duration } from '@salesforce/kit'; | ||
import { Package1VersionCreateRequest, PackagingSObjects } from '../interfaces'; | ||
|
||
Messages.importMessagesDirectory(__dirname); | ||
const messages = Messages.loadMessages('@salesforce/packaging', 'messages'); | ||
|
||
export async function package1VersionCreate( | ||
connection: Connection, | ||
options: Package1VersionCreateRequest, | ||
pollingOptions = { frequency: Duration.seconds(5), timeout: Duration.seconds(0) } | ||
): Promise<PackagingSObjects.PackageUploadRequest> { | ||
const createRequest = await connection.tooling.sobject('PackageUploadRequest').create(options); | ||
if (pollingOptions.timeout.seconds) { | ||
let timeout = pollingOptions.timeout.seconds; | ||
const pollingClient = await PollingClient.create({ | ||
poll: async () => { | ||
const pollingResult = await connection.tooling.sobject('PackageUploadRequest').retrieve(createRequest.id); | ||
switch (pollingResult.Status) { | ||
case 'SUCCESS': | ||
return { completed: true, payload: pollingResult }; | ||
case 'IN_PROGRESS': | ||
case 'QUEUED': | ||
timeout -= pollingOptions.frequency.seconds; | ||
await Lifecycle.getInstance().emit('package1VersionCreate:progress', { timeout, pollingResult }); | ||
|
||
return { completed: false, payload: pollingResult }; | ||
default: { | ||
if (pollingResult?.Errors?.errors?.length > 0) { | ||
throw messages.createError('package1VersionCreateCommandUploadFailure', [ | ||
(pollingResult.Errors.errors.map((e: Error) => e.message) as string[]).join(os.EOL), | ||
]); | ||
} else { | ||
throw messages.createError('package1VersionCreateCommandUploadFailureDefault'); | ||
} | ||
} | ||
} | ||
}, | ||
...pollingOptions, | ||
}); | ||
return pollingClient.subscribe<PackagingSObjects.PackageUploadRequest>(); | ||
} else { | ||
// jsforce templates weren't working when setting the type to PackageUploadRequest, so we have to cast `as unkown as PackagingSObjects.PackageUploadRequest` | ||
return (await connection.tooling | ||
.sobject('PackageUploadRequest') | ||
.retrieve(createRequest.id)) as unknown as PackagingSObjects.PackageUploadRequest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* 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 { MockTestOrgData, restoreContext, testSetup } from '@salesforce/core/lib/testSetup'; | ||
import { Duration } from '@salesforce/kit'; | ||
import { expect } from 'chai'; | ||
import { Connection, Lifecycle } from '@salesforce/core'; | ||
import { assert } from 'sinon'; | ||
import { package1Display, package1VersionCreate } from '../../src/package1'; | ||
import { PackagingSObjects } from '../../src/interfaces'; | ||
|
||
const options = { | ||
MetadataPackageId: '0HD4p000000blVyGAI', | ||
VersionName: 'Test', | ||
Description: 'Test', | ||
MajorVersion: 0, | ||
MinorVersion: 0, | ||
IsReleaseVersion: false, | ||
ReleaseNotesUrl: 'Test', | ||
PostInstallUrl: 'Test', | ||
Password: 'Test', | ||
}; | ||
|
||
const successResult = { | ||
Status: 'SUCCESS', | ||
Id: '0HD4p000000blVyGAI', | ||
MetadataPackageVersionId: '04t4p000002Bb4lAAC', | ||
MetadataPackageId: '03346000000MrC0AAK', | ||
}; | ||
|
||
const queuedResult = { | ||
Status: 'QUEUED', | ||
Id: '0HD4p000000blVyGAI', | ||
MetadataPackageVersionId: '04t4p000002Bb4lAAC', | ||
MetadataPackageId: '03346000000MrC0AAK', | ||
}; | ||
|
||
describe('Package1 Version Create and Display', () => { | ||
const $$ = testSetup(); | ||
const testOrg = new MockTestOrgData(); | ||
let conn: Connection; | ||
let sobjectStub: sinon.SinonStub; | ||
let queryStub: sinon.SinonStub; | ||
|
||
beforeEach(async () => { | ||
await $$.stubAuths(testOrg); | ||
conn = await testOrg.getConnection(); | ||
queryStub = $$.SANDBOX.stub(conn.tooling, 'query'); | ||
sobjectStub = $$.SANDBOX.stub(conn.tooling, 'sobject') | ||
.onFirstCall() | ||
.returns({ | ||
// @ts-ignore - to avoid stubbing every property of sobject | ||
create: () => ({ id: '0HD4p000000blUvGXX' }), | ||
}); | ||
}); | ||
|
||
after(() => { | ||
restoreContext($$); | ||
}); | ||
|
||
it('should query and collate data correctly', async () => { | ||
queryStub.resolves({ | ||
done: true, | ||
totalSize: 1, | ||
records: [ | ||
{ | ||
Id: '04t46000001ZfaXXXX', | ||
Name: 'Summer 22', | ||
MetadataPackageId: '03346000000dmo4XXX', | ||
MajorVersion: 1, | ||
MinorVersion: 0, | ||
PatchVersion: 3, | ||
ReleaseState: 'Beta', | ||
BuildNumber: 1, | ||
}, | ||
], | ||
}); | ||
const result = await package1Display(conn, '04t46000001ZfaXXXX'); | ||
expect(result).deep.equal([ | ||
{ | ||
BuildNumber: 1, | ||
MetadataPackageId: '03346000000dmo4XXX', | ||
MetadataPackageVersionId: '04t46000001ZfaXXXX', | ||
Name: 'Summer 22', | ||
ReleaseState: 'Beta', | ||
Version: '1.0.3', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should query and collate data correctly - no results', async () => { | ||
queryStub.resolves({ | ||
done: true, | ||
totalSize: 0, | ||
records: [], | ||
}); | ||
const result = await package1Display(conn, '04t46000001ZfaXXXX'); | ||
expect(result).deep.equal([]); | ||
}); | ||
it('should send the create request, wait for it to finish, and emit events along the way', async () => { | ||
sobjectStub | ||
.onSecondCall() | ||
.returns({ | ||
// @ts-ignore | ||
retrieve: async () => queuedResult, | ||
}) | ||
.onThirdCall() | ||
.returns({ | ||
// @ts-ignore | ||
retrieve: async () => successResult, | ||
}); | ||
Lifecycle.getInstance().on( | ||
'package1VersionCreate:progress', | ||
async (data: { timeout: number; pollingResult: PackagingSObjects.PackageUploadRequest }) => { | ||
// 3 minute timeout (180 seconds) - 1 second per poll | ||
expect(data.timeout).to.equal(179); | ||
expect(data.pollingResult.Status).to.equal('QUEUED'); | ||
} | ||
); | ||
|
||
const result = await package1VersionCreate(conn, options, { | ||
frequency: Duration.seconds(1), | ||
timeout: Duration.minutes(3), | ||
}); | ||
expect(result).deep.equal(successResult); | ||
}); | ||
|
||
it('should send the create request, and handle errors appropriately', async () => { | ||
sobjectStub.onSecondCall().returns({ | ||
// @ts-ignore | ||
retrieve: async () => ({ | ||
Status: 'ERROR', | ||
Errors: { errors: [new Error('message 1'), new Error('message 2')] }, | ||
}), | ||
}); | ||
|
||
try { | ||
await package1VersionCreate(conn, options, { frequency: Duration.seconds(1), timeout: Duration.minutes(3) }); | ||
assert.fail('the above should throw an error from polling'); | ||
} catch (e) { | ||
expect((e as Error).message).to.equal('Package upload failed. \nmessage 1\nmessage 2'); | ||
} | ||
}); | ||
|
||
it('should send the create request, and handle errors appropriately (0 error messages)', async () => { | ||
sobjectStub.onSecondCall().returns({ | ||
// @ts-ignore | ||
retrieve: async () => ({ | ||
Status: 'ERROR', | ||
Errors: [], | ||
}), | ||
}); | ||
Lifecycle.getInstance().on( | ||
'package1VersionCreate:progress', | ||
async (data: { timeout: number; pollingResult: PackagingSObjects.PackageUploadRequest }) => { | ||
// 3 minute timeout (180 seconds) - 1 second per poll | ||
expect(data.timeout).to.equal(179); | ||
} | ||
); | ||
try { | ||
await package1VersionCreate(conn, options, { frequency: Duration.seconds(1), timeout: Duration.minutes(3) }); | ||
assert.fail('the above should throw an error from polling'); | ||
} catch (e) { | ||
expect((e as Error).message).to.equal('Package version creation failed with unknown error'); | ||
} | ||
}); | ||
|
||
it('should send the create request, retrieve the request and return', async () => { | ||
sobjectStub.onSecondCall().returns({ | ||
// @ts-ignore | ||
retrieve: async () => queuedResult, | ||
}); | ||
|
||
const result = await package1VersionCreate(conn, options); | ||
expect(result).deep.equal(queuedResult); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.