Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: new hooks, updated tests #69

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
@@ -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,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(),
@@ -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 } });
9 changes: 3 additions & 6 deletions src/commands/force/source/retrieve.ts
Original file line number Diff line number Diff line change
@@ -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<FileResponse[]> {
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]));
3 changes: 2 additions & 1 deletion src/sourceCommand.ts
Original file line number Diff line number Diff line change
@@ -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' });
8 changes: 4 additions & 4 deletions test/commands/source/cancel.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 8 additions & 5 deletions test/commands/source/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -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<DeployResult>;
};
@@ -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);
};
8 changes: 4 additions & 4 deletions test/commands/source/report.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
12 changes: 8 additions & 4 deletions test/commands/source/retrieve.test.ts
Original file line number Diff line number Diff line change
@@ -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);
};
10 changes: 5 additions & 5 deletions test/commands/source/sourceCommand.test.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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',