Skip to content

Commit d593b10

Browse files
authored
Merge pull request #292 from salesforcecli/bm/W-10675890
fix: try to create local config first
2 parents 84fc66c + c36162e commit d593b10

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,21 @@ export class Create extends SfdxCommand {
262262
}
263263

264264
private async setAliasAndDefaultUsername(username: string): Promise<void> {
265+
const aliases = await Aliases.create(Aliases.getDefaultOptions());
265266
if (this.flags.setalias) {
266-
const alias = await Aliases.create(Aliases.getDefaultOptions());
267-
alias.set(this.flags.setalias, username);
268-
const result = await alias.write();
269-
this.logger.debug('Set Alias: %s result: %s', this.flags.setalias, result);
267+
await aliases.updateValue(this.flags.setalias, username);
268+
this.logger.debug('Set Alias: %s result: %s', this.flags.setalias);
270269
}
271270
if (this.flags.setdefaultusername) {
272-
const globalConfig: Config = this.configAggregator.getGlobalConfig();
273-
globalConfig.set(Config.DEFAULT_USERNAME, username);
274-
const result = await globalConfig.write();
271+
let config: Config;
272+
try {
273+
config = await Config.create({ isGlobal: false });
274+
} catch {
275+
config = await Config.create({ isGlobal: true });
276+
}
277+
const value = aliases.getKeysByValue(username)[0] || username;
278+
const result = config.set(Config.DEFAULT_USERNAME, value);
279+
await config.write();
275280
this.logger.debug('Set defaultUsername: %s result: %s', this.flags.setdefaultusername, result);
276281
}
277282
}

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('org:create', () => {
184184
'--type',
185185
'scratch',
186186
'--setalias',
187-
'sandboxAlias',
187+
'scratchOrgAlias',
188188
'--setdefaultusername',
189189
'--definitionfile',
190190
definitionfile,
@@ -198,7 +198,8 @@ describe('org:create', () => {
198198
...CREATE_RESULT,
199199
username: 'newScratchUsername',
200200
});
201-
const aliasStub = stubMethod(sandbox, Aliases.prototype, 'set');
201+
const updateValueStub = stubMethod(sandbox, Aliases.prototype, 'updateValue');
202+
const getKeysByValueStub = stubMethod(sandbox, Aliases.prototype, 'getKeysByValue').returns([]);
202203
const configStub = stubMethod(sandbox, Config.prototype, 'set');
203204
await command.runIt();
204205
expect(prodOrg.firstCall.args[0]).to.deep.equal({
@@ -216,10 +217,55 @@ describe('org:create', () => {
216217
retry: 0,
217218
orgConfig: {},
218219
});
219-
expect(aliasStub.firstCall.args).to.deep.equal(['sandboxAlias', 'newScratchUsername']);
220+
expect(updateValueStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
221+
expect(getKeysByValueStub.firstCall.args).to.deep.equal(['newScratchUsername']);
220222
expect(configStub.firstCall.args).to.deep.equal(['defaultusername', 'newScratchUsername']);
221223
});
222224

225+
it('will set alias as default', async () => {
226+
const definitionfile = 'myScratchDef.json';
227+
const command = createCommand([
228+
'--type',
229+
'scratch',
230+
'--setalias',
231+
'scratchOrgAlias',
232+
'--setdefaultusername',
233+
'--definitionfile',
234+
definitionfile,
235+
'-u',
236+
'testProdOrg',
237+
]);
238+
239+
scratchOrgCreateStub.restore();
240+
stubMethod(sandbox, Org, 'create').resolves(Org.prototype);
241+
const prodOrg = stubMethod(sandbox, Org.prototype, 'scratchOrgCreate').resolves({
242+
...CREATE_RESULT,
243+
username: 'newScratchUsername',
244+
});
245+
const updateValueStub = stubMethod(sandbox, Aliases.prototype, 'updateValue');
246+
const getKeysByValueStub = stubMethod(sandbox, Aliases.prototype, 'getKeysByValue').returns(['scratchOrgAlias']);
247+
const configStub = stubMethod(sandbox, Config.prototype, 'set');
248+
await command.runIt();
249+
expect(prodOrg.firstCall.args[0]).to.deep.equal({
250+
apiversion: undefined,
251+
clientSecret: undefined,
252+
connectedAppConsumerKey: undefined,
253+
definitionfile,
254+
durationDays: undefined,
255+
noancestors: undefined,
256+
nonamespace: undefined,
257+
wait: {
258+
quantity: 6,
259+
unit: 0,
260+
},
261+
retry: 0,
262+
orgConfig: {},
263+
});
264+
expect(updateValueStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
265+
expect(getKeysByValueStub.firstCall.args).to.deep.equal(['newScratchUsername']);
266+
expect(configStub.firstCall.args).to.deep.equal(['defaultusername', 'scratchOrgAlias']);
267+
});
268+
223269
it('will test json output', async () => {
224270
const definitionfile = 'myScratchDef.json';
225271
const command = createCommand(['--type', 'scratch', '--definitionfile', definitionfile, '-u', 'testProdOrg']);

0 commit comments

Comments
 (0)