Fix Windows test failures due to path separator issues (#219)

- Make test_cli_path_accepts_pathlib_path platform-aware by comparing
with str(path)
- Fix test_build_command_with_add_dirs to check directories individually
- Fix test_build_command_with_mcp_servers_as_file_path to handle Path
conversion
- Fix test_query_with_async_iterable to properly execute Python scripts
on Windows

All tests now pass on both Windows and Unix-like systems (110/110 tests
passing).

Fixes the issue #217 where pathlib.Path automatically converts path
separators based on the operating system, causing test assertions to
fail on Windows.
This commit is contained in:
KuaaMU 2025-10-08 07:18:30 +08:00 committed by GitHub
parent 333491eef5
commit 14f0714a8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 18 deletions

View file

@ -633,21 +633,28 @@ assert '"Second"' in stdin_messages[1]
print('{"type": "result", "subtype": "success", "duration_ms": 100, "duration_api_ms": 50, "is_error": false, "num_turns": 1, "session_id": "test", "total_cost_usd": 0.001}')
""")
Path(test_script).chmod(0o755)
# Make script executable (Unix-style systems)
if sys.platform != "win32":
Path(test_script).chmod(0o755)
try:
# Mock _find_cli to return python executing our test script
# Mock _find_cli to return the test script path directly
with patch.object(
SubprocessCLITransport, "_find_cli", return_value=sys.executable
SubprocessCLITransport, "_find_cli", return_value=test_script
):
# Mock _build_command to add our test script as first argument
# Mock _build_command to properly execute Python script
original_build_command = SubprocessCLITransport._build_command
def mock_build_command(self):
# Get original command
cmd = original_build_command(self)
# Replace the CLI path with python + script
cmd[0] = test_script
# On Windows, we need to use python interpreter to run the script
if sys.platform == "win32":
# Replace first element with python interpreter and script
cmd[0:1] = [sys.executable, test_script]
else:
# On Unix, just use the script directly
cmd[0] = test_script
return cmd
with patch.object(

View file

@ -44,13 +44,15 @@ class TestSubprocessCLITransport:
"""Test that cli_path accepts pathlib.Path objects."""
from pathlib import Path
path = Path("/usr/bin/claude")
transport = SubprocessCLITransport(
prompt="Hello",
options=ClaudeAgentOptions(),
cli_path=Path("/usr/bin/claude"),
cli_path=path,
)
assert transport._cli_path == "/usr/bin/claude"
# Path object is converted to string, compare with str(path)
assert transport._cli_path == str(path)
def test_build_command_with_system_prompt_string(self):
"""Test building CLI command with system prompt as string."""
@ -129,19 +131,25 @@ class TestSubprocessCLITransport:
"""Test building CLI command with add_dirs option."""
from pathlib import Path
dir1 = "/path/to/dir1"
dir2 = Path("/path/to/dir2")
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeAgentOptions(
add_dirs=["/path/to/dir1", Path("/path/to/dir2")]
),
options=ClaudeAgentOptions(add_dirs=[dir1, dir2]),
cli_path="/usr/bin/claude",
)
cmd = transport._build_command()
cmd_str = " ".join(cmd)
# Check that the command string contains the expected --add-dir flags
assert "--add-dir /path/to/dir1 --add-dir /path/to/dir2" in cmd_str
# Check that both directories are in the command
assert "--add-dir" in cmd
add_dir_indices = [i for i, x in enumerate(cmd) if x == "--add-dir"]
assert len(add_dir_indices) == 2
# The directories should appear after --add-dir flags
dirs_in_cmd = [cmd[i + 1] for i in add_dir_indices]
assert dir1 in dirs_in_cmd
assert str(dir2) in dirs_in_cmd
def test_session_continuation(self):
"""Test session continuation options."""
@ -322,28 +330,31 @@ class TestSubprocessCLITransport:
from pathlib import Path
# Test with string path
string_path = "/path/to/mcp-config.json"
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeAgentOptions(mcp_servers="/path/to/mcp-config.json"),
options=ClaudeAgentOptions(mcp_servers=string_path),
cli_path="/usr/bin/claude",
)
cmd = transport._build_command()
assert "--mcp-config" in cmd
mcp_idx = cmd.index("--mcp-config")
assert cmd[mcp_idx + 1] == "/path/to/mcp-config.json"
assert cmd[mcp_idx + 1] == string_path
# Test with Path object
path_obj = Path("/path/to/mcp-config.json")
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeAgentOptions(mcp_servers=Path("/path/to/mcp-config.json")),
options=ClaudeAgentOptions(mcp_servers=path_obj),
cli_path="/usr/bin/claude",
)
cmd = transport._build_command()
assert "--mcp-config" in cmd
mcp_idx = cmd.index("--mcp-config")
assert cmd[mcp_idx + 1] == "/path/to/mcp-config.json"
# Path object gets converted to string, compare with str(path_obj)
assert cmd[mcp_idx + 1] == str(path_obj)
def test_build_command_with_mcp_servers_as_json_string(self):
"""Test building CLI command with mcp_servers as JSON string."""