Skip to content

Commit

Permalink
fix: deploy:report now respects the wait flag (#153)
Browse files Browse the repository at this point in the history
* fix: deploy:report now respects the wait flag

* chore: fix build issue
  • Loading branch information
WillieRuemmele authored Jul 28, 2021
1 parent 4cc5606 commit c194f7c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
3 changes: 2 additions & 1 deletion messages/report.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"jobid": "job ID of the deployment you want to check; defaults to your most recent CLI deployment if not specified",
"wait": "wait time for command to finish in minutes",
"verbose": "verbose output of deploy result"
}
},
"mdapiDeployFailed": "The metadata deploy operation failed."
}
26 changes: 24 additions & 2 deletions src/commands/force/source/deploy/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import * as os from 'os';
import { Messages, SfdxProject } from '@salesforce/core';
import { flags, FlagsConfig } from '@salesforce/command';
import { Duration } from '@salesforce/kit';
import { Duration, env } from '@salesforce/kit';
import { MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { DeployCommand } from '../../../../deployCommand';
import {
DeployReportCommandResult,
DeployReportResultFormatter,
} from '../../../../formatters/deployReportResultFormatter';
import { ComponentSetBuilder } from '../../../../componentSetBuilder';
import { ProgressFormatter } from '../../../../formatters/progressFormatter';
import { DeployProgressBarFormatter } from '../../../../formatters/deployProgressBarFormatter';
import { DeployProgressStatusFormatter } from '../../../../formatters/deployProgressStatusFormatter';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'report');
Expand All @@ -38,13 +42,21 @@ export class Report extends DeployCommand {
description: messages.getMessage('flags.verbose'),
}),
};

public async run(): Promise<DeployReportCommandResult> {
await this.doReport();
this.resolveSuccess();
return this.formatResult();
}

/**
* This method is here to provide a workaround to stubbing a constructor in the tests.
*
* @param id
*/
public createDeploy(id?: string): MetadataApiDeploy {
return new MetadataApiDeploy({ usernameOrConnection: this.org.getUsername(), id });
}

protected async doReport(): Promise<void> {
const deployId = this.resolveDeployId(this.getFlag<string>('jobid'));

Expand All @@ -61,6 +73,16 @@ export class Report extends DeployCommand {
}
this.componentSet = await ComponentSetBuilder.build({ sourcepath });
}

const waitDuration = this.getFlag<Duration>('wait');
const deploy = this.createDeploy(deployId);
if (!this.isJsonOutput()) {
const progressFormatter: ProgressFormatter = env.getBoolean('SFDX_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter(this.logger, this.ux)
: new DeployProgressStatusFormatter(this.logger, this.ux);
progressFormatter.progress(deploy);
}
await deploy.pollStatus(500, waitDuration.seconds);
this.deployResult = await this.report(deployId);
}

Expand Down
17 changes: 12 additions & 5 deletions src/formatters/deployReportResultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { MetadataApiDeployStatus } from '@salesforce/source-deploy-retrieve/lib/src/client/types';
import { MetadataApiDeployStatus, RequestStatus } from '@salesforce/source-deploy-retrieve/lib/src/client/types';
import { getString } from '@salesforce/ts-types';
import { SfdxError } from '@salesforce/core';
import { DeployResultFormatter } from './deployResultFormatter';

export type DeployReportCommandResult = MetadataApiDeployStatus;
Expand Down Expand Up @@ -35,10 +36,16 @@ export class DeployReportResultFormatter extends DeployResultFormatter {
} else {
this.ux.log('No components deployed');
}
return;
} else {
this.displaySuccesses();
this.displayFailures();
this.displayTestResults();
}
this.displaySuccesses();
this.displayFailures();
this.displayTestResults();

if (status === RequestStatus.Failed) {
throw SfdxError.create('@salesforce/plugin-source', 'report', 'mdapiDeployFailed');
}

return;
}
}
24 changes: 24 additions & 0 deletions test/commands/source/report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import { fromStub, spyMethod, stubInterface, stubMethod } from '@salesforce/ts-s
import { ConfigFile, Org, SfdxProject } from '@salesforce/core';
import { IConfig } from '@oclif/config';
import { UX } from '@salesforce/command';
import { MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { Report } from '../../../src/commands/force/source/deploy/report';
import { DeployReportResultFormatter } from '../../../src/formatters/deployReportResultFormatter';
import { DeployCommandResult } from '../../../src/formatters/deployResultFormatter';
import { DeployProgressBarFormatter } from '../../../src/formatters/deployProgressBarFormatter';
import { DeployProgressStatusFormatter } from '../../../src/formatters/deployProgressStatusFormatter';
import { getDeployResult } from './deployResponses';

describe('force:source:report', () => {
Expand All @@ -33,6 +36,7 @@ describe('force:source:report', () => {
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox));
let checkDeployStatusStub: sinon.SinonStub;
let uxLogStub: sinon.SinonStub;
let pollStatusStub: sinon.SinonStub;

class TestReport extends Report {
public async runIt() {
Expand All @@ -45,6 +49,11 @@ describe('force:source:report', () => {
public setProject(project: SfdxProject) {
this.project = project;
}

public createDeploy(): MetadataApiDeploy {
pollStatusStub = sandbox.stub(MetadataApiDeploy.prototype, 'pollStatus');
return MetadataApiDeploy.prototype;
}
}

const runReportCmd = async (params: string[]) => {
Expand Down Expand Up @@ -91,9 +100,11 @@ describe('force:source:report', () => {
});

it('should display stashed deploy ID', async () => {
const progressBarStub = sandbox.stub(DeployProgressBarFormatter.prototype, 'progress').returns();
const result = await runReportCmd([]);
expect(result).to.deep.equal(expectedResults);
expect(uxLogStub.firstCall.args[0]).to.contain(stashedDeployId);
expect(progressBarStub.calledOnce).to.equal(true);
});

it('should use the jobid flag', async () => {
Expand All @@ -105,18 +116,22 @@ describe('force:source:report', () => {
});

it('should display the jobid flag', async () => {
const progressBarStub = sandbox.stub(DeployProgressBarFormatter.prototype, 'progress').returns();
const result = await runReportCmd(['--jobid', expectedResults.id]);
expect(result).to.deep.equal(expectedResults);
expect(uxLogStub.firstCall.args[0]).to.contain(expectedResults.id);
expect(progressBarStub.calledOnce).to.equal(true);
});

it('should display output with no --json', async () => {
const displayStub = sandbox.stub(DeployReportResultFormatter.prototype, 'display');
const progressBarStub = sandbox.stub(DeployProgressBarFormatter.prototype, 'progress').returns();
const getJsonStub = sandbox.stub(DeployReportResultFormatter.prototype, 'getJson');
await runReportCmd([]);
expect(displayStub.calledOnce).to.equal(true);
expect(getJsonStub.calledOnce).to.equal(true);
expect(uxLogStub.called).to.equal(true);
expect(progressBarStub.calledOnce).to.equal(true);
});

it('should NOT display output with --json', async () => {
Expand All @@ -127,4 +142,13 @@ describe('force:source:report', () => {
expect(getJsonStub.calledOnce).to.equal(true);
expect(uxLogStub.called).to.equal(false);
});

it('should call the correct progress method', async () => {
const progressBarStub = sandbox.stub(DeployProgressBarFormatter.prototype, 'progress').returns();
const progressStatusStub = sandbox.stub(DeployProgressStatusFormatter.prototype, 'progress').returns();
await runReportCmd([]);
expect(progressStatusStub.calledOnce).to.equal(false);
expect(progressBarStub.calledOnce).to.equal(true);
expect(pollStatusStub.calledOnce).to.equal(true);
});
});

0 comments on commit c194f7c

Please sign in to comment.