Skip to content

Commit

Permalink
fix: add package1:beta:version:create:get command, UTs, placeholder NUT
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Jul 13, 2022
1 parent 7389687 commit 9d3f4f5
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 25 deletions.
7 changes: 2 additions & 5 deletions messages/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -897,11 +897,8 @@ Stopped waiting for package upload to finish. Wait time exceeded. waitTimeInMinu

# package1VersionCreateCommandUploadFailure

Package upload failed. ${os.EOL}%s

# package1VersionCreateCommandUploadFailureDefault

Package version creation failed with unknown error
Package upload failed.
%s

# package1VersionCreateHumanSuccess

Expand Down
10 changes: 3 additions & 7 deletions messages/package1_version_create_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

retrieve the status of a package version creation request

# cliDescriptionLong

Retrieves the status of a package version creation request.

# help
# examples

Examples:
$ sfdx force:package:version:create:report -i 08c...
$ sfdx force:package:version:create:report -i 08c... -v devhub@example.com
$ sfdx force:package1:version:create:get -i 0HD...
$ sfdx force:package1:version:create:get -i 0HD... -u devhub@example.com

# requestId

Expand Down
26 changes: 20 additions & 6 deletions src/commands/force/package1/beta/version/create/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
* 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 { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { PackagingSObjects, package1VersionCreateGet } from '@salesforce/packaging';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package1_version_create_get');
const defaultMessages = Messages.loadMessages('@salesforce/plugin-packaging', 'default');

export class Package1VersionCreateGetCommand extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
public static readonly longDescription = messages.getMessage('cliDescriptionLong');
public static readonly help = messages.getMessage('help');
public static readonly;
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
requestid: flags.id({
Expand All @@ -26,8 +27,21 @@ export class Package1VersionCreateGetCommand extends SfdxCommand {
}),
};

public async run(): Promise<unknown> {
process.exitCode = 1;
return Promise.resolve('Not yet implemented');
public async run(): Promise<PackagingSObjects.PackageUploadRequest> {
const result = await package1VersionCreateGet(this.org.getConnection(), this.flags.requestid);

if (result.Status === 'ERROR') {
// toolbelt was accessing request.Errors.errors, I'm unsure about this type, but was unable to reproduce an error
// in the wild, and decided to trust how it was working
const errors = (result.Errors as unknown as { errors: Error[] })?.errors?.map((e) => e.message).join('\n');
throw defaultMessages.createError('package1VersionCreateCommandUploadFailure', [
errors ?? 'Package version creation failed with unknown error',
]);
} else {
const arg = result.Status === 'SUCCESS' ? [result.MetadataPackageVersionId] : [result.Id, this.org.getUsername()];
this.ux.log(messages.getMessage(result.Status, arg));
}

return result;
}
}
122 changes: 122 additions & 0 deletions test/commands/force/package1/versionCreateGet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 { Org } from '@salesforce/core';
import { testSetup } from '@salesforce/core/lib/testSetup';
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
import { Config } from '@oclif/core';
import { assert, expect } from 'chai';
import { Package1VersionCreateGetCommand } from '../../../../src/commands/force/package1/beta/version/create/get';

const $$ = testSetup();
const oclifConfigStub = fromStub(stubInterface<Config>($$.SANDBOX));
let uxStub: sinon.SinonStub;

class TestCommand extends Package1VersionCreateGetCommand {
public async runIt() {
await this.init();

uxStub = stubMethod($$.SANDBOX, this.ux, 'log');
return this.run();
}
public setOrg(org: Org) {
this.org = org;
}
}

const runCmd = async (params: string[], result: string, errors?: { errors: Error[] }) => {
const cmd = new TestCommand(params, oclifConfigStub);

stubMethod($$.SANDBOX, cmd, 'assignOrg').callsFake(() => {
const orgStub = fromStub(
stubInterface<Org>($$.SANDBOX, {
getUsername: () => 'test@user.com',
getConnection: () => {
return {
tooling: {
sobject: () => {
return {
retrieve: () => {
return Promise.resolve({
Status: result,
MetadataPackageVersionId: '04t4p000002BavTXXX',
Errors: errors,
});
},
};
},
},
};
},
})
);
cmd.setOrg(orgStub);
});
return cmd.runIt();
};

describe('force:package1:version:create:get', () => {
afterEach(() => {
$$.SANDBOX.restore();
});
it('should print SUCCESS status correctly', async () => {
const result = await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'SUCCESS');
expect(result.Status).to.equal('SUCCESS');
expect(uxStub.callCount).to.equal(1);
expect(uxStub.firstCall.args[0]).to.equal('Successfully uploaded package [04t4p000002BavTXXX]');
});

it('should print IN_PROGRESS status correctly', async () => {
const result = await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'IN_PROGRESS');
expect(result.Status).to.equal('IN_PROGRESS');
expect(uxStub.callCount).to.equal(1);
expect(uxStub.firstCall.args[0]).to.equal(
'PackageUploadRequest is still InProgress. You can query the status using\n' +
'sfdx force:package1:version:create:get -i undefined -u test@user.com'
);
});

it('should print QUEUED status correctly', async () => {
const result = await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'QUEUED');
expect(result.Status).to.equal('QUEUED');
expect(uxStub.callCount).to.equal(1);
expect(uxStub.firstCall.args[0]).to.equal(
'PackageUploadRequest has been enqueued. You can query the status using\n' +
'sfdx force:package1:version:create:get -i undefined -u test@user.com'
);
});

it('should print ERROR status correctly, undefined errors', async () => {
try {
await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'ERROR');
assert.fail('the above should throw an erorr, from the ERROR status');
} catch (e) {
expect((e as Error).message).to.equal(
'Package upload failed. \nPackage version creation failed with unknown error'
);
}
});

it('should print ERROR status correctly, multiple errors', async () => {
try {
await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'ERROR', { errors: [new Error('message1')] });
assert.fail('the above should throw an erorr, from the ERROR status');
} catch (e) {
expect((e as Error).message).to.equal('Package upload failed. \nmessage1');
}
});

it('should print ERROR status correctly, multiple errors (2+)', async () => {
try {
await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'ERROR', {
errors: [new Error('message1'), new Error('message2')],
});
assert.fail('the above should throw an erorr, from the ERROR status');
} catch (e) {
expect((e as Error).message).to.equal('Package upload failed. \nmessage1\nmessage2');
}
});
});
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"extends": "@salesforce/dev-config/tsconfig",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
"rootDir": "src",
"baseUrl": ".",
"paths": {
"jsforce": ["node_modules/jsforce"],
"@salesforce/core": ["node_modules/@salesforce/core"]
}
},
"include": ["./src/**/*.ts"]
}
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1245,12 +1245,7 @@
resolved "https://registry.yarnpkg.com/@salesforce/prettier-config/-/prettier-config-0.0.2.tgz#ded39bf7cb75238edc9db6dd093649111350f8bc"
integrity sha512-KExM355BLbxCW6siGBV7oUOotXvvVp0tAWERgzUkM2FcMb9fWrjwXDrIHc8V0UdDlA3UXtFltDWgN+Yqi+BA/g==

"@salesforce/schemas@^1.0.1", "@salesforce/schemas@^1.1.0":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@salesforce/schemas/-/schemas-1.1.3.tgz#fce83f55c7557d47b9c814d5d02978ad734300b3"
integrity sha512-XWohlOT2oQDqAJH00OXS3f2MGjkwZ6pr4emnnkHSQbg7UdGW0rvGpEnRKqBbDUfZ4K5YKSo9Gj216ZtaP3JLXg==

"@salesforce/schemas@^1.2.0":
"@salesforce/schemas@^1.0.1", "@salesforce/schemas@^1.1.0", "@salesforce/schemas@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@salesforce/schemas/-/schemas-1.2.0.tgz#3c7ed492e3ee5d9d9fb24a32b5c574893f6648db"
integrity sha512-76oYf/9Rsn6Yl+awrTQvLaQuRDNX7F3X9ksRiw53OCJdydIF05buX6XLzN0WDWpkCg/asw+lZMuAzbDVS0tBmg==
Expand Down

0 comments on commit 9d3f4f5

Please sign in to comment.