Skip to content

Commit 6694302

Browse files
committed
fix: use newer core scratchOrgCreate
1 parent eb870b5 commit 6694302

File tree

2 files changed

+28
-59
lines changed

2 files changed

+28
-59
lines changed

src/commands/force/org/beta/create.ts

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import {
2525
SandboxUserAuthResponse,
2626
SfError,
2727
StatusEvent,
28-
ScratchOrgRequest,
2928
ScratchOrgInfo,
29+
ScratchOrgRequest,
3030
} from '@salesforce/core';
3131
import { OrgCreateResult } from '../../../../shared/orgHooks';
3232
import { SandboxReporter } from '../../../../shared/sandboxReporter';
@@ -260,25 +260,6 @@ export class Create extends SfdxCommand {
260260
}
261261
}
262262

263-
private async setAliasAndDefaultUsername(username: string): Promise<void> {
264-
const stateAggregator = await StateAggregator.getInstance();
265-
if (this.flags.setalias) {
266-
stateAggregator.aliases.set(this.flags.setalias, username);
267-
this.logger.debug('Set Alias: %s result: %s', this.flags.setalias);
268-
}
269-
if (this.flags.setdefaultusername) {
270-
let config: Config;
271-
try {
272-
config = await Config.create({ isGlobal: false });
273-
} catch {
274-
config = await Config.create({ isGlobal: true });
275-
}
276-
const value = stateAggregator.aliases.get(username) || username;
277-
const result = config.set(OrgConfigProperties.TARGET_ORG, value);
278-
await config.write();
279-
this.logger.debug('Set defaultUsername: %s result: %s', this.flags.setdefaultusername, result);
280-
}
281-
}
282263
private async createScratchOrg(): Promise<ScratchOrgProcessObject> {
283264
this.logger.debug('OK, will do scratch org creation');
284265
if (!this.hubOrg) {
@@ -291,15 +272,14 @@ export class Create extends SfdxCommand {
291272

292273
this.logger.debug('validation complete');
293274

294-
let secret: string;
295-
if (this.flags.clientid) {
296-
// If the user supplied a specific client ID, we have no way of knowing if it's
297-
// a certificate-based Connected App or not. Therefore, we have to assume that
298-
// we'll need the client secret, so prompt the user for it.
299-
secret = await this.ux.prompt(messages.getMessage('secretPrompt'), {
300-
type: 'mask',
301-
});
302-
}
275+
// If the user supplied a specific client ID, we have no way of knowing if it's
276+
// a certificate-based Connected App or not. Therefore, we have to assume that
277+
// we'll need the client secret, so prompt the user for it.
278+
const secret = this.flags.clientid
279+
? await this.ux.prompt(messages.getMessage('secretPrompt'), {
280+
type: 'mask',
281+
})
282+
: undefined;
303283

304284
const createCommandOptions: ScratchOrgRequest = {
305285
connectedAppConsumerKey: this.flags.clientid as string,
@@ -312,14 +292,15 @@ export class Create extends SfdxCommand {
312292
definitionfile: this.flags.definitionfile as string,
313293
orgConfig: this.varargs,
314294
clientSecret: secret,
295+
setDefault: (this.flags.setdefaultusername as boolean) === true,
296+
alias: this.flags.setalias as string | undefined,
297+
tracksSource: true,
315298
};
316299

317300
const { username, scratchOrgInfo, authFields, warnings } = await this.hubOrg.scratchOrgCreate(createCommandOptions);
318301

319302
await Lifecycle.getInstance().emit('scratchOrgInfo', scratchOrgInfo);
320303

321-
await this.setAliasAndDefaultUsername(username);
322-
323304
// emit postorgcreate event for hook
324305
const postOrgCreateHookInfo: OrgCreateResult = [authFields].map((element) => ({
325306
accessToken: element.accessToken,

test/commands/force/org/scratchOrgCreate.test.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { Config, StateAggregator, SfError, Messages, Org, SfProject } from '@salesforce/core';
7+
import { SfError, Messages, Org, SfProject } from '@salesforce/core';
88
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
99
import * as sinon from 'sinon';
1010
import { expect } from '@salesforce/command/lib/test';
@@ -44,8 +44,6 @@ describe('org:create', () => {
4444
let uxLogStub: sinon.SinonStub;
4545
let uxWarnStub: sinon.SinonStub;
4646
let promptStub: sinon.SinonStub;
47-
let aliasGetStub: sinon.SinonStub;
48-
let aliasSetStub: sinon.SinonSpy;
4947
let cmd: TestCreate;
5048

5149
class TestCreate extends Create {
@@ -109,6 +107,7 @@ describe('org:create', () => {
109107
const prodOrg = stubMethod(sandbox, Org.prototype, 'scratchOrgCreate').resolves(CREATE_RESULT);
110108
await command.runIt();
111109
expect(prodOrg.firstCall.args[0]).to.deep.equal({
110+
alias: undefined,
112111
apiversion: undefined,
113112
clientSecret: undefined,
114113
connectedAppConsumerKey: undefined,
@@ -120,6 +119,8 @@ describe('org:create', () => {
120119
unit: 0,
121120
},
122121
retry: 0,
122+
setDefault: false,
123+
tracksSource: true,
123124
definitionfile: 'myScratchDef.json',
124125
orgConfig: {
125126
licenseType: 'LicenseFromVarargs',
@@ -161,6 +162,7 @@ describe('org:create', () => {
161162
const prodOrg = stubMethod(sandbox, Org.prototype, 'scratchOrgCreate').resolves(CREATE_RESULT);
162163
await command.runIt();
163164
expect(prodOrg.firstCall.args[0]).to.deep.equal({
165+
alias: undefined,
164166
apiversion: undefined,
165167
clientSecret,
166168
connectedAppConsumerKey,
@@ -173,6 +175,8 @@ describe('org:create', () => {
173175
unit: 0,
174176
},
175177
retry: 0,
178+
setDefault: false,
179+
tracksSource: true,
176180
orgConfig: {},
177181
});
178182
expect(promptStub.callCount).to.equal(1);
@@ -201,17 +205,9 @@ describe('org:create', () => {
201205
...CREATE_RESULT,
202206
username: 'newScratchUsername',
203207
});
204-
aliasGetStub = sinon.stub().returns('');
205-
aliasSetStub = sinon.spy();
206-
stubMethod(sandbox, StateAggregator, 'getInstance').returns({
207-
aliases: {
208-
get: aliasGetStub,
209-
set: aliasSetStub,
210-
},
211-
});
212-
const configStub = stubMethod(sandbox, Config.prototype, 'set');
213208
await command.runIt();
214209
expect(prodOrg.firstCall.args[0]).to.deep.equal({
210+
alias: 'scratchOrgAlias',
215211
apiversion: undefined,
216212
clientSecret: undefined,
217213
connectedAppConsumerKey: undefined,
@@ -224,11 +220,10 @@ describe('org:create', () => {
224220
unit: 0,
225221
},
226222
retry: 0,
223+
setDefault: true,
224+
tracksSource: true,
227225
orgConfig: {},
228226
});
229-
expect(aliasSetStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
230-
expect(aliasGetStub.firstCall.args).to.deep.equal(['newScratchUsername']);
231-
expect(configStub.firstCall.args).to.deep.equal(['target-org', 'newScratchUsername']);
232227
});
233228

234229
it('will set alias as default', async () => {
@@ -252,18 +247,9 @@ describe('org:create', () => {
252247
username: 'newScratchUsername',
253248
});
254249

255-
aliasGetStub = sinon.stub().returns('scratchOrgAlias');
256-
aliasSetStub = sinon.spy();
257-
stubMethod(sandbox, StateAggregator, 'getInstance').returns({
258-
aliases: {
259-
get: aliasGetStub,
260-
set: aliasSetStub,
261-
},
262-
});
263-
264-
const configStub = stubMethod(sandbox, Config.prototype, 'set');
265250
await command.runIt();
266251
expect(prodOrg.firstCall.args[0]).to.deep.equal({
252+
alias: 'scratchOrgAlias',
267253
apiversion: undefined,
268254
clientSecret: undefined,
269255
connectedAppConsumerKey: undefined,
@@ -277,10 +263,9 @@ describe('org:create', () => {
277263
},
278264
retry: 0,
279265
orgConfig: {},
266+
setDefault: true,
267+
tracksSource: true,
280268
});
281-
expect(aliasSetStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
282-
expect(aliasGetStub.firstCall.args).to.deep.equal(['newScratchUsername']);
283-
expect(configStub.firstCall.args).to.deep.equal(['target-org', 'scratchOrgAlias']);
284269
});
285270

286271
it('will test json output', async () => {
@@ -295,6 +280,7 @@ describe('org:create', () => {
295280
});
296281
const result = await command.runIt();
297282
expect(prodOrg.firstCall.args[0]).to.deep.equal({
283+
alias: undefined,
298284
apiversion: undefined,
299285
clientSecret: undefined,
300286
connectedAppConsumerKey: undefined,
@@ -307,6 +293,8 @@ describe('org:create', () => {
307293
unit: 0,
308294
},
309295
retry: 0,
296+
tracksSource: true,
297+
setDefault: false,
310298
orgConfig: {},
311299
});
312300
expect(result).to.have.keys(['username', 'authFields', 'scratchOrgInfo', 'warnings', 'orgId']);

0 commit comments

Comments
 (0)