From 283dd0b1f1e61ec3eee53ad74ab48da10c3a3d13 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 30 Nov 2024 13:18:49 -0800 Subject: [PATCH] test: refactor read-only file path handling in command tests --- tests/basic/test_commands.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/basic/test_commands.py b/tests/basic/test_commands.py index 93eeb3007..66ea1bdea 100644 --- a/tests/basic/test_commands.py +++ b/tests/basic/test_commands.py @@ -1432,42 +1432,40 @@ class TestCommands(TestCase): os.unlink(external_file_path) def test_cmd_drop_read_only_with_relative_path(self): - with GitTemporaryDirectory() as repo_dir: - io = InputOutput(pretty=False, fancy_input=False, yes=False) - coder = Coder.create(self.GPT35, None, io) - commands = Commands(io, coder) + with ChdirTemporaryDirectory() as repo_dir: + test_file = Path("test_file.txt") + test_file.write_text("Test content") # Create a test file in a subdirectory subdir = Path(repo_dir) / "subdir" subdir.mkdir() - test_file = subdir / "test_file.txt" - test_file.write_text("Test content") + os.chdir(subdir) + + io = InputOutput(pretty=False, fancy_input=False, yes=False) + coder = Coder.create(self.GPT35, None, io) + commands = Commands(io, coder) # Add the file as read-only using absolute path - commands.cmd_read_only(str(test_file)) + rel_path = str(Path("..") / "test_file.txt") + commands.cmd_read_only(rel_path) self.assertEqual(len(coder.abs_read_only_fnames), 1) # Try to drop using relative path from different working directories - os.chdir(subdir) commands.cmd_drop("test_file.txt") self.assertEqual(len(coder.abs_read_only_fnames), 0) # Add it again - commands.cmd_read_only("test_file.txt") + commands.cmd_read_only(rel_path) self.assertEqual(len(coder.abs_read_only_fnames), 1) - # Try dropping with relative path from repo root - os.chdir(repo_dir) - commands.cmd_drop("subdir/test_file.txt") + commands.cmd_drop(rel_path) self.assertEqual(len(coder.abs_read_only_fnames), 0) # Add it one more time - commands.cmd_read_only("subdir/test_file.txt") + commands.cmd_read_only(rel_path) self.assertEqual(len(coder.abs_read_only_fnames), 1) - # Try dropping with parent-relative path - os.chdir(subdir) - commands.cmd_drop("../subdir/test_file.txt") + commands.cmd_drop("test_file.txt") self.assertEqual(len(coder.abs_read_only_fnames), 0) def test_cmd_read_only_with_multiple_files(self):