Skip to content

Commit

Permalink
feat: add package1VersionCreate command, refactor tests, allow .test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Jul 28, 2022
1 parent 5a7982f commit aa34e8c
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .sfdevrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"test": {
"testsPath": "test/**/*Test.ts"
"testsPath": "test/**/*est.ts"
}
}
9 changes: 9 additions & 0 deletions messages/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,12 @@ Un-packaged metadata directory %s was specified but does not exist.
# directoryDoesNotExist

Directory %s does not exist.

# package1VersionCreateCommandUploadFailureDefault

Package version creation failed with unknown error

# package1VersionCreateCommandUploadFailure

Package upload failed.
%s
13 changes: 13 additions & 0 deletions src/interfaces/packagingInterfacesAndType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,16 @@ export type PackageVersionReportResult = Partial<PackagingSObjects.Package2Versi
export type PackageVersionCreateReportProgress = PackageVersionCreateRequestResult & {
remainingWaitTime: Duration;
};

export type Package1VersionCreateRequest = Pick<
PackagingSObjects.PackageUploadRequest,
| 'MetadataPackageId'
| 'VersionName'
| 'Description'
| 'MajorVersion'
| 'MinorVersion'
| 'IsReleaseVersion'
| 'ReleaseNotesUrl'
| 'PostInstallUrl'
| 'Password'
>;
1 change: 1 addition & 0 deletions src/package1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
export * from './packageVersion1GP';
export * from './packageDisplay';
export * from './package1VersionCreateGet';
export * from './package1VersionCreate';
55 changes: 55 additions & 0 deletions src/package1/package1VersionCreate.ts
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;
}
}
180 changes: 180 additions & 0 deletions test/package1/package1VersionCreate.test.ts
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);
});
});
13 changes: 0 additions & 13 deletions test/package1/package1VersionTest.ts

This file was deleted.

60 changes: 0 additions & 60 deletions test/package1/packageDisplay.test.ts

This file was deleted.

0 comments on commit aa34e8c

Please sign in to comment.