|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from codemcp.testing import MCPEndToEndTestCase |
| 6 | + |
| 7 | + |
| 8 | +class TestGitDirectoryError(MCPEndToEndTestCase): |
| 9 | + """Test that passing a file path instead of a directory to git operations raises errors properly.""" |
| 10 | + |
| 11 | + async def asyncSetUp(self): |
| 12 | + # Set up test environment with a git repository |
| 13 | + await super().asyncSetUp() |
| 14 | + |
| 15 | + # Create a file that we'll try to use as a directory |
| 16 | + self.sample_file = os.path.join(self.temp_dir.name, "sample.txt") |
| 17 | + with open(self.sample_file, "w") as f: |
| 18 | + f.write("This is a file, not a directory.\n") |
| 19 | + |
| 20 | + # Add and commit the file |
| 21 | + await self.git_run(["add", "sample.txt"]) |
| 22 | + await self.git_run(["commit", "-m", "Add sample file"]) |
| 23 | + |
| 24 | + async def test_file_path_raises_error(self): |
| 25 | + """Test that using a file path for git operations raises NotADirectoryError.""" |
| 26 | + # Get the chat ID for our test |
| 27 | + chat_id = await self.get_chat_id(None) |
| 28 | + |
| 29 | + # Use a file path instead of a directory and verify it fails with NotADirectoryError |
| 30 | + error_message = await self.call_tool_assert_error( |
| 31 | + None, |
| 32 | + "codemcp", |
| 33 | + { |
| 34 | + "subtool": "RunCommand", |
| 35 | + "command": "test", # Using test as a placeholder command that will invoke get_current_commit_hash |
| 36 | + "path": self.sample_file, # This is a file, not a directory |
| 37 | + "chat_id": chat_id, |
| 38 | + }, |
| 39 | + ) |
| 40 | + |
| 41 | + # The error is actually caught and handled in main.py's append_commit_hash |
| 42 | + # We're testing that we've successfully converted the warning to an error that halts execution |
| 43 | + # Since the error is caught and handled within the codebase, we just need to confirm it |
| 44 | + # failed, which is what call_tool_assert_error already verifies |
| 45 | + self.assertTrue(len(error_message) > 0) |
| 46 | + |
| 47 | + async def test_file_path_second_check(self): |
| 48 | + """Second test for file path validation.""" |
| 49 | + # Get the chat ID for our test |
| 50 | + chat_id = await self.get_chat_id(None) |
| 51 | + |
| 52 | + # Use a file path instead of a directory |
| 53 | + error_message = await self.call_tool_assert_error( |
| 54 | + None, |
| 55 | + "codemcp", |
| 56 | + { |
| 57 | + "subtool": "RunCommand", |
| 58 | + "command": "test", # Using test as a placeholder command that will invoke get_current_commit_hash |
| 59 | + "path": self.sample_file, # This is a file, not a directory |
| 60 | + "chat_id": chat_id, |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + # The error is actually caught and handled in main.py's append_commit_hash |
| 65 | + # We're testing that we've successfully converted the warning to an error that halts execution |
| 66 | + # Since the error is caught and handled within the codebase, we just need to confirm it |
| 67 | + # failed, which is what call_tool_assert_error already verifies |
| 68 | + self.assertTrue(len(error_message) > 0) |
| 69 | + |
| 70 | + async def test_file_path_with_write_file(self): |
| 71 | + """Test using WriteFile with a file path which should trigger NotADirectoryError.""" |
| 72 | + # Get the chat ID for our test |
| 73 | + chat_id = await self.get_chat_id(None) |
| 74 | + |
| 75 | + # Try to use WriteFile with a file path instead of a directory |
| 76 | + error_message = await self.call_tool_assert_error( |
| 77 | + None, |
| 78 | + "codemcp", |
| 79 | + { |
| 80 | + "subtool": "WriteFile", |
| 81 | + "path": self.sample_file, # This is a file, not a directory |
| 82 | + "content": "This will fail with NotADirectoryError", |
| 83 | + "description": "Should fail with NotADirectoryError", |
| 84 | + "chat_id": chat_id, |
| 85 | + }, |
| 86 | + ) |
| 87 | + |
| 88 | + # The error is actually caught and handled in main.py's append_commit_hash |
| 89 | + # We're testing that we've successfully converted the warning to an error that halts execution |
| 90 | + # Since the error is caught and handled within the codebase, we just need to confirm it |
| 91 | + # failed, which is what call_tool_assert_error already verifies |
| 92 | + self.assertTrue(len(error_message) > 0) |
| 93 | + |
| 94 | + async def test_write_file_with_file_path(self): |
| 95 | + """Test using WriteFile with a file path instead of a directory (simpler test case).""" |
| 96 | + # Get the chat ID for our test |
| 97 | + chat_id = await self.get_chat_id(None) |
| 98 | + |
| 99 | + # Create a file path to use - append a fake directory to our sample file |
| 100 | + file_path = os.path.join(self.sample_file, "test.txt") |
| 101 | + |
| 102 | + # Try to use WriteFile with a file path (which should fail with NotADirectoryError) |
| 103 | + error_message = await self.call_tool_assert_error( |
| 104 | + None, |
| 105 | + "codemcp", |
| 106 | + { |
| 107 | + "subtool": "WriteFile", |
| 108 | + "path": file_path, # This is a file/test.txt, not a directory/test.txt |
| 109 | + "content": "This should fail", |
| 110 | + "description": "Test write to invalid path", |
| 111 | + "chat_id": chat_id, |
| 112 | + }, |
| 113 | + ) |
| 114 | + |
| 115 | + # Verify the error message contains NotADirectoryError and mentions the file path |
| 116 | + self.assertIn("Not a directory", error_message) |
0 commit comments