|
| 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 | + |
| 8 | +import { join, resolve } from 'path'; |
| 9 | +import { Dictionary } from '@salesforce/ts-types'; |
| 10 | +import { DeployResult, MetadataConverter } from '@salesforce/source-deploy-retrieve'; |
| 11 | +import * as sinon from 'sinon'; |
| 12 | +import { expect } from 'chai'; |
| 13 | +import { Convert } from '../../../src/commands/force/source/convert'; |
| 14 | +import { FlagOptions } from '../../../src/sourceCommand'; |
| 15 | + |
| 16 | +describe('force:source:convert', () => { |
| 17 | + let createComponentSetStub: sinon.SinonStub; |
| 18 | + let deployStub: sinon.SinonStub; |
| 19 | + |
| 20 | + const defaultDir = join('my', 'default', 'package'); |
| 21 | + const myApp = join('new', 'package', 'directory'); |
| 22 | + |
| 23 | + const sandbox = sinon.createSandbox(); |
| 24 | + const packageXml = 'package.xml'; |
| 25 | + |
| 26 | + const run = async (flags: Dictionary<boolean | string | number | string[]> = {}): Promise<DeployResult> => { |
| 27 | + // Run the command |
| 28 | + return Convert.prototype.run.call({ |
| 29 | + flags: Object.assign({}, flags), |
| 30 | + ux: { |
| 31 | + log: () => {}, |
| 32 | + styledHeader: () => {}, |
| 33 | + table: () => {}, |
| 34 | + }, |
| 35 | + logger: { |
| 36 | + debug: () => {}, |
| 37 | + }, |
| 38 | + project: { |
| 39 | + getDefaultPackage: () => { |
| 40 | + return { path: defaultDir }; |
| 41 | + }, |
| 42 | + }, |
| 43 | + createComponentSet: createComponentSetStub, |
| 44 | + }) as Promise<DeployResult>; |
| 45 | + }; |
| 46 | + |
| 47 | + // Ensure SourceCommand.createComponentSet() args |
| 48 | + const ensureCreateComponentSetArgs = (overrides?: Partial<FlagOptions>) => { |
| 49 | + const defaultArgs = { |
| 50 | + sourcepath: [], |
| 51 | + manifest: undefined, |
| 52 | + metadata: undefined, |
| 53 | + }; |
| 54 | + const expectedArgs = { ...defaultArgs, ...overrides }; |
| 55 | + |
| 56 | + expect(createComponentSetStub.calledOnce).to.equal(true); |
| 57 | + expect(createComponentSetStub.firstCall.args[0]).to.deep.equal(expectedArgs); |
| 58 | + }; |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + sandbox.stub(MetadataConverter.prototype, 'convert').resolves({ packagePath: 'temp' }); |
| 62 | + createComponentSetStub = sandbox.stub().returns({ |
| 63 | + deploy: deployStub, |
| 64 | + getPackageXml: () => packageXml, |
| 65 | + getSourceComponents: () => { |
| 66 | + return { |
| 67 | + toArray: () => {}, |
| 68 | + }; |
| 69 | + }, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + sandbox.restore(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should pass along sourcepath', async () => { |
| 78 | + const sourcepath = ['somepath']; |
| 79 | + const result = await run({ sourcepath, json: true }); |
| 80 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 81 | + ensureCreateComponentSetArgs({ sourcepath }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call default package dir if no args', async () => { |
| 85 | + const result = await run({ json: true }); |
| 86 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 87 | + ensureCreateComponentSetArgs({ sourcepath: [defaultDir] }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should call with metadata', async () => { |
| 91 | + const result = await run({ metadata: ['ApexClass'], json: true }); |
| 92 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 93 | + ensureCreateComponentSetArgs({ metadata: ['ApexClass'] }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call with package.xml', async () => { |
| 97 | + const result = await run({ json: true }); |
| 98 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 99 | + ensureCreateComponentSetArgs({ sourcepath: [defaultDir] }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should call default package dir if no args', async () => { |
| 103 | + const result = await run({ json: true }); |
| 104 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 105 | + ensureCreateComponentSetArgs({ sourcepath: [defaultDir] }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should call root dir with rootdir flag', async () => { |
| 109 | + const result = await run({ rootdir: myApp, json: true }); |
| 110 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 111 | + ensureCreateComponentSetArgs({ sourcepath: [myApp] }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('rootdir should be overwritten by any other flag', () => { |
| 115 | + it('sourcepath', async () => { |
| 116 | + const result = await run({ rootdir: myApp, sourcepath: [defaultDir], json: true }); |
| 117 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 118 | + ensureCreateComponentSetArgs({ sourcepath: [defaultDir] }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('metadata', async () => { |
| 122 | + const result = await run({ rootdir: myApp, metadata: ['ApexClass', 'CustomObject'], json: true }); |
| 123 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 124 | + ensureCreateComponentSetArgs({ metadata: ['ApexClass', 'CustomObject'] }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('package', async () => { |
| 128 | + const result = await run({ rootdir: myApp, manifest: packageXml, json: true }); |
| 129 | + expect(result).to.deep.equal({ location: resolve('temp') }); |
| 130 | + ensureCreateComponentSetArgs({ manifest: packageXml }); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments