Skip to content

Commit 49c9824

Browse files
committed
test: add tests for -a and -s flags
1 parent 481b19d commit 49c9824

File tree

3 files changed

+182
-4
lines changed

3 files changed

+182
-4
lines changed

messages/clone.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"setalias": "alias for the created org",
1414
"definitionfile": "path to an org definition file"
1515
},
16+
"commandSuccess": "The sandbox org cloning process %s is in progress. Run \"sfdx force:org:status -n %s\" to check for status. If the org is ready, checking the status also logs the requesting user in to the sandbox org and authorizes the org for use with Salesforce CLI.",
1617
"missingLicenseType": "The sandbox license type is required, but you didn't provide a value. Specify the license type in the sandbox definition file with the \"licenseType\" option, or specify the option as a name-value pair at the command-line. See https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_sandbox_definition.htm for more information.",
1718
"commandOrganizationTypeNotSupport": "The only supported org type is: %s",
1819
"commandOrganizationTypeNotSupportAction": "force:org:clone -t %s",

src/commands/force/org/clone.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ export class OrgCloneCommand extends SfdxCommand {
6262
}),
6363
};
6464

65-
protected readonly lifecycleEventNames = ['postorgcreate'];
66-
6765
public async run(): Promise<unknown> {
6866
const lifecycle = Lifecycle.getInstance();
6967
if (this.flags.type === OrgTypes.Sandbox) {
@@ -75,11 +73,20 @@ export class OrgCloneCommand extends SfdxCommand {
7573

7674
// eslint-disable-next-line @typescript-eslint/require-await
7775
lifecycle.on(SandboxEvents.EVENT_STATUS, async (results: StatusEvent) => {
78-
SandboxReporter.sandboxProgress(results);
76+
this.ux.log(SandboxReporter.sandboxProgress(results));
7977
});
8078

8179
lifecycle.on(SandboxEvents.EVENT_RESULT, async (results: ResultEvent) => {
82-
SandboxReporter.logSandboxProcessResult(results);
80+
const { sandboxReadyForUse, data } = SandboxReporter.logSandboxProcessResult(results);
81+
this.ux.log(sandboxReadyForUse);
82+
this.ux.styledHeader('Sandbox Org Cloning Status');
83+
this.ux.table(data, {
84+
columns: [
85+
{ key: 'key', label: 'Name' },
86+
{ key: 'value', label: 'Value' },
87+
],
88+
});
89+
8390
if (results.sandboxRes && results.sandboxRes.authUserName) {
8491
if (this.flags.setalias) {
8592
const alias = await Aliases.create({});
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright (c) 2021, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import * as fs from 'fs';
8+
import { Org, Aliases, Config, ConfigAggregator, Lifecycle, SandboxEvents } from '@salesforce/core';
9+
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
10+
import * as sinon from 'sinon';
11+
import { expect, IConfig } from '@salesforce/command/lib/test';
12+
import { UX } from '@salesforce/command';
13+
import { OrgCloneCommand } from '../../../../src/commands/force/org/clone';
14+
15+
describe('org:clone', () => {
16+
const sandbox = sinon.createSandbox();
17+
const sanboxname = 'my-sandbox';
18+
const sandboxalias = 'my-sandbox-alias';
19+
const authUserName = 'my-user';
20+
const sandboxProcessObj = {
21+
attributes: {
22+
type: 'SandboxProcess',
23+
url: '/services/data/v54.0/tooling/sobjects/SandboxProcess/0GR4p000000HQG4GAO',
24+
},
25+
Id: '0GR4p000000HQG4GAO',
26+
Status: 'Completed',
27+
SandboxName: sanboxname,
28+
SandboxInfoId: '0GQ4p000000HOL2GAO',
29+
LicenseType: 'DEVELOPER',
30+
CreatedDate: '2022-03-02T15:30:32.000+0000',
31+
CopyProgress: 100,
32+
SandboxOrganization: '00D2f0000008gzD',
33+
SourceId: null,
34+
Description: null,
35+
ApexClassId: '0GQ4p000000HOL2KAO',
36+
EndDate: '2022-03-02T15:45:16.000+0000',
37+
};
38+
const resultObject = {
39+
sandboxProcessObj,
40+
sandboxRes: {
41+
authUserName,
42+
authCode: 'my-auth-code',
43+
instanceUrl: 'https://my-instance.com',
44+
loginUrl: 'https://my-login.com',
45+
},
46+
};
47+
const defFile = {
48+
SourceSandboxName: 'mySandbox',
49+
licenseType: 'Developer',
50+
sandboxName: 'newSandbox',
51+
};
52+
const statusEvent = {
53+
sandboxProcessObj,
54+
interval: 1,
55+
retries: 0,
56+
waitingOnAuth: false,
57+
};
58+
const oclifConfigStub = fromStub(stubInterface<IConfig.IConfig>(sandbox));
59+
60+
// stubs
61+
let uxTableStub: sinon.SinonStub;
62+
let uxStyledHeaderStub: sinon.SinonStub;
63+
let uxLogStub: sinon.SinonStub;
64+
let cmd: TestOrgCloneCommand;
65+
let aliasSetStub: sinon.SinonStub;
66+
let configSetStub: sinon.SinonStub;
67+
let configWriteStub: sinon.SinonStub;
68+
let onStub: sinon.SinonStub;
69+
let readFileSyncStub: sinon.SinonStub;
70+
let configAggregatorStub;
71+
72+
class TestOrgCloneCommand extends OrgCloneCommand {
73+
public async runIt() {
74+
await this.init();
75+
return this.run();
76+
}
77+
public setOrg(org: Org) {
78+
this.org = org;
79+
}
80+
public setConfigAggregator(configAggregator: ConfigAggregator) {
81+
this.configAggregator = configAggregator;
82+
}
83+
}
84+
85+
const runCloneCommand = async (params: string[]) => {
86+
cmd = new TestOrgCloneCommand(params, oclifConfigStub);
87+
stubMethod(sandbox, cmd, 'assignOrg').callsFake(() => {
88+
const orgStubOptions = {
89+
cloneSandbox: sandbox.stub().callsFake(async () => {
90+
return sandboxProcessObj;
91+
}),
92+
};
93+
const orgStub = fromStub(stubInterface<Org>(sandbox, orgStubOptions));
94+
cmd.setOrg(orgStub);
95+
configSetStub = sandbox.stub().returns(true);
96+
configWriteStub = sandbox.stub().resolves(true);
97+
const configAggregatorStubOptions = {
98+
getGlobalConfig: () => ({
99+
set: configSetStub,
100+
write: configWriteStub,
101+
}),
102+
};
103+
configAggregatorStub = fromStub(stubInterface<ConfigAggregator>(sandbox, configAggregatorStubOptions));
104+
cmd.setConfigAggregator(configAggregatorStub);
105+
});
106+
onStub = sandbox
107+
.stub()
108+
.callsArgWith(1, sandboxProcessObj)
109+
.callsArgWith(1, statusEvent)
110+
.callsArgWith(1, resultObject);
111+
stubMethod(sandbox, Lifecycle, 'getInstance').returns({
112+
on: onStub,
113+
});
114+
aliasSetStub = stubMethod(sandbox, Aliases.prototype, 'set').returns(sandboxalias);
115+
uxTableStub = stubMethod(sandbox, UX.prototype, 'table');
116+
uxLogStub = stubMethod(sandbox, UX.prototype, 'log');
117+
uxStyledHeaderStub = stubMethod(sandbox, UX.prototype, 'styledHeader');
118+
readFileSyncStub = stubMethod(sandbox, fs, 'readFileSync').returns(JSON.stringify(defFile));
119+
return cmd.runIt();
120+
};
121+
122+
it('will return sandbox process object', async () => {
123+
const res = await runCloneCommand(['-t', 'sandbox', '-u', 'DevHub', '-f', 'sandbox-def.json']);
124+
expect(uxStyledHeaderStub.calledOnce).to.be.true;
125+
expect(uxTableStub.firstCall.args[0].length).to.be.equal(12);
126+
expect(readFileSyncStub.calledOnce).to.be.true;
127+
expect(uxLogStub.callCount).to.be.equal(3);
128+
expect(aliasSetStub.callCount).to.be.equal(0);
129+
expect(configSetStub.callCount).to.be.equal(0);
130+
expect(configWriteStub.callCount).to.be.equal(0);
131+
expect(onStub.firstCall.firstArg).to.be.equal(SandboxEvents.EVENT_ASYNC_RESULT);
132+
expect(onStub.secondCall.firstArg).to.be.equal(SandboxEvents.EVENT_STATUS);
133+
expect(onStub.thirdCall.firstArg).to.be.equal(SandboxEvents.EVENT_RESULT);
134+
expect(onStub.callCount).to.be.equal(3);
135+
expect(res).to.deep.equal(sandboxProcessObj);
136+
});
137+
138+
it('will set alias and default username', async () => {
139+
const res = await runCloneCommand([
140+
'-t',
141+
'sandbox',
142+
'-u',
143+
'DevHub',
144+
'-f',
145+
'sandbox-def.json',
146+
'-a',
147+
sandboxalias,
148+
'-s',
149+
]);
150+
expect(uxStyledHeaderStub.calledOnce).to.be.true;
151+
expect(uxTableStub.firstCall.args[0].length).to.be.equal(12);
152+
expect(readFileSyncStub.calledOnce).to.be.true;
153+
expect(uxLogStub.callCount).to.be.equal(3);
154+
expect(aliasSetStub.callCount).to.be.equal(1);
155+
expect(aliasSetStub.firstCall.args[0]).to.be.equal(sandboxalias);
156+
expect(aliasSetStub.firstCall.args[1]).to.be.equal(authUserName);
157+
expect(configSetStub.firstCall.args[0]).to.be.equal(Config.DEFAULT_USERNAME);
158+
expect(configSetStub.firstCall.args[1]).to.be.equal(authUserName);
159+
expect(onStub.firstCall.firstArg).to.be.equal(SandboxEvents.EVENT_ASYNC_RESULT);
160+
expect(onStub.secondCall.firstArg).to.be.equal(SandboxEvents.EVENT_STATUS);
161+
expect(onStub.thirdCall.firstArg).to.be.equal(SandboxEvents.EVENT_RESULT);
162+
expect(onStub.callCount).to.be.equal(3);
163+
expect(configWriteStub.calledOnce).to.be.true;
164+
expect(res).to.deep.equal(sandboxProcessObj);
165+
});
166+
167+
afterEach(() => {
168+
sandbox.restore();
169+
});
170+
});

0 commit comments

Comments
 (0)