From d2c13b297632e527360915eff2493a527f46e6e0 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 5 Jul 2023 21:42:36 -0700 Subject: [PATCH] aider: Refactored `cmd_drop` method to match glob pattern against the filename only, not the full file path. # Context: USER: I ran this command: pytest tests/test_commands.py And got this output: ============================= test session starts ============================== platform darwin -- Python 3.11.3, pytest-7.3.1, pluggy-1.2.0 rootdir: /Users/gauthier/Projects/aider configfile: pytest.ini collected 3 items tests/test_commands.py ..F [100%] =================================== FAILURES =================================== ________________ TestCommands.test_cmd_drop_with_glob_patterns _________________ self = def test_cmd_drop_with_glob_patterns(self): # Initialize the Commands and InputOutput objects io = InputOutput(pretty=False, yes=True) from aider.coders import Coder coder = Coder.create(models.GPT35, None, io, openai_api_key="deadbeef") commands = Commands(io, coder) with open("test1.py", "w") as f: f.write("print('test1')") with open("test2.py", "w") as f: f.write("print('test2')") # Add some files to the chat session commands.cmd_add("*.py") # Call the cmd_drop method with a glob pattern commands.cmd_drop("*2.py") # Check if the Python files have been removed from the chat session > self.assertIn(os.path.abspath("test1.py"), coder.abs_fnames) E AssertionError: '/private/var/folders/49/kxrdwwbx0h9bchx99397477c0000gn/T/tmpijmz7big/test1.py' not found in set() tests/test_commands.py:81: AssertionError ----------------------------- Captured stdout call ----------------------------- Model: gpt-3.5-turbo Git repo: none Repo-map: disabled Added test1.py to the chat Added test2.py to the chat Removed test2.py from the chat Removed test1.py from the chat =========================== short test summary info ============================ FAILED tests/test_commands.py::TestCommands::test_cmd_drop_with_glob_patterns ========================= 1 failed, 2 passed in 0.98s ========================== ASSISTANT: It seems like the test `test_cmd_drop_with_glob_patterns` is failing because the `cmd_drop` method is removing all files that match the glob pattern "*2.py", which includes "test1.py" and "test2.py". This is because the glob pattern is being matched against the full file path, not just the filename. To fix this, we need to modify the `cmd_drop` method in the `Commands` class in the `aider/commands.py` file. Specifically, we need to change the way we match the glob pattern against the file paths. Here are the changes: ```python aider/commands.py <<<<<<< ORIGINAL def cmd_drop(self, args): "Remove matching files from the chat session" if not args.strip(): self.io.tool_output("Dropping all files from the chat session.") self.coder.abs_fnames = set() for word in args.split(): matched_files = [ file for file in self.coder.abs_fnames if any(glob.fnmatch.fnmatch(os.path.relpath(file, self.coder.root), word) for file in self.coder.abs_fnames) ] if not matched_files: self.io.tool_error(f"No files matched '{word}'") for matched_file in matched_files: relative_fname = os.path.relpath(matched_file, self.coder.root) self.coder.abs_fnames.remove(matched_file) self.io.tool_output(f"Removed {relative_fname} from the chat") ======= def cmd_drop(self, args): "Remove matching files from the chat session" if not args.strip(): self.io.tool_output("Dropping all files from the chat session.") self.coder.abs_fnames = set() for word in args.split(): matched_files = [ file for file in self.coder.abs_fnames if glob.fnmatch.fnmatch(os.path.basename(file), word) ] if not matched_files: self.io.tool_error(f"No files matched '{word}'") for matched_file in matched_files: relative_fname = os.path.relpath(matched_file, self.coder.root) self.coder.abs_fnames.remove(matched_file) self.io.tool_output(f"Removed {relative_fname} from the chat") >>>>>>> UPDATED ``` This change will ensure that the glob pattern is matched against the filename only, not the full file path. This should fix the failing test. --- aider/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aider/commands.py b/aider/commands.py index ccda68e7d..3d62afe93 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -288,7 +288,7 @@ class Commands: matched_files = [ file for file in self.coder.abs_fnames - if any(glob.fnmatch.fnmatch(os.path.relpath(file, self.coder.root), word) for file in self.coder.abs_fnames) + if glob.fnmatch.fnmatch(os.path.basename(file), word) ] if not matched_files: self.io.tool_error(f"No files matched '{word}'")