diff --git a/src/execCmd.ts b/src/execCmd.ts index 497eb4dc..dc0f1859 100644 --- a/src/execCmd.ts +++ b/src/execCmd.ts @@ -114,18 +114,21 @@ const getExitCodeError = (cmd: string, expectedCode: number, output: ShellString * * If the cli is 'inherit', the executable preference order is: * 1. TESTKIT_EXECUTABLE_PATH env var - * 2. `bin/dev` (default) + * 2. `bin/dev.js` (default) * * @returns The command string with CLI executable. E.g., `"node_modules/bin/sf org:create:user -a testuser1"` */ const determineExecutable = (cli: CLI = 'inherit'): string => { const debug = Debug('testkit:determineExecutable'); - const bin = + let bin = cli === 'inherit' ? env.getString('TESTKIT_EXECUTABLE_PATH') ?? - pathJoin(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev') + pathJoin(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev.js') : cli; + + // Support plugins who still use bin/dev instead of bin/dev.js + if (bin.endsWith('dev.js') && !fs.existsSync(bin)) bin = bin.replace('.js', ''); const which = shelljs.which(bin); let resolvedPath = pathResolve(bin); diff --git a/test/unit/execCmd.test.ts b/test/unit/execCmd.test.ts index e7748ce9..010b7f88 100644 --- a/test/unit/execCmd.test.ts +++ b/test/unit/execCmd.test.ts @@ -37,8 +37,8 @@ describe('execCmd (sync)', () => { sandbox.restore(); }); - it('should default to bin/dev executable', () => { - const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev'); + it('should default to bin/dev.js executable', () => { + const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev.js'); sandbox.stub(fs, 'existsSync').returns(true); sandbox.stub(shelljs, 'which').callsFake((x) => new ShellString(x)); const shellString = new ShellString(JSON.stringify(output)); @@ -49,8 +49,20 @@ describe('execCmd (sync)', () => { expect(execStub.args[0][0]).to.include('2> stderr'); }); - it('should default to bin/dev executable when cli = inherit', () => { + it('should default to bin/dev executable if bin/dev.js does not exist', async () => { const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev'); + sandbox.stub(fs, 'existsSync').withArgs(binPath).returns(false).withArgs(binPath.replace('.js', '')).returns(true); + sandbox.stub(shelljs, 'which').callsFake((x) => new ShellString(x)); + const shellString = new ShellString(JSON.stringify(output)); + const execStub = stubMethod(sandbox, shelljs, 'exec').returns(shellString); + execCmd(cmd); + expect(execStub.args[0][0]).to.include(`${binPath} ${cmd}`); + expect(execStub.args[0][0]).to.include('1> stdout'); + expect(execStub.args[0][0]).to.include('2> stderr'); + }); + + it('should default to bin/dev.js executable when cli = inherit', () => { + const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev.js'); sandbox.stub(fs, 'existsSync').returns(true); sandbox.stub(shelljs, 'which').callsFake((x) => new ShellString(x)); const shellString = new ShellString(JSON.stringify(output)); @@ -228,8 +240,8 @@ describe('execCmd (async)', () => { sandbox.restore(); }); - it('should default to bin/dev executable', async () => { - const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev'); + it('should default to bin/dev.js executable', async () => { + const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev.js'); sandbox.stub(fs, 'existsSync').returns(true); sandbox.stub(shelljs, 'which').callsFake((x) => new ShellString(x)); const shellString = new ShellString(JSON.stringify(output)); @@ -240,6 +252,18 @@ describe('execCmd (async)', () => { expect(execStub.args[0][0]).to.match(/2> .*stderr.*txt/); }); + it('should default to bin/dev executable if bin/dev.js does not exist', async () => { + const binPath = join(process.cwd(), 'bin', process.platform === 'win32' ? 'dev.cmd' : 'dev'); + sandbox.stub(fs, 'existsSync').withArgs(binPath).returns(false).withArgs(binPath.replace('.js', '')).returns(true); + sandbox.stub(shelljs, 'which').callsFake((x) => new ShellString(x)); + const shellString = new ShellString(JSON.stringify(output)); + const execStub = stubMethod(sandbox, shelljs, 'exec').yields(0, shellString, ''); + await execCmd(cmd, { async: true }); + expect(execStub.args[0][0]).to.include(`${binPath} ${cmd}`); + expect(execStub.args[0][0]).to.match(/1> .*stdout.*txt/); + expect(execStub.args[0][0]).to.match(/2> .*stderr.*txt/); + }); + it('should accept valid sfdx path in env var', async () => { const binPath = join('@salesforce', 'cli', 'bin', 'sfdx'); sandbox.stub(fs, 'existsSync').returns(true);