test: add glob functionality tests for cmd_read_only method

This commit is contained in:
Paul Gauthier (aider) 2024-10-02 09:20:39 -07:00
parent ff0bacc984
commit 79350ab195

View file

@ -642,6 +642,88 @@ class TestCommands(TestCase):
del commands
del repo
def test_cmd_read_only_with_glob_pattern(self):
with GitTemporaryDirectory() as repo_dir:
io = InputOutput(pretty=False, yes=False)
coder = Coder.create(self.GPT35, None, io)
commands = Commands(io, coder)
# Create multiple test files
test_files = ["test_file1.txt", "test_file2.txt", "other_file.txt"]
for file_name in test_files:
file_path = Path(repo_dir) / file_name
file_path.write_text(f"Content of {file_name}")
# Test the /read-only command with a glob pattern
commands.cmd_read_only("test_*.txt")
# Check if only the matching files were added to abs_read_only_fnames
self.assertEqual(len(coder.abs_read_only_fnames), 2)
for file_name in ["test_file1.txt", "test_file2.txt"]:
file_path = Path(repo_dir) / file_name
self.assertTrue(
any(
os.path.samefile(str(file_path), fname)
for fname in coder.abs_read_only_fnames
)
)
# Check that other_file.txt was not added
other_file_path = Path(repo_dir) / "other_file.txt"
self.assertFalse(
any(
os.path.samefile(str(other_file_path), fname)
for fname in coder.abs_read_only_fnames
)
)
def test_cmd_read_only_with_recursive_glob(self):
with GitTemporaryDirectory() as repo_dir:
io = InputOutput(pretty=False, yes=False)
coder = Coder.create(self.GPT35, None, io)
commands = Commands(io, coder)
# Create a directory structure with files
(Path(repo_dir) / "subdir").mkdir()
test_files = [
"test_file1.txt",
"subdir/test_file2.txt",
"subdir/other_file.txt"
]
for file_name in test_files:
file_path = Path(repo_dir) / file_name
file_path.write_text(f"Content of {file_name}")
# Test the /read-only command with a recursive glob pattern
commands.cmd_read_only("**/*.txt")
# Check if all .txt files were added to abs_read_only_fnames
self.assertEqual(len(coder.abs_read_only_fnames), 3)
for file_name in test_files:
file_path = Path(repo_dir) / file_name
self.assertTrue(
any(
os.path.samefile(str(file_path), fname)
for fname in coder.abs_read_only_fnames
)
)
def test_cmd_read_only_with_nonexistent_glob(self):
with GitTemporaryDirectory() as repo_dir:
io = InputOutput(pretty=False, yes=False)
coder = Coder.create(self.GPT35, None, io)
commands = Commands(io, coder)
# Test the /read-only command with a non-existent glob pattern
with mock.patch.object(io, "tool_error") as mock_tool_error:
commands.cmd_read_only("nonexistent*.txt")
# Check if the appropriate error message was displayed
mock_tool_error.assert_called_once_with("No matches found for: nonexistent*.txt")
# Ensure no files were added to abs_read_only_fnames
self.assertEqual(len(coder.abs_read_only_fnames), 0)
def test_cmd_add_unicode_error(self):
# Initialize the Commands and InputOutput objects
io = InputOutput(pretty=False, yes=True)