Skip to content

Commit 38eed36

Browse files
fix: can delete default being an alias
1 parent 4cddd3f commit 38eed36

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

src/commands/force/org/delete.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ export class Delete extends SfCommand<DeleteResult> {
5555
public async run(): Promise<DeleteResult> {
5656
const { flags } = await this.parse(Delete);
5757
const resolvedUsername =
58-
// from -o alias -> -o username -> [default username]
58+
// from -o alias -> -o username -> [default username resolved an alias] -> [default username]
5959
(await StateAggregator.getInstance()).aliases.getUsername(flags['target-org'] ?? '') ??
6060
flags['target-org'] ??
61+
(await StateAggregator.getInstance()).aliases.getUsername(
62+
this.configAggregator.getPropertyValue('target-org') as string
63+
) ??
6164
(this.configAggregator.getPropertyValue('target-org') as string);
6265

6366
if (!resolvedUsername) {

src/commands/org/delete/sandbox.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ export default class EnvDeleteSandbox extends SfCommand<SandboxDeleteResponse> {
3737

3838
public async run(): Promise<SandboxDeleteResponse> {
3939
const flags = (await this.parse(EnvDeleteSandbox)).flags;
40-
const username = // from -o alias -> -o username -> [default username]
40+
const username =
41+
// from -o alias -> -o username -> [default username resolved an alias] -> [default username]
4142
(await StateAggregator.getInstance()).aliases.getUsername(flags['target-org'] ?? '') ??
4243
flags['target-org'] ??
44+
(await StateAggregator.getInstance()).aliases.getUsername(
45+
this.configAggregator.getPropertyValue('target-org') as string
46+
) ??
4347
(this.configAggregator.getPropertyValue('target-org') as string);
4448
if (!username) {
4549
throw new SfError('The org does not have a username.');

src/commands/org/delete/scratch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { AuthInfo, AuthRemover, Messages, Org, StateAggregator } from '@salesforce/core';
9-
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
9+
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
1010

1111
Messages.importMessagesDirectory(__dirname);
1212
const messages = Messages.loadMessages('@salesforce/plugin-org', 'delete_scratch');
@@ -41,9 +41,12 @@ export default class EnvDeleteScratch extends SfCommand<ScratchDeleteResponse> {
4141
public async run(): Promise<ScratchDeleteResponse> {
4242
const flags = (await this.parse(EnvDeleteScratch)).flags;
4343
const resolvedUsername =
44-
// from -o alias -> -o username -> [default username]
44+
// from -o alias -> -o username -> [default username resolved an alias] -> [default username]
4545
(await StateAggregator.getInstance()).aliases.getUsername(flags['target-org'] ?? '') ??
4646
flags['target-org'] ??
47+
(await StateAggregator.getInstance()).aliases.getUsername(
48+
this.configAggregator.getPropertyValue('target-org') as string
49+
) ??
4750
(this.configAggregator.getPropertyValue('target-org') as string);
4851
const orgId = (await AuthInfo.create({ username: resolvedUsername })).getFields().orgId as string;
4952

test/nut/scratchDelete.nut.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ScratchDeleteResponse } from '../../src/commands/org/delete/scratch';
1414
describe('env:delete:scratch NUTs', () => {
1515
const scratchOrgAlias = 'scratch-org';
1616
const scratchOrgAlias2 = 'scratch-org-2';
17+
const scratchOrgAlias3 = 'scratch-org-3';
1718
let session: TestSession;
1819

1920
before(async () => {
@@ -33,6 +34,7 @@ describe('env:delete:scratch NUTs', () => {
3334
},
3435
{
3536
setDefault: true,
37+
alias: scratchOrgAlias3,
3638
duration: 1,
3739
config: path.join('config', 'project-scratch-def.json'),
3840
},

test/unit/force/org/delete.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { ConfigAggregator, Messages, Org, SfError } from '@salesforce/core';
88
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
99

10-
import { expect, config } from 'chai';
10+
import { config, expect } from 'chai';
1111
import { stubPrompter, stubSfCommandUx } from '@salesforce/sf-plugins-core';
1212
import { SandboxAccessor } from '@salesforce/core/lib/stateAggregator/accessors/sandboxAccessor';
1313
import { Config } from '@oclif/core';
@@ -52,7 +52,7 @@ describe('org:delete', () => {
5252
it('will prompt before attempting to delete', async () => {
5353
const deleteCommand = new Delete([], {} as Config);
5454
deleteCommand.configAggregator = await ConfigAggregator.create();
55-
$$.SANDBOX.stub(deleteCommand.configAggregator, 'getPropertyValue').onSecondCall().returns(testOrg.username);
55+
$$.SANDBOX.stub(deleteCommand.configAggregator, 'getPropertyValue').onThirdCall().returns(testOrg.username);
5656
const res = await deleteCommand.run();
5757
expect(prompterStubs.confirm.calledOnce).to.equal(true);
5858
expect(prompterStubs.confirm.firstCall.args[0]).to.equal(
@@ -61,6 +61,23 @@ describe('org:delete', () => {
6161
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
6262
});
6363

64+
it('will resolve a default alias', async () => {
65+
const deleteCommand = new Delete([], {} as Config);
66+
deleteCommand.configAggregator = await ConfigAggregator.create();
67+
await $$.stubConfig({ 'target-org': 'myAlias' });
68+
$$.stubAliases({ myAlias: testOrg.username });
69+
const getPropertyValueStub = $$.SANDBOX.stub(deleteCommand.configAggregator, 'getPropertyValue')
70+
.onSecondCall()
71+
.returns('myAlias');
72+
const res = await deleteCommand.run();
73+
expect(prompterStubs.confirm.calledOnce).to.equal(true);
74+
expect(prompterStubs.confirm.firstCall.args[0]).to.equal(
75+
messages.getMessage('confirmDelete', ['scratch', testOrg.username])
76+
);
77+
expect(getPropertyValueStub.calledTwice).to.be.true;
78+
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
79+
});
80+
6481
it('will determine sandbox vs scratch org and delete sandbox', async () => {
6582
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(true);
6683
const res = await Delete.run(['--target-org', testOrg.username]);

0 commit comments

Comments
 (0)