diff --git a/src/commands/force/source/deploy.ts b/src/commands/force/source/deploy.ts index f43907a41..121690676 100644 --- a/src/commands/force/source/deploy.ts +++ b/src/commands/force/source/deploy.ts @@ -8,7 +8,7 @@ import * as os from 'os'; import * as path from 'path'; import { flags, FlagsConfig } from '@salesforce/command'; -import { Lifecycle, Messages } from '@salesforce/core'; +import { Messages } from '@salesforce/core'; import { DeployResult, MetadataApiDeploy } from '@salesforce/source-deploy-retrieve'; import { Duration } from '@salesforce/kit'; import { asString, asArray, getBoolean } from '@salesforce/ts-types'; @@ -106,8 +106,6 @@ export class Deploy extends SourceCommand { }); return this.deployReport(id); } - - const hookEmitter = Lifecycle.getInstance(); const cs = await this.createComponentSet({ sourcepath: asArray(this.flags.sourcepath), manifest: asString(this.flags.manifest), @@ -115,7 +113,7 @@ export class Deploy extends SourceCommand { apiversion: asString(this.flags.apiversion), }); - await hookEmitter.emit('predeploy', { packageXmlPath: cs.getPackageXml() }); + await this.lifecycle.emit('predeploy', cs.toArray()); const deploy = cs.deploy({ usernameOrConnection: this.org.getUsername(), @@ -136,7 +134,7 @@ export class Deploy extends SourceCommand { const results = await deploy.start(); - await hookEmitter.emit('postdeploy', results); + await this.lifecycle.emit('postdeploy', results); const file = this.getConfig(); await file.write({ [SourceCommand.STASH_KEY]: { jobid: results.response.id } }); diff --git a/src/commands/force/source/retrieve.ts b/src/commands/force/source/retrieve.ts index dcf543db7..6d002828a 100644 --- a/src/commands/force/source/retrieve.ts +++ b/src/commands/force/source/retrieve.ts @@ -7,7 +7,7 @@ import * as os from 'os'; import { flags, FlagsConfig } from '@salesforce/command'; -import { Lifecycle, Messages, SfdxError } from '@salesforce/core'; +import { Messages, SfdxError } from '@salesforce/core'; import { Duration } from '@salesforce/kit'; import { asArray, asString } from '@salesforce/ts-types'; import { blue } from 'chalk'; @@ -53,8 +53,6 @@ export class Retrieve extends SourceCommand { protected readonly lifecycleEventNames = ['preretrieve', 'postretrieve']; public async run(): Promise { - const hookEmitter = Lifecycle.getInstance(); - const defaultPackagePath = this.project.getDefaultPackage().fullPath; const cs = await this.createComponentSet({ @@ -65,8 +63,7 @@ export class Retrieve extends SourceCommand { apiversion: asString(this.flags.apiversion), }); - // Emit the preretrieve event, which needs the package.xml from the ComponentSet - await hookEmitter.emit('preretrieve', { packageXmlPath: cs.getPackageXml() }); + await this.lifecycle.emit('preretrieve', cs.toArray()); const mdapiResult = await cs .retrieve({ @@ -78,7 +75,7 @@ export class Retrieve extends SourceCommand { .start(); const results = mdapiResult.response; - await hookEmitter.emit('postretrieve', results); + await this.lifecycle.emit('postretrieve', results); if (results.status === 'InProgress') { throw new SfdxError(messages.getMessage('retrieveTimeout', [(this.flags.wait as Duration).minutes])); diff --git a/src/sourceCommand.ts b/src/sourceCommand.ts index 45de6f0b5..6fffa3b11 100644 --- a/src/sourceCommand.ts +++ b/src/sourceCommand.ts @@ -7,7 +7,7 @@ import * as path from 'path'; import { SfdxCommand } from '@salesforce/command'; import { ComponentSet, DeployResult } from '@salesforce/source-deploy-retrieve'; -import { fs, SfdxError, Logger, ConfigFile } from '@salesforce/core'; +import { fs, SfdxError, Logger, ConfigFile, Lifecycle } from '@salesforce/core'; import { ComponentLike } from '@salesforce/source-deploy-retrieve/lib/src/resolve'; import cli from 'cli-ux'; import { asString } from '@salesforce/ts-types'; @@ -34,6 +34,7 @@ export abstract class SourceCommand extends SfdxCommand { public static STASH_KEY = 'SOURCE_DEPLOY'; public progressBar?: ProgressBar; public logger = Logger.childFromRoot(this.constructor.name); + public lifecycle = Lifecycle.getInstance(); public getConfig(): ConfigFile<{ isGlobal: true; filename: 'stash.json' }> { return new ConfigFile({ isGlobal: true, filename: 'stash.json' }); diff --git a/test/commands/source/cancel.test.ts b/test/commands/source/cancel.test.ts index ff82b7f12..88d92da3b 100644 --- a/test/commands/source/cancel.test.ts +++ b/test/commands/source/cancel.test.ts @@ -10,7 +10,7 @@ import { DeployResult } from '@salesforce/source-deploy-retrieve'; import * as sinon from 'sinon'; import { expect } from 'chai'; import { Cancel } from '../../../src/commands/force/source/deploy/cancel'; -import { deployReport } from './deployReport'; +import { exampleDeployResponse } from './testConsts'; describe('force:source:cancel', () => { const jobid = '0Af1k00000r2BebCAE'; @@ -28,7 +28,7 @@ describe('force:source:cancel', () => { getConfig: () => { return { readSync: () => {}, get: () => jobid }; }, - deployReport: () => deployReport, + deployReport: () => exampleDeployResponse, org: { getConnection: () => { return { @@ -51,12 +51,12 @@ describe('force:source:cancel', () => { it('should read from ~/.sfdx/stash.json', async () => { const result = await run({ json: true }); - expect(result).to.deep.equal(deployReport); + expect(result).to.deep.equal(exampleDeployResponse); }); it('should use the jobid flag', async () => { const jobIdFlag = '0Af1k00000r29C9CAI'; const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag); - expect(result).to.deep.equal(deployReport); + expect(result).to.deep.equal(exampleDeployResponse); }); }); diff --git a/test/commands/source/deploy.test.ts b/test/commands/source/deploy.test.ts index 4eb6767f5..a5d7a2d98 100644 --- a/test/commands/source/deploy.test.ts +++ b/test/commands/source/deploy.test.ts @@ -12,11 +12,11 @@ import { Dictionary } from '@salesforce/ts-types'; import { Lifecycle } from '@salesforce/core'; import { Deploy } from '../../../src/commands/force/source/deploy'; import { FlagOptions } from '../../../src/sourceCommand'; +import { exampleSourceComponent } from './testConsts'; describe('force:source:deploy', () => { const sandbox = sinon.createSandbox(); const username = 'deploy-test@org.com'; - const packageXml = 'package.xml'; // TODO: When output work items have been done we can test result output // that more closely matches actual output. @@ -50,6 +50,9 @@ describe('force:source:deploy', () => { }, initProgressBar: () => {}, progress: progressStub, + lifecycle: { + emit: lifecycleEmitStub, + }, print: () => {}, }) as Promise; }; @@ -60,7 +63,9 @@ describe('force:source:deploy', () => { progressStub = sandbox.stub(); createComponentSetStub = sandbox.stub().returns({ deploy: deployStub, - getPackageXml: () => packageXml, + toArray: () => { + return [exampleSourceComponent]; + }, }); lifecycleEmitStub = sandbox.stub(Lifecycle.prototype, 'emit'); }); @@ -108,9 +113,7 @@ describe('force:source:deploy', () => { const failureMsg = 'Lifecycle.emit() should be called for predeploy and postdeploy'; expect(lifecycleEmitStub.calledTwice, failureMsg).to.equal(true); expect(lifecycleEmitStub.firstCall.args[0]).to.equal('predeploy'); - expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal({ - packageXmlPath: packageXml, - }); + expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal([exampleSourceComponent]); expect(lifecycleEmitStub.secondCall.args[0]).to.equal('postdeploy'); expect(lifecycleEmitStub.secondCall.args[1]).to.deep.equal(stubbedResults); }; diff --git a/test/commands/source/report.test.ts b/test/commands/source/report.test.ts index 9d1b2ba1f..04bf862a4 100644 --- a/test/commands/source/report.test.ts +++ b/test/commands/source/report.test.ts @@ -10,7 +10,7 @@ import { DeployResult } from '@salesforce/source-deploy-retrieve'; import * as sinon from 'sinon'; import { expect } from 'chai'; import { Report } from '../../../src/commands/force/source/deploy/report'; -import { deployReport } from './deployReport'; +import { exampleDeployResponse } from './testConsts'; describe('force:source:report', () => { const jobid = '0Af1k00000r2BebCAE'; @@ -31,7 +31,7 @@ describe('force:source:report', () => { ux: { log: () => {}, }, - deployReport: () => deployReport, + deployReport: () => exampleDeployResponse, org: { getConnection: () => { return { @@ -54,12 +54,12 @@ describe('force:source:report', () => { it('should read from ~/.sfdx/stash.json', async () => { const result = await run({ json: true }); - expect(result).to.deep.equal(deployReport); + expect(result).to.deep.equal(exampleDeployResponse); }); it('should use the jobid flag', async () => { const jobIdFlag = '0Af1k00000r29C9CAI'; const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag); - expect(result).to.deep.equal(deployReport); + expect(result).to.deep.equal(exampleDeployResponse); }); }); diff --git a/test/commands/source/retrieve.test.ts b/test/commands/source/retrieve.test.ts index 9e9d6efc5..a6b024b77 100644 --- a/test/commands/source/retrieve.test.ts +++ b/test/commands/source/retrieve.test.ts @@ -12,6 +12,7 @@ import { Dictionary } from '@salesforce/ts-types'; import { Lifecycle } from '@salesforce/core'; import { Retrieve } from '../../../src/commands/force/source/retrieve'; import { FlagOptions } from '../../../src/sourceCommand'; +import { exampleSourceComponent } from './testConsts'; describe('force:source:retrieve', () => { const sandbox = sinon.createSandbox(); @@ -38,7 +39,6 @@ describe('force:source:retrieve', () => { flags: Object.assign({}, flags), ux: { log: () => {}, - logJson: () => {}, styledHeader: () => {}, table: () => {}, }, @@ -48,6 +48,9 @@ describe('force:source:retrieve', () => { org: { getUsername: () => username, }, + lifecycle: { + emit: lifecycleEmitStub, + }, project: { getDefaultPackage: () => ({ fullPath: defaultPackagePath }), }, @@ -60,6 +63,9 @@ describe('force:source:retrieve', () => { retrieveStub = sandbox.stub().returns({ start: startStub }); createComponentSetStub = sandbox.stub().returns({ retrieve: retrieveStub, + toArray: () => { + return [exampleSourceComponent]; + }, getPackageXml: () => packageXml, }); lifecycleEmitStub = sandbox.stub(Lifecycle.prototype, 'emit'); @@ -103,9 +109,7 @@ describe('force:source:retrieve', () => { const failureMsg = 'Lifecycle.emit() should be called for preretrieve and postretrieve'; expect(lifecycleEmitStub.calledTwice, failureMsg).to.equal(true); expect(lifecycleEmitStub.firstCall.args[0]).to.equal('preretrieve'); - expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal({ - packageXmlPath: packageXml, - }); + expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal([exampleSourceComponent]); expect(lifecycleEmitStub.secondCall.args[0]).to.equal('postretrieve'); expect(lifecycleEmitStub.secondCall.args[1]).to.deep.equal(stubbedResults.response); }; diff --git a/test/commands/source/sourceCommand.test.ts b/test/commands/source/sourceCommand.test.ts index f42e305a4..5d54be2d4 100644 --- a/test/commands/source/sourceCommand.test.ts +++ b/test/commands/source/sourceCommand.test.ts @@ -15,7 +15,7 @@ import { stubMethod } from '@salesforce/ts-sinon'; import { fs as fsCore, SfdxError, SfdxProject } from '@salesforce/core'; import cli from 'cli-ux'; import { FlagOptions, ProgressBar, SourceCommand } from '../../../src/sourceCommand'; -import { deployReport } from './deployReport'; +import { exampleDeployResponse } from './testConsts'; describe('SourceCommand', () => { const sandbox = sinon.createSandbox(); @@ -326,7 +326,7 @@ describe('SourceCommand', () => { getConnection: () => { return { metadata: { - checkDeployStatus: () => deployReport, + checkDeployStatus: () => exampleDeployResponse, }, }; }, @@ -349,7 +349,7 @@ describe('SourceCommand', () => { it('should "print" the progress bar', async () => { const res = await command.callDeployProgress('0Af1h00000fCQgsCAG'); - expect(res).to.deep.equal(deployReport); + expect(res).to.deep.equal(exampleDeployResponse); expect(initProgressBarStub.called).to.be.true; expect(pbStart.callCount).to.equal(1); expect(pbStop.callCount).to.equal(1); @@ -362,7 +362,7 @@ describe('SourceCommand', () => { expect(initProgressBarStub.called).to.be.false; const res = await command.callDeployProgress('0Af1h00000fCQgsCAG'); - expect(res).to.deep.equal(deployReport); + expect(res).to.deep.equal(exampleDeployResponse); }); it('should NOT "print" the progress bar because of env var', async () => { @@ -371,7 +371,7 @@ describe('SourceCommand', () => { const res = await command.callDeployProgress('0Af1h00000fCQgsCAG'); expect(initProgressBarStub.called).to.be.false; - expect(res).to.deep.equal(deployReport); + expect(res).to.deep.equal(exampleDeployResponse); } finally { delete process.env.SFDX_USE_PROGRESS_BAR; } diff --git a/test/commands/source/deployReport.ts b/test/commands/source/testConsts.ts similarity index 95% rename from test/commands/source/deployReport.ts rename to test/commands/source/testConsts.ts index df1328b42..0c962b520 100644 --- a/test/commands/source/deployReport.ts +++ b/test/commands/source/testConsts.ts @@ -4,7 +4,27 @@ * 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 */ -export const deployReport = { + +export const exampleSourceComponent = { + name: 'GeocodingService', + type: { + id: 'apexclass', + name: 'ApexClass', + suffix: 'cls', + directoryName: 'classes', + inFolder: false, + strictDirectoryName: false, + strategies: { + adapter: 'matchingContentFile', + }, + }, + xml: + '/Users/william.ruemmele/projects/scratches/dreamhouse-lwc/force-app/main/default/classes/GeocodingService.cls-meta.xml', + content: + '/Users/william.ruemmele/projects/scratches/dreamhouse-lwc/force-app/main/default/classes/GeocodingService.cls', +}; + +export const exampleDeployResponse = { result: { canceledBy: '0051h000006BHOq', canceledByName: 'User User',