fix(core): mock powershell output in shell-utils test (#15831)

This commit is contained in:
Gal Zahavi
2026-01-02 15:20:26 -08:00
committed by GitHub
parent e78c3fe4f0
commit 30f5c4af4a

View File

@@ -69,6 +69,24 @@ afterEach(() => {
vi.clearAllMocks();
});
const mockPowerShellResult = (
commands: Array<{ name: string; text: string }>,
hasRedirection: boolean,
) => {
mockSpawnSync.mockReturnValue({
stdout: Buffer.from(
JSON.stringify({
success: true,
commands,
hasRedirection,
}),
),
stderr: Buffer.from(''),
status: 0,
error: undefined,
});
};
describe('getCommandRoots', () => {
it('should return a single command', () => {
expect(getCommandRoots('ls -l')).toEqual(['ls']);
@@ -197,6 +215,14 @@ describeWindowsOnly('PowerShell integration', () => {
});
it('should return command roots using PowerShell AST output', () => {
mockPowerShellResult(
[
{ name: 'Get-ChildItem', text: 'Get-ChildItem' },
{ name: 'Select-Object', text: 'Select-Object Name' },
],
false,
);
const roots = getCommandRoots('Get-ChildItem | Select-Object Name');
expect(roots.length).toBeGreaterThan(0);
expect(roots).toContain('Get-ChildItem');
@@ -378,24 +404,6 @@ describe('hasRedirection (PowerShell via mock)', () => {
process.env['ComSpec'] = 'powershell.exe';
});
const mockPowerShellResult = (
commands: Array<{ name: string; text: string }>,
hasRedirection: boolean,
) => {
mockSpawnSync.mockReturnValue({
stdout: Buffer.from(
JSON.stringify({
success: true,
commands,
hasRedirection,
}),
),
stderr: Buffer.from(''),
status: 0,
error: undefined,
});
};
it('should return true when PowerShell parser detects redirection', () => {
mockPowerShellResult([{ name: 'echo', text: 'echo hello' }], true);
expect(hasRedirection('echo hello > file.txt')).toBe(true);