From 79350ab195a3cf4d234391ab4810b70703d14e80 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Wed, 2 Oct 2024 09:20:39 -0700 Subject: [PATCH] test: add glob functionality tests for cmd_read_only method --- tests/basic/test_commands.py | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/basic/test_commands.py b/tests/basic/test_commands.py index b4c859a57..8e9c8631b 100644 --- a/tests/basic/test_commands.py +++ b/tests/basic/test_commands.py @@ -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)