From 323502925240c2f7ed5659a366dd31d90843ba41 Mon Sep 17 00:00:00 2001 From: Filip Maj Date: Fri, 1 Nov 2024 15:57:50 -0400 Subject: [PATCH] add tests for windows vs other shell spawning behaviour --- packages/cli-test/src/cli/shell.spec.ts | 72 ++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/packages/cli-test/src/cli/shell.spec.ts b/packages/cli-test/src/cli/shell.spec.ts index b84d0205f..69d0f15f0 100644 --- a/packages/cli-test/src/cli/shell.spec.ts +++ b/packages/cli-test/src/cli/shell.spec.ts @@ -65,7 +65,12 @@ 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')); @@ -73,6 +78,35 @@ describe('shell module', () => { 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', () => { @@ -83,7 +117,12 @@ 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')); @@ -91,6 +130,35 @@ describe('shell module', () => { 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', () => {