Skip to content

Commit

Permalink
Merge pull request #554 from salesforcecli/mdonnalley/dev-js
Browse files Browse the repository at this point in the history
feat: support bin/dev.js
  • Loading branch information
iowillhoit authored Sep 26, 2023
2 parents 366f17b + c42d767 commit 4804687
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/execCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
34 changes: 29 additions & 5 deletions test/unit/execCmd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down

0 comments on commit 4804687

Please sign in to comment.