Skip to content

Commit 3235029

Browse files
author
Filip Maj
committed
add tests for windows vs other shell spawning behaviour
1 parent 48ac92b commit 3235029

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

packages/cli-test/src/cli/shell.spec.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,48 @@ describe('shell module', () => {
6565
const fakeArgs = ['"hi there"'];
6666
shell.runCommandSync(fakeCmd, fakeArgs);
6767
sandbox.assert.calledOnce(assembleSpy);
68-
sandbox.assert.calledWithMatch(runSpy, fakeCmd, fakeArgs, sinon.match({ shell: true, env: fakeEnv }));
68+
sandbox.assert.calledWithMatch(
69+
runSpy,
70+
sinon.match.string,
71+
sinon.match.array,
72+
sinon.match({ shell: true, env: fakeEnv }),
73+
);
6974
});
7075
it('should raise bubble error details up', () => {
7176
runSpy.throws(new Error('this is bat country'));
7277
assert.throw(() => {
7378
shell.runCommandSync('about to explode', []);
7479
}, /this is bat country/);
7580
});
81+
if (process.platform === 'win32') {
82+
it('on Windows, should wrap command to shell out in a `cmd /s /c` wrapper process', () => {
83+
const fakeEnv = { HEY: 'yo' };
84+
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
85+
const fakeCmd = 'echo';
86+
const fakeArgs = ['"hi there"'];
87+
shell.runCommandSync(fakeCmd, fakeArgs);
88+
sandbox.assert.calledWithMatch(
89+
runSpy,
90+
'cmd',
91+
sinon.match.array.contains(['/s', '/c', fakeCmd, ...fakeArgs]),
92+
sinon.match({ shell: true, env: fakeEnv }),
93+
);
94+
});
95+
} else {
96+
it('on non-Windows, should shell out to provided command directly', () => {
97+
const fakeEnv = { HEY: 'yo' };
98+
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
99+
const fakeCmd = 'echo';
100+
const fakeArgs = ['"hi there"'];
101+
shell.runCommandSync(fakeCmd, fakeArgs);
102+
sandbox.assert.calledWithMatch(
103+
runSpy,
104+
fakeCmd,
105+
sinon.match.array.contains(fakeArgs),
106+
sinon.match({ shell: true, env: fakeEnv }),
107+
);
108+
});
109+
}
76110
});
77111

78112
describe('spawnProcess method', () => {
@@ -83,14 +117,48 @@ describe('shell module', () => {
83117
const fakeArgs = ['"hi there"'];
84118
shell.spawnProcess(fakeCmd, fakeArgs);
85119
sandbox.assert.calledOnce(assembleSpy);
86-
sandbox.assert.calledWithMatch(spawnSpy, fakeCmd, fakeArgs, sinon.match({ shell: true, env: fakeEnv }));
120+
sandbox.assert.calledWithMatch(
121+
spawnSpy,
122+
sinon.match.string,
123+
sinon.match.array,
124+
sinon.match({ shell: true, env: fakeEnv }),
125+
);
87126
});
88127
it('should raise bubble error details up', () => {
89128
spawnSpy.throws(new Error('this is bat country'));
90129
assert.throw(() => {
91130
shell.spawnProcess('about to explode', []);
92131
}, /this is bat country/);
93132
});
133+
if (process.platform === 'win32') {
134+
it('on Windows, should wrap command to shell out in a `cmd /s /c` wrapper process', () => {
135+
const fakeEnv = { HEY: 'yo' };
136+
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
137+
const fakeCmd = 'echo';
138+
const fakeArgs = ['"hi there"'];
139+
shell.spawnProcess(fakeCmd, fakeArgs);
140+
sandbox.assert.calledWithMatch(
141+
spawnSpy,
142+
'cmd',
143+
sinon.match.array.contains(['/s', '/c', fakeCmd, ...fakeArgs]),
144+
sinon.match({ shell: true, env: fakeEnv }),
145+
);
146+
});
147+
} else {
148+
it('on non-Windows, should shell out to provided command directly', () => {
149+
const fakeEnv = { HEY: 'yo' };
150+
sandbox.stub(shell, 'assembleShellEnv').returns(fakeEnv);
151+
const fakeCmd = 'echo';
152+
const fakeArgs = ['"hi there"'];
153+
shell.spawnProcess(fakeCmd, fakeArgs);
154+
sandbox.assert.calledWithMatch(
155+
spawnSpy,
156+
fakeCmd,
157+
sinon.match.array.contains(fakeArgs),
158+
sinon.match({ shell: true, env: fakeEnv }),
159+
);
160+
});
161+
}
94162
});
95163

96164
describe('waitForOutput method', () => {

0 commit comments

Comments
 (0)