fix: command pipe execution by improving shell detection (#437)

## Description
This PR fixes Issue #421 where commands with pipes (e.g., `grep -R ...
-n | head -n 20`) were failing to execute properly after PR #391 was
merged.

## Changes
- Modified the `requiresShell` function to only enable shell mode when
the command is a single string containing shell operators
- Added logic to handle the case where shell operators are passed as
separate arguments
- Added comprehensive tests to verify the fix

## Root Cause
The issue was that the `requiresShell` function was detecting shell
operators like `|` even when they were passed as separate arguments,
which caused the command to be executed with `shell: true`
unnecessarily. This was causing syntax errors when running commands with
pipes.

## Testing
- Added unit tests to verify the fix
- Manually tested with real commands using pipes
- Ensured all existing tests pass

Fixes #421
This commit is contained in:
Brayden Moon
2025-04-21 14:11:19 +10:00
committed by GitHub
parent eafbc75612
commit 8dd1125681
4 changed files with 115 additions and 3 deletions

View File

@@ -0,0 +1,19 @@
import { describe, it, expect } from "vitest";
import { parse } from "shell-quote";
/* eslint-disable no-console */
describe("shell-quote parse with pipes", () => {
it("should correctly parse a command with a pipe", () => {
const cmd = 'grep -n "finally:" some-file | head';
const tokens = parse(cmd);
console.log("Parsed tokens:", JSON.stringify(tokens, null, 2));
// Check if any token has an 'op' property
const hasOpToken = tokens.some(
(token) => typeof token === "object" && "op" in token,
);
expect(hasOpToken).toBe(true);
});
});