Skip to content

Commit

Permalink
add tests for windows vs other shell spawning behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj committed Nov 1, 2024
1 parent 48ac92b commit 3235029
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions packages/cli-test/src/cli/shell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,48 @@ describe('shell module', () => {
const fakeArgs = ['"hi there"'];
shell.runCommandSync(fakeCmd, fakeArgs);
sandbox.assert.calledOnce(assembleSpy);
sandbox.assert.calledWithMatch(runSpy, fakeCmd, fakeArgs, sinon.match({ shell: true, env: fakeEnv }));
sandbox.assert.calledWithMatch(
runSpy,
sinon.match.string,
sinon.match.array,
sinon.match({ shell: true, env: fakeEnv }),
);
});
it('should raise bubble error details up', () => {
runSpy.throws(new Error('this is bat country'));
assert.throw(() => {
shell.runCommandSync('about to explode', []);
}, /this is bat country/);
});
if (process.platform === 'win32') {
it('on Windows, should wrap command to shell out in a `cmd /s /c` wrapper process', () => {
const fakeEnv = { HEY: 'yo' };
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
const fakeCmd = 'echo';
const fakeArgs = ['"hi there"'];
shell.runCommandSync(fakeCmd, fakeArgs);
sandbox.assert.calledWithMatch(
runSpy,
'cmd',
sinon.match.array.contains(['/s', '/c', fakeCmd, ...fakeArgs]),
sinon.match({ shell: true, env: fakeEnv }),
);
});
} else {
it('on non-Windows, should shell out to provided command directly', () => {
const fakeEnv = { HEY: 'yo' };
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
const fakeCmd = 'echo';
const fakeArgs = ['"hi there"'];
shell.runCommandSync(fakeCmd, fakeArgs);
sandbox.assert.calledWithMatch(
runSpy,
fakeCmd,
sinon.match.array.contains(fakeArgs),
sinon.match({ shell: true, env: fakeEnv }),
);
});
}
});

describe('spawnProcess method', () => {
Expand All @@ -83,14 +117,48 @@ describe('shell module', () => {
const fakeArgs = ['"hi there"'];
shell.spawnProcess(fakeCmd, fakeArgs);
sandbox.assert.calledOnce(assembleSpy);
sandbox.assert.calledWithMatch(spawnSpy, fakeCmd, fakeArgs, sinon.match({ shell: true, env: fakeEnv }));
sandbox.assert.calledWithMatch(
spawnSpy,
sinon.match.string,
sinon.match.array,
sinon.match({ shell: true, env: fakeEnv }),
);
});
it('should raise bubble error details up', () => {
spawnSpy.throws(new Error('this is bat country'));
assert.throw(() => {
shell.spawnProcess('about to explode', []);
}, /this is bat country/);
});
if (process.platform === 'win32') {
it('on Windows, should wrap command to shell out in a `cmd /s /c` wrapper process', () => {
const fakeEnv = { HEY: 'yo' };
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
const fakeCmd = 'echo';
const fakeArgs = ['"hi there"'];
shell.spawnProcess(fakeCmd, fakeArgs);
sandbox.assert.calledWithMatch(
spawnSpy,
'cmd',
sinon.match.array.contains(['/s', '/c', fakeCmd, ...fakeArgs]),
sinon.match({ shell: true, env: fakeEnv }),
);
});
} else {
it('on non-Windows, should shell out to provided command directly', () => {
const fakeEnv = { HEY: 'yo' };
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
const fakeCmd = 'echo';
const fakeArgs = ['"hi there"'];
shell.spawnProcess(fakeCmd, fakeArgs);
sandbox.assert.calledWithMatch(
spawnSpy,
fakeCmd,
sinon.match.array.contains(fakeArgs),
sinon.match({ shell: true, env: fakeEnv }),
);
});
}
});

describe('waitForOutput method', () => {
Expand Down

0 comments on commit 3235029

Please sign in to comment.