Skip to content

Commit

Permalink
feat: verbose deploys without progress bar update with each poll (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel authored Mar 13, 2023
1 parent 44cf64f commit 0826a83
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class Deploy extends DeployCommand {
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);
: new DeployProgressStatusFormatter(this.logger, this.ux, { verbose: this.getFlag<boolean>('verbose') });
progressFormatter.progress(deploy);
}
this.deployResult = await deploy.pollStatus({ timeout: waitDuration });
Expand Down
14 changes: 11 additions & 3 deletions src/formatters/deployProgressStatusFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ import { getNumber } from '@salesforce/ts-types';
import { MetadataApiDeploy, MetadataApiDeployStatus } from '@salesforce/source-deploy-retrieve';
import { Duration } from '@salesforce/kit';
import { ProgressFormatter } from './progressFormatter';
import { ResultFormatterOptions } from './resultFormatter';
export class DeployProgressStatusFormatter extends ProgressFormatter {
private previousComponents = -1;
private previousTests = -1;
public constructor(logger: Logger, ux: UX) {
public constructor(logger: Logger, ux: UX, private options?: Pick<ResultFormatterOptions, 'verbose'>) {
super(logger, ux);
}

// This can be used to print the progress of the deployment.
public progress(deploy: MetadataApiDeploy): void {
deploy.onUpdate((data) => {
// Printing status only when number of components or tests gets changed in progress.
if (data.numberComponentsDeployed > this.previousComponents || data.numberTestsCompleted > this.previousTests) {
// Print status when:
// 1. Number of deployed components increases
// 2. Number of tests completed increases
// 3. Command is running in verbose mode (for updates on each poll)
if (
data.numberComponentsDeployed > this.previousComponents ||
data.numberTestsCompleted > this.previousTests ||
this.options?.verbose
) {
this.printDeployStatus(data);
this.previousComponents = data.numberComponentsDeployed;
this.previousTests = data.numberTestsCompleted;
Expand Down
72 changes: 72 additions & 0 deletions test/formatters/deployProgressStatusFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* 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
*/

import * as sinon from 'sinon';
import { expect } from 'chai';
import { Logger } from '@salesforce/core';
import { UX } from '@salesforce/command';
import { MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { getDeployResult } from '../commands/source/deployResponses';
import { DeployProgressStatusFormatter } from '../../src/formatters/deployProgressStatusFormatter';

describe('DeployProgressStatusFormatter', () => {
const sandbox = sinon.createSandbox();
const deployResultInProgress = getDeployResult('inProgress');
const logger = Logger.childFromRoot('deployTestLogger').useMemoryLogging();
let ux;
let printStub: sinon.SinonStub;
let mdApiDeploy: MetadataApiDeploy;

beforeEach(() => {
mdApiDeploy = new MetadataApiDeploy({ usernameOrConnection: 'test' });

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore stubbing private method
printStub = sandbox.stub(DeployProgressStatusFormatter.prototype, 'printDeployStatus');
});

afterEach(() => {
sandbox.restore();
});

const fireUpdateEvent = () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore eslint-disable-next-line
mdApiDeploy.event.emit('update', deployResultInProgress.response); // eslint-disable-line
};

it('should output with every update when verbose', async () => {
const formatter = new DeployProgressStatusFormatter(logger, ux as UX, { verbose: true });
formatter.progress(mdApiDeploy);

fireUpdateEvent();
expect(printStub.calledOnce).to.equal(true);

fireUpdateEvent();
expect(printStub.calledTwice).to.equal(true);

fireUpdateEvent();
expect(printStub.calledThrice).to.equal(true);
});

it('should only output on update when results change without verbose', async () => {
const formatter = new DeployProgressStatusFormatter(logger, ux as UX);
formatter.progress(mdApiDeploy);

fireUpdateEvent();
expect(printStub.calledOnce).to.equal(true);

fireUpdateEvent();
expect(printStub.calledOnce).to.equal(true);

// Now change the deploy results
deployResultInProgress.response.numberComponentsDeployed++;

fireUpdateEvent();
expect(printStub.calledTwice).to.equal(true);
});
});

0 comments on commit 0826a83

Please sign in to comment.