Repository navigation
test(mcp): add error handling test for install_mcp - #3
YchampionOP wants to merge 1 commit into
Conversation
- Mock Path.read_text to raise OSError in install_mcp - Assert SystemExit(1) is raised when OSError occurs - Ensure pytest is imported in tests/test_codeclaw_cli_mcp.py Co-authored-by: YchampionOP <86096030+YchampionOP@users.noreply.github.com>
Summary of ChangesHello @YchampionOP, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a test case for error handling when reading the MCP configuration file. The test correctly verifies that an OSError during file read leads to a SystemExit. I've suggested a small simplification to the mocking logic in the new test to improve its clarity and maintainability. Otherwise, the change is good.
| 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) |
There was a problem hiding this comment.
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.
| 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 | |
| ) | |
Co-authored-by: ychampion <68075205+ychampion@users.noreply.github.com>
ychampion
left a comment
There was a problem hiding this comment.
Thanks for covering this branch. I replayed this PR on current main and the full suite passed all 428 tests. I also ran codeclaw install-mcp from the published 0.4.6 wheel against three disposable homes. A directory at the config path and an unreadable file each produced the read diagnostic and exit 1. A writable config kept its existing server entry and installed CodeClaw.
I'd keep the regression in this PR and exercise a real read failure. The suggestion also checks the diagnostic and verifies that the installer leaves the existing path alone. The full suite passed all 428 tests with this version too.
| 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) | ||
|
|
||
| with pytest.raises(SystemExit) as excinfo: | ||
| mcp_server.install_mcp() | ||
|
|
||
| assert excinfo.value.code == 1 |
There was a problem hiding this comment.
| 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) | |
| with pytest.raises(SystemExit) as excinfo: | |
| mcp_server.install_mcp() | |
| assert excinfo.value.code == 1 | |
| def test_install_mcp_handles_read_oserror(monkeypatch, tmp_path, capsys): | |
| monkeypatch.setattr(Path, "home", lambda: tmp_path) | |
| config_path = tmp_path / ".claude" / "mcp.json" | |
| config_path.mkdir(parents=True) | |
| marker = config_path / "keep.txt" | |
| marker.write_text("keep", encoding="utf-8") | |
| with pytest.raises(SystemExit) as excinfo: | |
| mcp_server.install_mcp() | |
| captured = capsys.readouterr() | |
| assert excinfo.value.code == 1 | |
| assert f"Error reading {config_path}" in captured.err | |
| assert captured.out == "" | |
| assert marker.read_text(encoding="utf-8") == "keep" | |
| assert not config_path.with_name("mcp.json.corrupt.bak").exists() |
Summary
Describe what changed and why.
Changes
Validation
python -m pytest -qRisk and Rollback
Risk level:
Rollback plan: