Skip to content

Commit

Permalink
fix: new hooks, updated tests (#69)
Browse files Browse the repository at this point in the history
* fix: new hooks, updated tests

* chore: remove extra comments, refactor test const names
  • Loading branch information
WillieRuemmele authored Apr 23, 2021
1 parent 2ed3da2 commit 81e1058
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 35 deletions.
8 changes: 3 additions & 5 deletions src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -106,16 +106,14 @@ export class Deploy extends SourceCommand {
});
return this.deployReport(id);
}

const hookEmitter = Lifecycle.getInstance();
const cs = await this.createComponentSet({
sourcepath: asArray<string>(this.flags.sourcepath),
manifest: asString(this.flags.manifest),
metadata: asArray<string>(this.flags.metadata),
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(),
Expand All @@ -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 } });
Expand Down
9 changes: 3 additions & 6 deletions src/commands/force/source/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -53,8 +53,6 @@ export class Retrieve extends SourceCommand {
protected readonly lifecycleEventNames = ['preretrieve', 'postretrieve'];

public async run(): Promise<FileResponse[]> {
const hookEmitter = Lifecycle.getInstance();

const defaultPackagePath = this.project.getDefaultPackage().fullPath;

const cs = await this.createComponentSet({
Expand All @@ -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({
Expand All @@ -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]));
Expand Down
3 changes: 2 additions & 1 deletion src/sourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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' });
Expand Down
8 changes: 4 additions & 4 deletions test/commands/source/cancel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,7 +28,7 @@ describe('force:source:cancel', () => {
getConfig: () => {
return { readSync: () => {}, get: () => jobid };
},
deployReport: () => deployReport,
deployReport: () => exampleDeployResponse,
org: {
getConnection: () => {
return {
Expand All @@ -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);
});
});
13 changes: 8 additions & 5 deletions test/commands/source/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';
const packageXml = 'package.xml';

// TODO: When output work items have been done we can test result output
// that more closely matches actual output.
Expand Down Expand Up @@ -50,6 +50,9 @@ describe('force:source:deploy', () => {
},
initProgressBar: () => {},
progress: progressStub,
lifecycle: {
emit: lifecycleEmitStub,
},
print: () => {},
}) as Promise<DeployResult>;
};
Expand All @@ -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');
});
Expand Down Expand Up @@ -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);
};
Expand Down
8 changes: 4 additions & 4 deletions test/commands/source/report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -31,7 +31,7 @@ describe('force:source:report', () => {
ux: {
log: () => {},
},
deployReport: () => deployReport,
deployReport: () => exampleDeployResponse,
org: {
getConnection: () => {
return {
Expand All @@ -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);
});
});
12 changes: 8 additions & 4 deletions test/commands/source/retrieve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -38,7 +39,6 @@ describe('force:source:retrieve', () => {
flags: Object.assign({}, flags),
ux: {
log: () => {},
logJson: () => {},
styledHeader: () => {},
table: () => {},
},
Expand All @@ -48,6 +48,9 @@ describe('force:source:retrieve', () => {
org: {
getUsername: () => username,
},
lifecycle: {
emit: lifecycleEmitStub,
},
project: {
getDefaultPackage: () => ({ fullPath: defaultPackagePath }),
},
Expand All @@ -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');
Expand Down Expand Up @@ -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);
};
Expand Down
10 changes: 5 additions & 5 deletions test/commands/source/sourceCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('SourceCommand', () => {
getConnection: () => {
return {
metadata: {
checkDeployStatus: () => deployReport,
checkDeployStatus: () => exampleDeployResponse,
},
};
},
Expand All @@ -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);
Expand All @@ -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 () => {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 81e1058

Please sign in to comment.