Skip to content

Commit 39914ac

Browse files
authored
Merge pull request #975 from salesforcecli/sh/refresh-sandbox
fix: fix NUTs for sbx refresh
2 parents 73f3808 + 25049ca commit 39914ac

File tree

9 files changed

+46
-29
lines changed

9 files changed

+46
-29
lines changed

test/nut/async-create-resume.nut.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import fs from 'node:fs';
99
import path from 'node:path';
10-
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
10+
import { execCmd, TestSession, genUniqueString } from '@salesforce/cli-plugins-testkit';
1111
import { assert, expect } from 'chai';
1212
import { AuthFields, Global, ScratchOrgCache } from '@salesforce/core';
1313
import { CachedOptions } from '@salesforce/core/lib/org/scratchOrgCache.js';
@@ -37,8 +37,10 @@ describe('env:create:scratch async/resume', () => {
3737
};
3838

3939
before(async () => {
40+
const uid = genUniqueString('acr_%s');
4041
session = await TestSession.create({
4142
project: { name: 'asyncCreateResume' },
43+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
4244
devhubAuthStrategy: 'AUTO',
4345
});
4446
cacheFilePath = path.join(session.dir, '.sf', ScratchOrgCache.getFileName());

test/nut/listAndDisplay.nut.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { join } from 'node:path';
99
import os from 'node:os';
1010
import { expect, config, assert } from 'chai';
11-
import { TestSession } from '@salesforce/cli-plugins-testkit';
11+
import { TestSession, genUniqueString } from '@salesforce/cli-plugins-testkit';
1212
import { execCmd } from '@salesforce/cli-plugins-testkit';
1313
import { OrgListResult, defaultHubEmoji, defaultOrgEmoji } from '../../src/commands/org/list.js';
1414
import { OrgOpenOutput } from '../../src/commands/org/open.js';
@@ -52,8 +52,10 @@ describe('Org Command NUT', () => {
5252
let aliasUserOrgId: string;
5353

5454
before(async () => {
55+
const uid = genUniqueString('listAndDisplay_%s');
5556
session = await TestSession.create({
5657
project: { name: 'listAndDisplay' },
58+
sessionDir: join(process.cwd(), `test_session_${uid}`),
5759
devhubAuthStrategy: 'AUTO',
5860
scratchOrgs: [
5961
{

test/nut/metadata.nut.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
import path from 'node:path';
88
import fs from 'node:fs';
99
import { expect } from 'chai';
10-
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
10+
import { execCmd, TestSession, genUniqueString } from '@salesforce/cli-plugins-testkit';
1111
import type { DescribeMetadataResult } from 'jsforce/lib/api/metadata/schema.js';
1212
import type { ListMetadataCommandResult } from '../../src/commands/org/list/metadata.js';
1313

1414
describe('org list metadata*', () => {
1515
let session: TestSession;
1616
before(async () => {
17+
const uid = genUniqueString('listMetadata_%s');
1718
session = await TestSession.create({
1819
project: {
1920
gitClone: 'https://github.com/trailheadapps/dreamhouse-lwc.git',
2021
},
22+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
2123
devhubAuthStrategy: 'AUTO',
2224
scratchOrgs: [
2325
{

test/nut/open.nut.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import path from 'node:path';
99
import fs from 'node:fs';
10-
import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit';
10+
import { TestSession, execCmd, genUniqueString } from '@salesforce/cli-plugins-testkit';
1111
import { expect, config, assert } from 'chai';
1212
import { AuthFields } from '@salesforce/core';
1313
import { ComponentSetBuilder } from '@salesforce/source-deploy-retrieve';
@@ -24,10 +24,12 @@ describe('test org:open command', () => {
2424
const flowPath = path.join('force-app', 'main', 'default', 'flows', 'Create_property.flow-meta.xml');
2525

2626
before(async () => {
27+
const uid = genUniqueString('orgOpen_%s');
2728
session = await TestSession.create({
2829
project: {
2930
gitClone: 'https://github.com/trailheadapps/dreamhouse-lwc.git',
3031
},
32+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
3133
devhubAuthStrategy: 'AUTO',
3234
scratchOrgs: [
3335
{

test/nut/sandboxRefresh.nut.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from 'node:fs';
99
import path from 'node:path';
1010
import { assert, expect } from 'chai';
1111
import sinon from 'sinon';
12-
import { TestSession } from '@salesforce/cli-plugins-testkit';
12+
import { TestSession, genUniqueString } from '@salesforce/cli-plugins-testkit';
1313
import { Org, SandboxProcessObject, SandboxRequestCache, SfError, Messages, AuthInfo } from '@salesforce/core';
1414
import { stubSfCommandUx, stubSpinner, stubUx } from '@salesforce/sf-plugins-core';
1515
import RefreshSandbox from '../../src/commands/org/refresh/sandbox.js';
@@ -30,7 +30,7 @@ import {
3030
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
3131
const messages = Messages.loadMessages('@salesforce/plugin-org', 'sandboxbase');
3232

33-
describe.skip('Sandbox Refresh', () => {
33+
describe('Sandbox Refresh', () => {
3434
let session: TestSession;
3535
let hubOrgUsername: string;
3636
let cacheFilePath: string;
@@ -43,9 +43,11 @@ describe.skip('Sandbox Refresh', () => {
4343
const sandboxProcessSoql = getSandboxProcessSoql();
4444

4545
before(async () => {
46+
const uid = genUniqueString('sbxRefresh_%s');
4647
session = await TestSession.create({
4748
project: { name: 'sandboxRefresh' },
48-
devhubAuthStrategy: 'AUTO',
49+
devhubAuthStrategy: 'AUTH_URL',
50+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
4951
});
5052
assert(session.hubOrg.username);
5153
hubOrgUsername = session.hubOrg.username;
@@ -67,13 +69,13 @@ describe.skip('Sandbox Refresh', () => {
6769
await session?.clean();
6870
});
6971

70-
afterEach(async () => {
71-
sinonSandbox.restore();
72+
afterEach(() => {
7273
try {
73-
await deleteSandboxCacheFile(cacheFilePath);
74+
deleteSandboxCacheFile(cacheFilePath);
7475
} catch (err) {
7576
// ignore since there isn't always a cache file written
7677
}
78+
sinonSandbox.restore();
7779
});
7880

7981
//
@@ -110,7 +112,7 @@ describe.skip('Sandbox Refresh', () => {
110112
expect(toolingQueryStub.calledOnce).to.be.true;
111113

112114
// check the sandbox cache entry
113-
const cache = await readSandboxCacheFile(cacheFilePath);
115+
const cache = readSandboxCacheFile(cacheFilePath);
114116
expect(cache).to.have.property(sbxName);
115117
expect(cache[sbxName]).to.have.property('action', 'Refresh');
116118
expect(cache[sbxName]).to.have.property('prodOrgUsername', hubOrgUsername);
@@ -154,7 +156,7 @@ describe.skip('Sandbox Refresh', () => {
154156
expect(toolingQueryStub.calledOnce).to.be.true;
155157

156158
// check the sandbox cache entry
157-
const cache = await readSandboxCacheFile(cacheFilePath);
159+
const cache = readSandboxCacheFile(cacheFilePath);
158160
expect(cache).to.have.property(sbxName);
159161
expect(cache[sbxName]).to.have.property('action', 'Refresh');
160162
expect(cache[sbxName]).to.have.property('prodOrgUsername', hubOrgUsername);
@@ -190,6 +192,7 @@ describe.skip('Sandbox Refresh', () => {
190192

191193
try {
192194
await RefreshSandbox.run(['--name', sbxName, '-o', hubOrgUsername, '--async', '--json']);
195+
assert(false, 'Expected SandboxNotFoundError');
193196
} catch (e) {
194197
assert(e instanceof SfError, 'Expect error to be an instance of SfError');
195198
expect(e.name).to.equal('SandboxNotFoundError');
@@ -238,7 +241,7 @@ describe.skip('Sandbox Refresh', () => {
238241
expect(toolingQueryStub.calledOnce).to.be.true;
239242

240243
// check the sandbox cache entry
241-
const cache = await readSandboxCacheFile(cacheFilePath);
244+
const cache = readSandboxCacheFile(cacheFilePath);
242245
expect(cache).to.have.property(sbxName);
243246
expect(cache[sbxName]).to.have.property('action', 'Refresh');
244247
expect(cache[sbxName]).to.have.property('prodOrgUsername', hubOrgUsername);
@@ -283,7 +286,7 @@ describe.skip('Sandbox Refresh', () => {
283286
expect(querySandboxProcessByIdStub.callCount).to.be.greaterThan(2);
284287

285288
// check the sandbox cache entry
286-
const cache = await readSandboxCacheFile(cacheFilePath);
289+
const cache = readSandboxCacheFile(cacheFilePath);
287290
expect(cache).to.have.property(sbxName);
288291
expect(cache[sbxName]).to.have.property('action', 'Refresh');
289292
expect(cache[sbxName]).to.have.property('prodOrgUsername', hubOrgUsername);
@@ -299,7 +302,7 @@ describe.skip('Sandbox Refresh', () => {
299302
expect(sfCommandUxStubs.info.firstCall.args[0]).to.equal(sbxStatusMsg);
300303
});
301304

302-
it.skip('should poll and report a success and write an auth file', async () => {
305+
it('should poll and report a success and write an auth file', async () => {
303306
const sbxInfo = getSandboxInfo();
304307
const sbxName = sbxInfo.SandboxName;
305308
const sbxProcess = getSandboxProcess();
@@ -334,7 +337,7 @@ describe.skip('Sandbox Refresh', () => {
334337

335338
// Stub AuthInfo functions so an auth file is written without making http calls
336339
// @ts-expect-error stubbing private function
337-
const authInfoCreateStub = sinonSandbox.stub(AuthInfo.prototype, 'exchangeToken').resolves({
340+
const authInfoExchangeTokenStub = sinonSandbox.stub(AuthInfo.prototype, 'exchangeToken').resolves({
338341
username: sbxAuthResponse.authUserName,
339342
parentUsername: hubOrgUsername,
340343
instanceUrl: sbxAuthResponse.instanceUrl,
@@ -366,10 +369,10 @@ describe.skip('Sandbox Refresh', () => {
366369
expect(toolingQueryStub.calledOnce, 'toolingQueryStub called').to.be.true;
367370
expect(querySandboxProcessByIdStub.called, 'querySandboxProcessByIdStub called').to.be.true;
368371
expect(sandboxSignupCompleteStub.called, 'sandboxSignupCompleteStub called').to.be.true;
369-
expect(authInfoCreateStub.called, 'authInfoCreateStub called').to.be.true;
372+
expect(authInfoExchangeTokenStub.called, 'authInfoExchangeTokenStub called').to.be.true;
370373

371374
// Check auth files exist
372-
const authFileContents = await readAuthFile(session.homeDir, sbxAuthResponse.authUserName);
375+
const authFileContents = readAuthFile(session.homeDir, sbxAuthResponse.authUserName);
373376
expect(authFileContents).to.be.ok;
374377
expect(authFileContents).to.have.property('username', sbxAuthResponse.authUserName);
375378
expect(authFileContents).to.have.property('parentUsername', hubOrgUsername);
@@ -379,7 +382,7 @@ describe.skip('Sandbox Refresh', () => {
379382

380383
// check sandbox auth file doesn't exist
381384
try {
382-
await readSandboxCacheFile(cacheFilePath);
385+
readSandboxCacheFile(cacheFilePath);
383386
assert(false, 'should not have found a sandbox cache file');
384387
} catch (err) {
385388
// ignore

test/nut/scratchCreate.nut.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ describe('env create scratch NUTs', () => {
2929
};
3030

3131
before(async () => {
32+
const uid = genUniqueString('sbxCreate_%s');
3233
session = await TestSession.create({
3334
project: { name: 'scratchOrgCreate' },
3435
devhubAuthStrategy: 'AUTO',
36+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
3537
});
3638
});
3739

test/nut/scratchDelete.nut.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import path from 'node:path';
99
import fs from 'node:fs';
10-
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
10+
import { execCmd, TestSession, genUniqueString } from '@salesforce/cli-plugins-testkit';
1111
import { assert, expect } from 'chai';
1212
import { ScratchDeleteResponse } from '../../src/commands/org/delete/scratch.js';
1313

@@ -18,8 +18,10 @@ describe('org:delete:scratch NUTs', () => {
1818
let session: TestSession;
1919

2020
before(async () => {
21+
const uid = genUniqueString('scratchDelete_%s');
2122
session = await TestSession.create({
2223
project: { name: 'scratchOrgDelete' },
24+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
2325
devhubAuthStrategy: 'AUTO',
2426
scratchOrgs: [
2527
{

test/nut/tracking.nut.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8-
9-
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
8+
import path from 'node:path';
9+
import { execCmd, TestSession, genUniqueString } from '@salesforce/cli-plugins-testkit';
1010
import { expect } from 'chai';
1111
import { Messages } from '@salesforce/core';
1212
import { OrgEnableTrackingResult } from '../../src/commands/org/enable/tracking.js';
1313
import { OrgDisableTrackingResult } from '../../src/commands/org/disable/tracking.js';
1414

15-
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
15+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1616
const messages = Messages.loadMessages('@salesforce/plugin-org', 'org.enable.tracking');
1717

1818
describe('org enable/disable tracking NUTs', () => {
1919
let session: TestSession;
2020

2121
before(async () => {
22+
const uid = genUniqueString('tracking_%s');
2223
session = await TestSession.create({
2324
devhubAuthStrategy: 'AUTO',
2425
project: { name: 'orgEnableDisableTrackingNut' },
26+
sessionDir: path.join(process.cwd(), `test_session_${uid}`),
2527
scratchOrgs: [{ setDefault: true, edition: 'developer' }],
2628
});
2729
});

test/shared/sandboxMockUtils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ export const updateSuccessResponse: SaveResult = {
7474
errors: [],
7575
};
7676

77-
export const readSandboxCacheFile = async (cacheFilePath: string): Promise<Record<string, CachedOptions>> =>
78-
JSON.parse(await fs.promises.readFile(cacheFilePath, 'utf8')) as unknown as Record<string, CachedOptions>;
79-
export const deleteSandboxCacheFile = async (cacheFilePath: string): Promise<void> => fs.promises.unlink(cacheFilePath);
80-
export const readAuthFile = async (homeDir: string, username: string): Promise<AuthFields> => {
77+
export const readSandboxCacheFile = (cacheFilePath: string): Record<string, CachedOptions> =>
78+
JSON.parse(fs.readFileSync(cacheFilePath, 'utf8')) as unknown as Record<string, CachedOptions>;
79+
export const deleteSandboxCacheFile = (cacheFilePath: string): void => fs.unlinkSync(cacheFilePath);
80+
export const readAuthFile = (homeDir: string, username: string): AuthFields => {
8181
const filePath = path.join(homeDir, Global.STATE_FOLDER, `${username}.json`);
82-
return JSON.parse(await fs.promises.readFile(filePath, 'utf8')) as AuthFields;
82+
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as AuthFields;
8383
};
8484
export const getSandboxInfo = (overrides?: Partial<SandboxInfo>): SandboxInfo =>
8585
Object.assign({}, sandboxInfo, overrides);
@@ -114,7 +114,7 @@ export const stubProdOrgConnection = async (
114114
.stub(Org, 'create')
115115
.withArgs({ aliasOrUsername: username })
116116
.callsFake(async (opts) => {
117-
const org = (await orgCreateFn.call(Org, opts)) as Org;
117+
const org = (await orgCreateFn(opts)) as Org;
118118
// @ts-expect-error re-assigning a private property
119119
org.connection = connection;
120120
return org;

0 commit comments

Comments
 (0)