From e84418f04887cc7998a1f62784c1c4904f688e23 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Mon, 12 Aug 2024 09:00:27 -0700 Subject: [PATCH] feat: add test for handling read-only files in cmd_add --- tests/basic/test_commands.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/basic/test_commands.py b/tests/basic/test_commands.py index 4190effb5..a5a08025e 100644 --- a/tests/basic/test_commands.py +++ b/tests/basic/test_commands.py @@ -537,6 +537,38 @@ class TestCommands(TestCase): commands.cmd_add("file.txt") self.assertEqual(coder.abs_fnames, set()) + def test_cmd_add_read_only_file(self): + # Initialize the Commands and InputOutput objects + io = InputOutput(pretty=False, yes=True) + from aider.coders import Coder + + coder = Coder.create(self.GPT35, None, io) + commands = Commands(io, coder) + + # Create a test file + test_file = Path("test_read_only.txt") + test_file.write_text("Test content") + + # Add the file as read-only + commands.cmd_read(str(test_file)) + + # Verify it's in abs_read_only_fnames + self.assertTrue(any(os.path.samefile(str(test_file.resolve()), fname) for fname in coder.abs_read_only_fnames)) + + # Mock the repo to simulate a tracked file + coder.repo = mock.MagicMock() + coder.repo.is_tracked_file.return_value = True + + # Try to add the read-only file + commands.cmd_add(str(test_file)) + + # Verify it's now in abs_fnames and not in abs_read_only_fnames + self.assertTrue(any(os.path.samefile(str(test_file.resolve()), fname) for fname in coder.abs_fnames)) + self.assertFalse(any(os.path.samefile(str(test_file.resolve()), fname) for fname in coder.abs_read_only_fnames)) + + # Clean up + test_file.unlink() + def test_cmd_test_unbound_local_error(self): with ChdirTemporaryDirectory(): io = InputOutput(pretty=False, yes=False)