Skip to content

Commit 81e1058

Browse files
fix: new hooks, updated tests (#69)
* fix: new hooks, updated tests * chore: remove extra comments, refactor test const names
1 parent 2ed3da2 commit 81e1058

File tree

9 files changed

+58
-35
lines changed

9 files changed

+58
-35
lines changed

src/commands/force/source/deploy.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as os from 'os';
99
import * as path from 'path';
1010
import { flags, FlagsConfig } from '@salesforce/command';
11-
import { Lifecycle, Messages } from '@salesforce/core';
11+
import { Messages } from '@salesforce/core';
1212
import { DeployResult, MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
1313
import { Duration } from '@salesforce/kit';
1414
import { asString, asArray, getBoolean } from '@salesforce/ts-types';
@@ -106,16 +106,14 @@ export class Deploy extends SourceCommand {
106106
});
107107
return this.deployReport(id);
108108
}
109-
110-
const hookEmitter = Lifecycle.getInstance();
111109
const cs = await this.createComponentSet({
112110
sourcepath: asArray<string>(this.flags.sourcepath),
113111
manifest: asString(this.flags.manifest),
114112
metadata: asArray<string>(this.flags.metadata),
115113
apiversion: asString(this.flags.apiversion),
116114
});
117115

118-
await hookEmitter.emit('predeploy', { packageXmlPath: cs.getPackageXml() });
116+
await this.lifecycle.emit('predeploy', cs.toArray());
119117

120118
const deploy = cs.deploy({
121119
usernameOrConnection: this.org.getUsername(),
@@ -136,7 +134,7 @@ export class Deploy extends SourceCommand {
136134

137135
const results = await deploy.start();
138136

139-
await hookEmitter.emit('postdeploy', results);
137+
await this.lifecycle.emit('postdeploy', results);
140138

141139
const file = this.getConfig();
142140
await file.write({ [SourceCommand.STASH_KEY]: { jobid: results.response.id } });

src/commands/force/source/retrieve.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import * as os from 'os';
88

99
import { flags, FlagsConfig } from '@salesforce/command';
10-
import { Lifecycle, Messages, SfdxError } from '@salesforce/core';
10+
import { Messages, SfdxError } from '@salesforce/core';
1111
import { Duration } from '@salesforce/kit';
1212
import { asArray, asString } from '@salesforce/ts-types';
1313
import { blue } from 'chalk';
@@ -53,8 +53,6 @@ export class Retrieve extends SourceCommand {
5353
protected readonly lifecycleEventNames = ['preretrieve', 'postretrieve'];
5454

5555
public async run(): Promise<FileResponse[]> {
56-
const hookEmitter = Lifecycle.getInstance();
57-
5856
const defaultPackagePath = this.project.getDefaultPackage().fullPath;
5957

6058
const cs = await this.createComponentSet({
@@ -65,8 +63,7 @@ export class Retrieve extends SourceCommand {
6563
apiversion: asString(this.flags.apiversion),
6664
});
6765

68-
// Emit the preretrieve event, which needs the package.xml from the ComponentSet
69-
await hookEmitter.emit('preretrieve', { packageXmlPath: cs.getPackageXml() });
66+
await this.lifecycle.emit('preretrieve', cs.toArray());
7067

7168
const mdapiResult = await cs
7269
.retrieve({
@@ -78,7 +75,7 @@ export class Retrieve extends SourceCommand {
7875
.start();
7976

8077
const results = mdapiResult.response;
81-
await hookEmitter.emit('postretrieve', results);
78+
await this.lifecycle.emit('postretrieve', results);
8279

8380
if (results.status === 'InProgress') {
8481
throw new SfdxError(messages.getMessage('retrieveTimeout', [(this.flags.wait as Duration).minutes]));

src/sourceCommand.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import * as path from 'path';
88
import { SfdxCommand } from '@salesforce/command';
99
import { ComponentSet, DeployResult } from '@salesforce/source-deploy-retrieve';
10-
import { fs, SfdxError, Logger, ConfigFile } from '@salesforce/core';
10+
import { fs, SfdxError, Logger, ConfigFile, Lifecycle } from '@salesforce/core';
1111
import { ComponentLike } from '@salesforce/source-deploy-retrieve/lib/src/resolve';
1212
import cli from 'cli-ux';
1313
import { asString } from '@salesforce/ts-types';
@@ -34,6 +34,7 @@ export abstract class SourceCommand extends SfdxCommand {
3434
public static STASH_KEY = 'SOURCE_DEPLOY';
3535
public progressBar?: ProgressBar;
3636
public logger = Logger.childFromRoot(this.constructor.name);
37+
public lifecycle = Lifecycle.getInstance();
3738

3839
public getConfig(): ConfigFile<{ isGlobal: true; filename: 'stash.json' }> {
3940
return new ConfigFile({ isGlobal: true, filename: 'stash.json' });

test/commands/source/cancel.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DeployResult } from '@salesforce/source-deploy-retrieve';
1010
import * as sinon from 'sinon';
1111
import { expect } from 'chai';
1212
import { Cancel } from '../../../src/commands/force/source/deploy/cancel';
13-
import { deployReport } from './deployReport';
13+
import { exampleDeployResponse } from './testConsts';
1414

1515
describe('force:source:cancel', () => {
1616
const jobid = '0Af1k00000r2BebCAE';
@@ -28,7 +28,7 @@ describe('force:source:cancel', () => {
2828
getConfig: () => {
2929
return { readSync: () => {}, get: () => jobid };
3030
},
31-
deployReport: () => deployReport,
31+
deployReport: () => exampleDeployResponse,
3232
org: {
3333
getConnection: () => {
3434
return {
@@ -51,12 +51,12 @@ describe('force:source:cancel', () => {
5151

5252
it('should read from ~/.sfdx/stash.json', async () => {
5353
const result = await run({ json: true });
54-
expect(result).to.deep.equal(deployReport);
54+
expect(result).to.deep.equal(exampleDeployResponse);
5555
});
5656

5757
it('should use the jobid flag', async () => {
5858
const jobIdFlag = '0Af1k00000r29C9CAI';
5959
const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag);
60-
expect(result).to.deep.equal(deployReport);
60+
expect(result).to.deep.equal(exampleDeployResponse);
6161
});
6262
});

test/commands/source/deploy.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { Dictionary } from '@salesforce/ts-types';
1212
import { Lifecycle } from '@salesforce/core';
1313
import { Deploy } from '../../../src/commands/force/source/deploy';
1414
import { FlagOptions } from '../../../src/sourceCommand';
15+
import { exampleSourceComponent } from './testConsts';
1516

1617
describe('force:source:deploy', () => {
1718
const sandbox = sinon.createSandbox();
1819
const username = 'deploy-test@org.com';
19-
const packageXml = 'package.xml';
2020

2121
// TODO: When output work items have been done we can test result output
2222
// that more closely matches actual output.
@@ -50,6 +50,9 @@ describe('force:source:deploy', () => {
5050
},
5151
initProgressBar: () => {},
5252
progress: progressStub,
53+
lifecycle: {
54+
emit: lifecycleEmitStub,
55+
},
5356
print: () => {},
5457
}) as Promise<DeployResult>;
5558
};
@@ -60,7 +63,9 @@ describe('force:source:deploy', () => {
6063
progressStub = sandbox.stub();
6164
createComponentSetStub = sandbox.stub().returns({
6265
deploy: deployStub,
63-
getPackageXml: () => packageXml,
66+
toArray: () => {
67+
return [exampleSourceComponent];
68+
},
6469
});
6570
lifecycleEmitStub = sandbox.stub(Lifecycle.prototype, 'emit');
6671
});
@@ -108,9 +113,7 @@ describe('force:source:deploy', () => {
108113
const failureMsg = 'Lifecycle.emit() should be called for predeploy and postdeploy';
109114
expect(lifecycleEmitStub.calledTwice, failureMsg).to.equal(true);
110115
expect(lifecycleEmitStub.firstCall.args[0]).to.equal('predeploy');
111-
expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal({
112-
packageXmlPath: packageXml,
113-
});
116+
expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal([exampleSourceComponent]);
114117
expect(lifecycleEmitStub.secondCall.args[0]).to.equal('postdeploy');
115118
expect(lifecycleEmitStub.secondCall.args[1]).to.deep.equal(stubbedResults);
116119
};

test/commands/source/report.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DeployResult } from '@salesforce/source-deploy-retrieve';
1010
import * as sinon from 'sinon';
1111
import { expect } from 'chai';
1212
import { Report } from '../../../src/commands/force/source/deploy/report';
13-
import { deployReport } from './deployReport';
13+
import { exampleDeployResponse } from './testConsts';
1414

1515
describe('force:source:report', () => {
1616
const jobid = '0Af1k00000r2BebCAE';
@@ -31,7 +31,7 @@ describe('force:source:report', () => {
3131
ux: {
3232
log: () => {},
3333
},
34-
deployReport: () => deployReport,
34+
deployReport: () => exampleDeployResponse,
3535
org: {
3636
getConnection: () => {
3737
return {
@@ -54,12 +54,12 @@ describe('force:source:report', () => {
5454

5555
it('should read from ~/.sfdx/stash.json', async () => {
5656
const result = await run({ json: true });
57-
expect(result).to.deep.equal(deployReport);
57+
expect(result).to.deep.equal(exampleDeployResponse);
5858
});
5959

6060
it('should use the jobid flag', async () => {
6161
const jobIdFlag = '0Af1k00000r29C9CAI';
6262
const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag);
63-
expect(result).to.deep.equal(deployReport);
63+
expect(result).to.deep.equal(exampleDeployResponse);
6464
});
6565
});

test/commands/source/retrieve.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Dictionary } from '@salesforce/ts-types';
1212
import { Lifecycle } from '@salesforce/core';
1313
import { Retrieve } from '../../../src/commands/force/source/retrieve';
1414
import { FlagOptions } from '../../../src/sourceCommand';
15+
import { exampleSourceComponent } from './testConsts';
1516

1617
describe('force:source:retrieve', () => {
1718
const sandbox = sinon.createSandbox();
@@ -38,7 +39,6 @@ describe('force:source:retrieve', () => {
3839
flags: Object.assign({}, flags),
3940
ux: {
4041
log: () => {},
41-
logJson: () => {},
4242
styledHeader: () => {},
4343
table: () => {},
4444
},
@@ -48,6 +48,9 @@ describe('force:source:retrieve', () => {
4848
org: {
4949
getUsername: () => username,
5050
},
51+
lifecycle: {
52+
emit: lifecycleEmitStub,
53+
},
5154
project: {
5255
getDefaultPackage: () => ({ fullPath: defaultPackagePath }),
5356
},
@@ -60,6 +63,9 @@ describe('force:source:retrieve', () => {
6063
retrieveStub = sandbox.stub().returns({ start: startStub });
6164
createComponentSetStub = sandbox.stub().returns({
6265
retrieve: retrieveStub,
66+
toArray: () => {
67+
return [exampleSourceComponent];
68+
},
6369
getPackageXml: () => packageXml,
6470
});
6571
lifecycleEmitStub = sandbox.stub(Lifecycle.prototype, 'emit');
@@ -103,9 +109,7 @@ describe('force:source:retrieve', () => {
103109
const failureMsg = 'Lifecycle.emit() should be called for preretrieve and postretrieve';
104110
expect(lifecycleEmitStub.calledTwice, failureMsg).to.equal(true);
105111
expect(lifecycleEmitStub.firstCall.args[0]).to.equal('preretrieve');
106-
expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal({
107-
packageXmlPath: packageXml,
108-
});
112+
expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal([exampleSourceComponent]);
109113
expect(lifecycleEmitStub.secondCall.args[0]).to.equal('postretrieve');
110114
expect(lifecycleEmitStub.secondCall.args[1]).to.deep.equal(stubbedResults.response);
111115
};

test/commands/source/sourceCommand.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { stubMethod } from '@salesforce/ts-sinon';
1515
import { fs as fsCore, SfdxError, SfdxProject } from '@salesforce/core';
1616
import cli from 'cli-ux';
1717
import { FlagOptions, ProgressBar, SourceCommand } from '../../../src/sourceCommand';
18-
import { deployReport } from './deployReport';
18+
import { exampleDeployResponse } from './testConsts';
1919

2020
describe('SourceCommand', () => {
2121
const sandbox = sinon.createSandbox();
@@ -326,7 +326,7 @@ describe('SourceCommand', () => {
326326
getConnection: () => {
327327
return {
328328
metadata: {
329-
checkDeployStatus: () => deployReport,
329+
checkDeployStatus: () => exampleDeployResponse,
330330
},
331331
};
332332
},
@@ -349,7 +349,7 @@ describe('SourceCommand', () => {
349349

350350
it('should "print" the progress bar', async () => {
351351
const res = await command.callDeployProgress('0Af1h00000fCQgsCAG');
352-
expect(res).to.deep.equal(deployReport);
352+
expect(res).to.deep.equal(exampleDeployResponse);
353353
expect(initProgressBarStub.called).to.be.true;
354354
expect(pbStart.callCount).to.equal(1);
355355
expect(pbStop.callCount).to.equal(1);
@@ -362,7 +362,7 @@ describe('SourceCommand', () => {
362362
expect(initProgressBarStub.called).to.be.false;
363363

364364
const res = await command.callDeployProgress('0Af1h00000fCQgsCAG');
365-
expect(res).to.deep.equal(deployReport);
365+
expect(res).to.deep.equal(exampleDeployResponse);
366366
});
367367

368368
it('should NOT "print" the progress bar because of env var', async () => {
@@ -371,7 +371,7 @@ describe('SourceCommand', () => {
371371
const res = await command.callDeployProgress('0Af1h00000fCQgsCAG');
372372
expect(initProgressBarStub.called).to.be.false;
373373

374-
expect(res).to.deep.equal(deployReport);
374+
expect(res).to.deep.equal(exampleDeployResponse);
375375
} finally {
376376
delete process.env.SFDX_USE_PROGRESS_BAR;
377377
}

test/commands/source/deployReport.ts renamed to test/commands/source/testConsts.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,27 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
export const deployReport = {
7+
8+
export const exampleSourceComponent = {
9+
name: 'GeocodingService',
10+
type: {
11+
id: 'apexclass',
12+
name: 'ApexClass',
13+
suffix: 'cls',
14+
directoryName: 'classes',
15+
inFolder: false,
16+
strictDirectoryName: false,
17+
strategies: {
18+
adapter: 'matchingContentFile',
19+
},
20+
},
21+
xml:
22+
'/Users/william.ruemmele/projects/scratches/dreamhouse-lwc/force-app/main/default/classes/GeocodingService.cls-meta.xml',
23+
content:
24+
'/Users/william.ruemmele/projects/scratches/dreamhouse-lwc/force-app/main/default/classes/GeocodingService.cls',
25+
};
26+
27+
export const exampleDeployResponse = {
828
result: {
929
canceledBy: '0051h000006BHOq',
1030
canceledByName: 'User User',

0 commit comments

Comments
 (0)