Skip to content

Commit

Permalink
Sh/ensure testing (#110)
Browse files Browse the repository at this point in the history
* test: fix/add unit tests

Adds some tests. Refactors some tests. Deletes some tests. Changes some public methods to be
protected.

* test: adds tests for retrieve result formatter
  • Loading branch information
shetzel authored Jun 10, 2021
1 parent 187e837 commit 016295a
Show file tree
Hide file tree
Showing 13 changed files with 557 additions and 307 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Source commands for Salesforce CLI

#### Current List of Commands

1. force:source:convert
2. force:source:deploy
3. force:source:deploy:cancel
Expand Down Expand Up @@ -72,7 +73,9 @@ sfdx plugins
```

# Usage

<!-- usage -->

```sh-session
$ npm install -g @salesforce/plugin-source
$ sfdx COMMAND
Expand All @@ -84,15 +87,18 @@ USAGE
$ sfdx COMMAND
...
```

<!-- usagestop -->

# Commands

<!-- commands -->
* [`sfdx force:source:convert [-r <directory>] [-d <directory>] [-n <string>] [-p <array> | -x <string> | -m <array>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourceconvert--r-directory--d-directory--n-string--p-array---x-string---m-array---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:source:deploy [--soapdeploy] [-w <minutes>] [-q <id> | -x <filepath> | -m <array> | -p <array> | -c | -l NoTestRun|RunSpecifiedTests|RunLocalTests|RunAllTestsInOrg | -r <array> | -o | -g] [-u <string>] [--apiversion <string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourcedeploy---soapdeploy--w-minutes--q-id---x-filepath---m-array---p-array---c---l-notestrunrunspecifiedtestsrunlocaltestsrunalltestsinorg---r-array---o---g--u-string---apiversion-string---verbose---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:source:deploy:cancel [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourcedeploycancel--w-minutes--i-id--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:source:deploy:report [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourcedeployreport--w-minutes--i-id--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:source:retrieve [-p <array> | -x <filepath> | -m <array>] [-w <minutes>] [-n <array>] [-u <string>] [-a <string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourceretrieve--p-array---x-filepath---m-array--w-minutes--n-array--u-string--a-string---verbose---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

- [`sfdx force:source:convert [-r <directory>] [-d <directory>] [-n <string>] [-p <array> | -x <string> | -m <array>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourceconvert--r-directory--d-directory--n-string--p-array---x-string---m-array---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:source:deploy [--soapdeploy] [-w <minutes>] [-q <id> | -x <filepath> | -m <array> | -p <array> | -c | -l NoTestRun|RunSpecifiedTests|RunLocalTests|RunAllTestsInOrg | -r <array> | -o | -g] [-u <string>] [--apiversion <string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourcedeploy---soapdeploy--w-minutes--q-id---x-filepath---m-array---p-array---c---l-notestrunrunspecifiedtestsrunlocaltestsrunalltestsinorg---r-array---o---g--u-string---apiversion-string---verbose---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:source:deploy:cancel [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourcedeploycancel--w-minutes--i-id--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:source:deploy:report [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourcedeployreport--w-minutes--i-id--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:source:retrieve [-p <array> | -x <filepath> | -m <array>] [-w <minutes>] [-n <array>] [-u <string>] [-a <string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forcesourceretrieve--p-array---x-filepath---m-array--w-minutes--n-array--u-string--a-string---verbose---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

## `sfdx force:source:convert [-r <directory>] [-d <directory>] [-n <string>] [-p <array> | -x <string> | -m <array>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Expand All @@ -102,7 +108,7 @@ convert source into Metadata API format
convert source into Metadata API format
USAGE
$ sfdx force:source:convert [-r <directory>] [-d <directory>] [-n <string>] [-p <array> | -x <string> | -m <array>]
$ sfdx force:source:convert [-r <directory>] [-d <directory>] [-n <string>] [-p <array> | -x <string> | -m <array>]
[--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand Down Expand Up @@ -145,8 +151,8 @@ deploy source to an org
deploy source to an org
USAGE
$ sfdx force:source:deploy [--soapdeploy] [-w <minutes>] [-q <id> | -x <filepath> | -m <array> | -p <array> | -c | -l
NoTestRun|RunSpecifiedTests|RunLocalTests|RunAllTestsInOrg | -r <array> | -o | -g] [-u <string>] [--apiversion
$ sfdx force:source:deploy [--soapdeploy] [-w <minutes>] [-q <id> | -x <filepath> | -m <array> | -p <array> | -c | -l
NoTestRun|RunSpecifiedTests|RunLocalTests|RunAllTestsInOrg | -r <array> | -o | -g] [-u <string>] [--apiversion
<string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand Down Expand Up @@ -220,7 +226,7 @@ cancel a source deployment
cancel a source deployment
USAGE
$ sfdx force:source:deploy:cancel [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
$ sfdx force:source:deploy:cancel [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand Down Expand Up @@ -258,7 +264,7 @@ check the status of a metadata deployment
check the status of a metadata deployment
USAGE
$ sfdx force:source:deploy:report [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
$ sfdx force:source:deploy:report [-w <minutes>] [-i <id>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand Down Expand Up @@ -301,7 +307,7 @@ retrieve source from an org
retrieve source from an org
USAGE
$ sfdx force:source:retrieve [-p <array> | -x <filepath> | -m <array>] [-w <minutes>] [-n <array>] [-u <string>] [-a
$ sfdx force:source:retrieve [-p <array> | -x <filepath> | -m <array>] [-w <minutes>] [-n <array>] [-u <string>] [-a
<string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand Down Expand Up @@ -347,4 +353,5 @@ EXAMPLES
```

_See code: [src/commands/force/source/retrieve.ts](https://github.com/salesforcecli/plugin-source/blob/v0.0.17/src/commands/force/source/retrieve.ts)_

<!-- commandsstop -->
7 changes: 5 additions & 2 deletions src/commands/force/source/deploy/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { Duration } from '@salesforce/kit';
import { getString } from '@salesforce/ts-types';
import { RequestStatus } from '@salesforce/source-deploy-retrieve/lib/src/client/types';
import { DeployCommand } from '../../../../deployCommand';
import { DeployCancelCommandResult, DeployCancelFormatter } from '../../../../formatters/deployCancelResultFormatter';
import {
DeployCancelCommandResult,
DeployCancelResultFormatter,
} from '../../../../formatters/deployCancelResultFormatter';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'cancel');
Expand Down Expand Up @@ -60,7 +63,7 @@ export class Cancel extends DeployCommand {
}

protected formatResult(): DeployCancelCommandResult {
const formatter = new DeployCancelFormatter(this.logger, this.ux, this.deployResult);
const formatter = new DeployCancelResultFormatter(this.logger, this.ux, this.deployResult);
if (!this.isJsonOutput()) {
formatter.display();
}
Expand Down
4 changes: 1 addition & 3 deletions src/deployCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import { SourceCommand } from './sourceCommand';

export abstract class DeployCommand extends SourceCommand {
protected static readonly STASH_KEY = 'SOURCE_DEPLOY';

protected deployIdDisplayed = false;

protected deployResult: DeployResult;

/**
Expand All @@ -25,7 +23,7 @@ export abstract class DeployCommand extends SourceCommand {
* @param id the Deploy ID of a deployment request
* @returns DeployResult
*/
public async report(id?: string): Promise<DeployResult> {
protected async report(id?: string): Promise<DeployResult> {
const deployId = this.resolveDeployId(id);
this.displayDeployId(deployId);

Expand Down
2 changes: 1 addition & 1 deletion src/formatters/deployCancelResultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ResultFormatter } from './resultFormatter';

export type DeployCancelCommandResult = MetadataApiDeployStatus;

export class DeployCancelFormatter extends ResultFormatter {
export class DeployCancelResultFormatter extends ResultFormatter {
protected result: DeployResult;

public constructor(logger: Logger, ux: UX, result: DeployResult) {
Expand Down
12 changes: 6 additions & 6 deletions src/sourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ export type ProgressBar = {
};

export abstract class SourceCommand extends SfdxCommand {
public static DEFAULT_SRC_WAIT_MINUTES = 33;
public progressBar?: ProgressBar;
public lifecycle = Lifecycle.getInstance();
public static readonly DEFAULT_SRC_WAIT_MINUTES = 33;
protected progressBar?: ProgressBar;
protected lifecycle = Lifecycle.getInstance();

public isJsonOutput(): boolean {
protected isJsonOutput(): boolean {
return getBoolean(this.flags, 'json', false);
}

public getFlag<T>(flagName: string, defaultVal?: unknown): T {
protected getFlag<T>(flagName: string, defaultVal?: unknown): T {
return get(this.flags, flagName, defaultVal) as T;
}

public initProgressBar(): void {
protected initProgressBar(): void {
this.logger.debug('initializing progress bar');
this.progressBar = cli.progress({
format: 'SOURCE PROGRESS | {bar} | {value}/{total} Components',
Expand Down
147 changes: 110 additions & 37 deletions test/commands/source/cancel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,132 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { Dictionary } from '@salesforce/ts-types';
import { DeployResult } from '@salesforce/source-deploy-retrieve';
import { join } from 'path';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { fromStub, spyMethod, stubInterface, stubMethod } from '@salesforce/ts-sinon';
import { ConfigFile, Org, SfdxProject } from '@salesforce/core';
import { IConfig } from '@oclif/config';
import { UX } from '@salesforce/command';
import { Cancel } from '../../../src/commands/force/source/deploy/cancel';
import { exampleDeployResponse } from './testConsts';
import { DeployCancelResultFormatter } from '../../../src/formatters/deployCancelResultFormatter';
import { DeployCommandResult } from '../../../src/formatters/deployResultFormatter';
import { getDeployResult } from './deployResponses';

// TODO: Rewrite tests for changes
describe.skip('force:source:cancel', () => {
const jobid = '0Af1k00000r2BebCAE';
describe('force:source:cancel', () => {
const sandbox = sinon.createSandbox();
const username = '[email protected]';
const defaultDir = join('my', 'default', 'package');
const stashedDeployId = 'IMA000STASHID';

const run = async (
flags: Dictionary<boolean | string | number | string[]> = {},
id?: string
): Promise<DeployResult> => {
// TODO: fix this test for SDRL
// Run the command
// all the stubs will change with SDRL implementation, just call it good for now
return Cancel.prototype.run.call({
flags: Object.assign({}, flags),
getConfig: () => {
return { readSync: () => {}, get: () => jobid };
},
deployReport: () => exampleDeployResponse,
org: {
getConnection: () => {
return {
const deployResult = getDeployResult('canceled');
const expectedResults = deployResult.response as DeployCommandResult;
expectedResults.deployedSource = deployResult.getFileResponses();
expectedResults.outboundFiles = [];
expectedResults.deploys = [deployResult.response];

// Stubs
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox));
let checkDeployStatusStub: sinon.SinonStub;
let invokeStub: sinon.SinonStub;
let uxLogStub: sinon.SinonStub;

class TestCancel extends Cancel {
public async runIt() {
await this.init();
return this.run();
}
public setOrg(org: Org) {
this.org = org;
}
public setProject(project: SfdxProject) {
this.project = project;
}
}

const runCancelCmd = async (params: string[]) => {
const cmd = new TestCancel(params, oclifConfigStub);
stubMethod(sandbox, cmd, 'assignProject').callsFake(() => {
const sfdxProjectStub = fromStub(
stubInterface<SfdxProject>(sandbox, {
getUniquePackageDirectories: () => [{ fullPath: defaultDir }],
})
);
cmd.setProject(sfdxProjectStub);
});
stubMethod(sandbox, cmd, 'assignOrg').callsFake(() => {
const orgStub = fromStub(
stubInterface<Org>(sandbox, {
getUsername: () => username,
getConnection: () => ({
metadata: {
_invoke: () => {
return {
id: id || jobid,
};
},
checkDeployStatus: checkDeployStatusStub,
// TODO: FIX FOR SDR CHANGES WHEN THEY ARE PUBLISHED
_invoke: invokeStub,
},
};
},
},
}) as Promise<DeployResult>;
}),
})
);
cmd.setOrg(orgStub);
});
uxLogStub = stubMethod(sandbox, UX.prototype, 'log');
stubMethod(sandbox, ConfigFile.prototype, 'readSync');
stubMethod(sandbox, ConfigFile.prototype, 'get').returns({ jobid: stashedDeployId });
checkDeployStatusStub = sandbox.stub().resolves(expectedResults);
invokeStub = sandbox.stub().resolves();

return cmd.runIt();
};

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

it('should read from ~/.sfdx/stash.json', async () => {
const result = await run({ json: true });
expect(result).to.deep.equal(exampleDeployResponse);
it('should use stashed deploy ID', async () => {
const getStashSpy = spyMethod(sandbox, Cancel.prototype, 'getStash');
const result = await runCancelCmd(['--json']);
expect(result).to.deep.equal(expectedResults);
expect(getStashSpy.called).to.equal(true);
expect(checkDeployStatusStub.firstCall.args[0]).to.equal(stashedDeployId);
expect(invokeStub.firstCall.args[1]).to.deep.equal({ deployId: stashedDeployId });
});

it('should display stashed deploy ID', async () => {
const result = await runCancelCmd([]);
expect(result).to.deep.equal(expectedResults);
expect(uxLogStub.firstCall.args[0]).to.contain(stashedDeployId);
});

it('should use the jobid flag', async () => {
const jobIdFlag = '0Af1k00000r29C9CAI';
const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag);
expect(result).to.deep.equal(exampleDeployResponse);
const getStashSpy = spyMethod(sandbox, Cancel.prototype, 'getStash');
const result = await runCancelCmd(['--json', '--jobid', expectedResults.id]);
expect(result).to.deep.equal(expectedResults);
expect(getStashSpy.called).to.equal(false);
expect(checkDeployStatusStub.firstCall.args[0]).to.equal(expectedResults.id);
expect(invokeStub.firstCall.args[1]).to.deep.equal({ deployId: expectedResults.id });
});

it('should display the jobid flag', async () => {
const result = await runCancelCmd(['--jobid', expectedResults.id]);
expect(result).to.deep.equal(expectedResults);
expect(uxLogStub.firstCall.args[0]).to.contain(expectedResults.id);
});

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

it('should NOT display output with --json', async () => {
const displayStub = sandbox.stub(DeployCancelResultFormatter.prototype, 'display');
const getJsonStub = sandbox.stub(DeployCancelResultFormatter.prototype, 'getJson');
await runCancelCmd(['--json']);
expect(displayStub.calledOnce).to.equal(false);
expect(getJsonStub.calledOnce).to.equal(true);
expect(uxLogStub.called).to.equal(false);
});
});
Loading

0 comments on commit 016295a

Please sign in to comment.