Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_codeclaw_cli_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess
import sys
from pathlib import Path
import pytest

from codeclaw import cli as codeclaw_cli
from codeclaw import mcp_server
Expand Down Expand Up @@ -215,3 +216,23 @@ def test_mcp_refresh_index_returns_meta(monkeypatch):
assert payload["ok"] is True
assert payload["meta"]["refresh_count"] == 1
assert payload["meta"]["session_count"] == 2

def test_install_mcp_handles_read_oserror(monkeypatch, tmp_path):
monkeypatch.setattr(Path, "home", lambda: tmp_path)
config_path = tmp_path / ".claude" / "mcp.json"
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text("{}", encoding="utf-8")

original_read_text = Path.read_text

def mock_read_text(self, *args, **kwargs):
if str(self).endswith("mcp.json"):
raise OSError("Fake OS Error")
return original_read_text(self, *args, **kwargs)

monkeypatch.setattr(Path, "read_text", mock_read_text)
Comment on lines +226 to +233

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The way Path.read_text is mocked is a bit complex. Since install_mcp only performs one read_text operation in the code path under test, you don't need to preserve the original method or check the filename. A simpler mock that just raises the exception would be more direct and maintainable.

Suggested change
original_read_text = Path.read_text
def mock_read_text(self, *args, **kwargs):
if str(self).endswith("mcp.json"):
raise OSError("Fake OS Error")
return original_read_text(self, *args, **kwargs)
monkeypatch.setattr(Path, "read_text", mock_read_text)
def mock_read_text_raises_oserror(*args, **kwargs):
raise OSError("Fake OS Error")
monkeypatch.setattr(
Path,
"read_text",
mock_read_text_raises_oserror
)


with pytest.raises(SystemExit) as excinfo:
mcp_server.install_mcp()

assert excinfo.value.code == 1