Skip to content

Tests fail on Windows due to path separator issues #217

@KuaaMU

Description

@KuaaMU

Tests fail on Windows due to path separator issues

Description

When running the test suite on Windows, 4 tests fail due to hardcoded Unix-style paths and path separator incompatibility issues. The tests pass on Linux/macOS but fail on Windows because pathlib.Path automatically converts path separators based on the operating system.

Environment

  • OS: Windows 10 Professional (Version shown as "Microsoft Windows 10 专业版")
  • Python: 3.11.13 (conda environment)
  • claude-agent-sdk version: 0.1.1
  • Test runner: pytest 8.4.2

Steps to Reproduce

  1. Clone the repository on Windows
  2. Install dependencies: pip install -e ".[dev]"
  3. Run tests: python -m pytest tests/ -v

Root Cause

The issue occurs in subprocess_cli.py line 45:

self._cli_path = str(cli_path) if cli_path else self._find_cli()

When cli_path is a pathlib.Path object, calling str() on it converts the path to the OS-specific format. On Windows, Path("/usr/bin/claude") becomes "\\usr\\bin\\claude".

Test Failures

1. test_query_with_async_iterable (tests/test_streaming_client.py:222)

E           claude_agent_sdk._errors.CLIConnectionError: Failed to start Claude Code: [WinError 193] %1 is not a valid Win32 application
  • The test creates a mock CLI with path "/bin/claude" which Windows cannot execute

2. test_cli_path_accepts_pathlib_path (tests/test_transport.py:53)

assert transport._cli_path == "/usr/bin/claude"
  • Expected: /usr/bin/claude
  • Actual: \usr\bin\claude
  • Path separators are converted on Windows

3. test_build_command_with_add_dirs (tests/test_transport.py:144)

assert "--add-dir /path/to/dir1 --add-dir /path/to/dir2" in cmd_str
  • Expected: --add-dir /path/to/dir1 --add-dir /path/to/dir2
  • Actual: --add-dir /path/to/dir1 --add-dir \path\to\dir2
  • The second path (created with Path()) gets Windows separators

4. test_build_command_with_mcp_servers_as_file_path (tests/test_transport.py:346)

assert cmd[mcp_idx + 1] == "/path/to/mcp-config.json"
  • Expected: /path/to/mcp-config.json
  • Actual: \path\to\mcp-config.json
  • Path object converts to Windows format

Test Summary

     =========================== short test summary info ===========================
     FAILED tests/test_streaming_client.py::TestQueryWithAsyncIterable::test_query_with_async_iterable
     FAILED tests/test_transport.py::TestSubprocessCLITransport::test_cli_path_accepts_pathlib_path
     FAILED tests/test_transport.py::TestSubprocessCLITransport::test_build_command_with_add_dirs
     FAILED tests/test_transport.py::TestSubprocessCLITransport::test_build_command_with_mcp_servers_as_file_path
     ======================== 4 failed, 106 passed in 3.89s ========================

Suggested Fix

The tests need to be made platform-aware. Here are two approaches:

Option 1: Use as_posix() for consistent path format (Recommended)

def test_cli_path_accepts_pathlib_path(self):
    from pathlib import Path

    path = Path("/usr/bin/claude")
    transport = SubprocessCLITransport(
        prompt="Hello",
        options=ClaudeAgentOptions(),
        cli_path=path,
    )

    # Compare using as_posix() for consistent format
    assert transport._cli_path == str(path)
    # Or if we need to verify the exact path:
    # assert Path(transport._cli_path) == path

Option 2: Make tests platform-aware

import sys

def test_cli_path_accepts_pathlib_path(self):
    from pathlib import Path

    transport = SubprocessCLITransport(
        prompt="Hello",
        options=ClaudeAgentOptions(),
        cli_path=Path("/usr/bin/claude"),
    )

    # Use platform-specific assertion
    if sys.platform == "win32":
        assert transport._cli_path == r"\usr\bin\claude"
    else:
        assert transport._cli_path == "/usr/bin/claude"

Option 3: Skip Unix-specific tests on Windows

import pytest
import sys

@pytest.mark.skipif(sys.platform == "win32", reason="Unix path test")
def test_cli_path_accepts_pathlib_path(self):
    # ... test code ...

Impact

  • This affects all Windows developers who want to contribute to the project
  • The SDK functionality itself works on Windows, only the tests are affected
  • CI/CD on Windows platforms would fail

Additional Context

These tests appear to be testing the path handling logic but use hardcoded Unix paths. Since the SDK should support Windows users, the tests should either be platform-aware or explicitly marked as Unix-only.

I'm happy to submit a PR to fix these tests if this approach sounds good to the maintainers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions