|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { Org } from '@salesforce/core'; |
| 8 | +import { testSetup } from '@salesforce/core/lib/testSetup'; |
| 9 | +import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon'; |
| 10 | +import { Config } from '@oclif/core'; |
| 11 | +import { assert, expect } from 'chai'; |
| 12 | +import { Package1VersionCreateGetCommand } from '../../../../src/commands/force/package1/beta/version/create/get'; |
| 13 | + |
| 14 | +const $$ = testSetup(); |
| 15 | +const oclifConfigStub = fromStub(stubInterface<Config>($$.SANDBOX)); |
| 16 | +let uxStub: sinon.SinonStub; |
| 17 | + |
| 18 | +class TestCommand extends Package1VersionCreateGetCommand { |
| 19 | + public async runIt() { |
| 20 | + await this.init(); |
| 21 | + |
| 22 | + uxStub = stubMethod($$.SANDBOX, this.ux, 'log'); |
| 23 | + return this.run(); |
| 24 | + } |
| 25 | + public setOrg(org: Org) { |
| 26 | + this.org = org; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +const runCmd = async (params: string[], result: string, errors?: { errors: Error[] }) => { |
| 31 | + const cmd = new TestCommand(params, oclifConfigStub); |
| 32 | + |
| 33 | + stubMethod($$.SANDBOX, cmd, 'assignOrg').callsFake(() => { |
| 34 | + const orgStub = fromStub( |
| 35 | + stubInterface<Org>($$.SANDBOX, { |
| 36 | + getUsername: () => '[email protected]', |
| 37 | + getConnection: () => { |
| 38 | + return { |
| 39 | + tooling: { |
| 40 | + sobject: () => { |
| 41 | + return { |
| 42 | + retrieve: () => { |
| 43 | + return Promise.resolve({ |
| 44 | + Status: result, |
| 45 | + MetadataPackageVersionId: '04t4p000002BavTXXX', |
| 46 | + Errors: errors, |
| 47 | + }); |
| 48 | + }, |
| 49 | + }; |
| 50 | + }, |
| 51 | + }, |
| 52 | + }; |
| 53 | + }, |
| 54 | + }) |
| 55 | + ); |
| 56 | + cmd.setOrg(orgStub); |
| 57 | + }); |
| 58 | + return cmd.runIt(); |
| 59 | +}; |
| 60 | + |
| 61 | +describe('force:package1:version:create:get', () => { |
| 62 | + afterEach(() => { |
| 63 | + $$.SANDBOX.restore(); |
| 64 | + }); |
| 65 | + it('should print SUCCESS status correctly', async () => { |
| 66 | + const result = await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'SUCCESS'); |
| 67 | + expect(result.Status).to.equal('SUCCESS'); |
| 68 | + expect(uxStub.callCount).to.equal(1); |
| 69 | + expect(uxStub.firstCall.args[0]).to.equal('Successfully uploaded package [04t4p000002BavTXXX]'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should print IN_PROGRESS status correctly', async () => { |
| 73 | + const result = await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'IN_PROGRESS'); |
| 74 | + expect(result.Status).to.equal('IN_PROGRESS'); |
| 75 | + expect(uxStub.callCount).to.equal(1); |
| 76 | + expect(uxStub.firstCall.args[0]).to.equal( |
| 77 | + 'PackageUploadRequest is still InProgress. You can query the status using\n' + |
| 78 | + 'sfdx force:package1:version:create:get -i undefined -u [email protected]' |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should print QUEUED status correctly', async () => { |
| 83 | + const result = await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'QUEUED'); |
| 84 | + expect(result.Status).to.equal('QUEUED'); |
| 85 | + expect(uxStub.callCount).to.equal(1); |
| 86 | + expect(uxStub.firstCall.args[0]).to.equal( |
| 87 | + 'PackageUploadRequest has been enqueued. You can query the status using\n' + |
| 88 | + 'sfdx force:package1:version:create:get -i undefined -u [email protected]' |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should print ERROR status correctly, undefined errors', async () => { |
| 93 | + try { |
| 94 | + await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'ERROR'); |
| 95 | + assert.fail('the above should throw an erorr, from the ERROR status'); |
| 96 | + } catch (e) { |
| 97 | + expect((e as Error).message).to.equal( |
| 98 | + 'Package upload failed. \nPackage version creation failed with unknown error' |
| 99 | + ); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('should print ERROR status correctly, multiple errors', async () => { |
| 104 | + try { |
| 105 | + await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'ERROR', { errors: [new Error('message1')] }); |
| 106 | + assert.fail('the above should throw an erorr, from the ERROR status'); |
| 107 | + } catch (e) { |
| 108 | + expect((e as Error).message).to.equal('Package upload failed. \nmessage1'); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it('should print ERROR status correctly, multiple errors (2+)', async () => { |
| 113 | + try { |
| 114 | + await runCmd(['--requestid', '0HD4p000000blSkXXX'], 'ERROR', { |
| 115 | + errors: [new Error('message1'), new Error('message2')], |
| 116 | + }); |
| 117 | + assert.fail('the above should throw an erorr, from the ERROR status'); |
| 118 | + } catch (e) { |
| 119 | + expect((e as Error).message).to.equal('Package upload failed. \nmessage1\nmessage2'); |
| 120 | + } |
| 121 | + }); |
| 122 | +}); |
0 commit comments