Skip to content

Commit

Permalink
Merge pull request #587 from salesforcecli/mdonnalley/cache-cwd
Browse files Browse the repository at this point in the history
fix: cache process.cwd()
  • Loading branch information
WillieRuemmele authored Nov 2, 2023
2 parents 69b06f1 + 290e986 commit c622261
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/execCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ export const determineExecutable = (cli: CLI = 'inherit'): string => {
const debug = Debug('testkit:determineExecutable');

let bin: string | undefined;
const root = Cache.getInstance().get('pluginDir') ?? process.cwd();
switch (cli) {
case 'inherit':
bin =
env.getString('TESTKIT_EXECUTABLE_PATH') ??
pathJoin(process.cwd(), 'bin', process.platform === 'win32' ? 'run.cmd' : 'run.js');
pathJoin(root, 'bin', process.platform === 'win32' ? 'run.cmd' : 'run.js');
break;
case 'dev':
bin =
env.getString('TESTKIT_EXECUTABLE_PATH') ??
pathJoin(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev.js');
pathJoin(root, 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev.js');
break;
case 'sfdx':
bin = cli;
Expand Down Expand Up @@ -452,3 +453,14 @@ export async function execInteractiveCmd(
});
});
}

export class Cache extends Map<string, string> {
private static instance: Cache;

public static getInstance(): Cache {
if (!Cache.instance) {
Cache.instance = new Cache();
}
return Cache.instance;
}
}
16 changes: 7 additions & 9 deletions src/testSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { zipDir } from './zip';

import { TestProject, TestProjectOptions } from './testProject';
import { DevhubAuthStrategy, getAuthStrategy, testkitHubAuth, transferExistingAuthToEnv } from './hubAuth';
import { determineExecutable, JsonOutput } from './execCmd';
import { Cache, JsonOutput } from './execCmd';

export type ScratchOrgConfig = {
/**
Expand Down Expand Up @@ -134,6 +134,12 @@ export class TestSession<T extends TestSessionOptions = TestSessionOptions> exte
this.debug = debug('testkit:session');
this.zipDir = zipDir;

// Cache the current process.cwd() so that we can use it when resolving
// the bin/run.js executable path. This is necessary because setting the
// project option changes the process.cwd() to the project dir, which
// doesn't have bin/run.js
Cache.getInstance().set('pluginDir', process.cwd());

this.createdDate = new Date();
this.id = genUniqueString(`${this.createdDate.valueOf()}%s`);
this.retries = env.getNumber('TESTKIT_SETUP_RETRIES', this.options.retries ?? 0);
Expand All @@ -156,14 +162,6 @@ export class TestSession<T extends TestSessionOptions = TestSessionOptions> exte
projectDir = this.project.dir;
}

// The default bin/run.js in execCmd will no longer resolve properly when
// a test project is used since process.cwd is changed. If the
// TESTKIT_EXECUTABLE_PATH env var is not being used, then set it
// to use the bin/run.js from the cwd now.
if (!env.getString('TESTKIT_EXECUTABLE_PATH')) {
env.setString('TESTKIT_EXECUTABLE_PATH', determineExecutable('inherit'));
}

this.stubCwd(projectDir);
}

Expand Down

0 comments on commit c622261

Please sign in to comment.