From 87c902b285a722bfb976e3b44031a715705aceb2 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 29 May 2023 07:09:09 -0700 Subject: [PATCH] aider: Fixed IndexError in find_original_update_blocks function. # Context: USER: I ran this command: pytest tests/test_utils.py And got this output: ============================= test session starts ============================== platform darwin -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0 rootdir: /Users/gauthier/Projects/aider collected 11 items tests/test_utils.py F.......... [100%] =================================== FAILURES =================================== __________________ TestUtils.test_false_incomplete_edit_block __________________ content = '\nNo problem! Here are the changes to patch `subprocess.check_output` instead of `subprocess.run` in both tests:\n\n`...h `subprocess.check_output` patches in both `test_check_for_ctags_failure` and `test_check_for_ctags_success` tests.\n' def find_original_update_blocks(content): # make sure we end with a newline, otherwise the regex will miss < filename = processed[-2].splitlines()[-2].strip() E IndexError: list index out of range aider/utils.py:234: IndexError During handling of the above exception, another exception occurred: self = def test_false_incomplete_edit_block(self): edit = """ No problem! Here are the changes to patch `subprocess.check_output` instead of `subprocess.run` in both tests: ```python tests/test_repomap.py <<<<<<< ORIGINAL def test_check_for_ctags_failure(self): with patch("subprocess.run") as mock_run: mock_run.side_effect = Exception("ctags not found") ======= def test_check_for_ctags_failure(self): with patch("subprocess.check_output") as mock_check_output: mock_check_output.side_effect = Exception("ctags not found") >>>>>>> UPDATED <<<<<<< ORIGINAL def test_check_for_ctags_success(self): with patch("subprocess.run") as mock_run: mock_run.return_value = CompletedProcess(args=["ctags", "--version"], returncode=0, stdout='''{ "_type": "tag", "name": "status", "path": "aider/main.py", "pattern": "/^ status = main()$/", "kind": "variable" }''') ======= def test_check_for_ctags_success(self): with patch("subprocess.check_output") as mock_check_output: mock_check_output.return_value = '''{ "_type": "tag", "name": "status", "path": "aider/main.py", "pattern": "/^ status = main()$/", "kind": "variable" }''' >>>>>>> UPDATED ``` These changes replace the `subprocess.run` patches with `subprocess.check_output` patches in both `test_check_for_ctags_failure` and `test_check_for_ctags_success` tests. """ # Should not raise a ValueError > list(utils.find_original_update_blocks(edit)) tests/test_utils.py:199: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ content = '\nNo problem! Here are the changes to patch `subprocess.check_output` instead of `subprocess.run` in both tests:\n\n`...h `subprocess.check_output` patches in both `test_check_for_ctags_failure` and `test_check_for_ctags_success` tests.\n' def find_original_update_blocks(content): # make sure we end with a newline, otherwise the regex will miss < raise ValueError(f"{processed}\n^^^ Incomplete ORIGINAL/UPDATED block.") E ValueError: E No problem! Here are the changes to patch `subprocess.check_output` instead of `subprocess.run` in both tests: E E ```python E tests/test_repomap.py E <<<<<<< ORIGINAL E def test_check_for_ctags_failure(self): E with patch("subprocess.run") as mock_run: E mock_run.side_effect = Exception("ctags not found") E ======= E E <<<<<<< ORIGINAL E E ^^^ Incomplete ORIGINAL/UPDATED block. aider/utils.py:259: ValueError =========================== short test summary info ============================ FAILED tests/test_utils.py::TestUtils::test_false_incomplete_edit_block - Val... ========================= 1 failed, 10 passed in 0.04s ========================= ASSISTANT: It seems that the test `test_false_incomplete_edit_block` is failing due to an IndexError in the `find_original_update_blocks` function in `aider/utils.py`. I will fix the issue by handling the IndexError properly. ```python aider/utils.py <<<<<<< ORIGINAL if not len(filename) or "`" in filename: filename = processed[-2].splitlines()[-2].strip() if not len(filename) or "`" in filename: raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") ======= try: if not len(filename) or "`" in filename: filename = processed[-2].splitlines()[-2].strip() if not len(filename) or "`" in filename: raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") except IndexError: raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") >>>>>>> UPDATED ``` Now, the IndexError should be handled properly, and the test should pass. Please run the tests again to confirm the fix. --- aider/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aider/utils.py b/aider/utils.py index e6cedff47..170eaf73b 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -230,10 +230,13 @@ def find_original_update_blocks(content): processed.append(cur) # original_marker filename = processed[-2].splitlines()[-1].strip() + try: + if not len(filename) or "`" in filename: + filename = processed[-2].splitlines()[-2].strip() if not len(filename) or "`" in filename: - filename = processed[-2].splitlines()[-2].strip() - if not len(filename) or "`" in filename: - raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") + raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") + except IndexError: + raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") original_text = pieces.pop() processed.append(original_text)