From 290e98650827f8505aa98d8f334d941df4ca9e19 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 2 Nov 2023 08:23:11 -0600 Subject: [PATCH] fix: cache process.cwd() --- src/execCmd.ts | 16 ++++++++++++++-- src/testSession.ts | 16 +++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/execCmd.ts b/src/execCmd.ts index e024b931..904de0e3 100644 --- a/src/execCmd.ts +++ b/src/execCmd.ts @@ -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; @@ -452,3 +453,14 @@ export async function execInteractiveCmd( }); }); } + +export class Cache extends Map { + private static instance: Cache; + + public static getInstance(): Cache { + if (!Cache.instance) { + Cache.instance = new Cache(); + } + return Cache.instance; + } +} diff --git a/src/testSession.ts b/src/testSession.ts index 729a150a..3e88c416 100644 --- a/src/testSession.ts +++ b/src/testSession.ts @@ -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 = { /** @@ -134,6 +134,12 @@ export class TestSession 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); @@ -156,14 +162,6 @@ export class TestSession 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); }