From bb969c7197ea1529e9bb68d6af14e667824ff0c1 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sun, 28 May 2023 22:35:00 -0700 Subject: [PATCH] aider: Added a temporary directory and changed the current working directory in `tests/test_commands.py` and restored the working directory after the context manager exits. # Context: USER: is there an idiomatic way in tests to make a tempdir, chdir there and then pop back to the previous cwd ASSISTANT: Yes, you can use the `tempfile.TemporaryDirectory` context manager along with the `os.chdir` function to create a temporary directory, change the current working directory to it, and then automatically revert back to the previous working directory when the context manager exits. Here's an example: ```python import os import tempfile def test_tempdir_example(): original_cwd = os.getcwd() with tempfile.TemporaryDirectory() as tempdir: os.chdir(tempdir) # Perform your tests here # The working directory is automatically restored after the context manager exits assert os.getcwd() == original_cwd ``` In this example, the `test_tempdir_example` function creates a temporary directory, changes the current working directory to it, and then performs any tests inside the `with` block. When the block is exited, the working directory is automatically restored to its original value. USER: do that in tests/test_commands.py ASSISTANT: Here's the updated `tests/test_commands.py` with the temporary directory and working directory change: ```python tests/test_commands.py <<<<<<< ORIGINAL import os import tempfile from unittest import TestCase from aider.commands import Commands from aider.io import InputOutput class TestCommands(TestCase): def test_cmd_add(self): # Create a temporary directory and change the current working directory with tempfile.TemporaryDirectory() as tmpdir: os.chdir(tmpdir) # Initialize the Commands and InputOutput objects io = InputOutput(pretty=False, yes=True) from aider.coder import Coder coder = Coder(io, openai_api_key="deadbeef") commands = Commands(io, coder) # Call the cmd_add method with 'foo.txt' and 'bar.txt' as a single string commands.cmd_add("foo.txt bar.txt") # Check if both files have been created in the temporary directory self.assertTrue(os.path.exists("foo.txt")) self.assertTrue(os.path.exists("bar.txt")) ======= import os import tempfile from unittest import TestCase from aider.commands import Commands from aider.io import InputOutput class TestCommands(TestCase): def test_cmd_add(self): original_cwd = os.getcwd() with tempfile.TemporaryDirectory() as tmpdir: os.chdir(tmpdir) # Initialize the Commands and InputOutput objects io = InputOutput(pretty=False, yes=True) from aider.coder import Coder coder = Coder(io, openai_api_key="deadbeef") commands = Commands(io, coder) # Call the cmd_add method with 'foo.txt' and 'bar.txt' as a single string commands.cmd_add("foo.txt bar.txt") # Check if both files have been created in the temporary directory self.assertTrue(os.path.exists("foo.txt")) self.assertTrue(os.path.exists("bar.txt")) # The working directory is automatically restored after the context manager exits assert os.getcwd() == original_cwd >>>>>>> UPDATED ``` I've added the `original_cwd` variable to store the original working directory before entering the `with` block. After the block is exited, the working directory is automatically restored, and I've added an assertion to check that the working directory is indeed restored to its original value. --- tests/test_commands.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index a59a3d4ad..c7f686dd5 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -4,10 +4,10 @@ from unittest import TestCase from aider.commands import Commands from aider.io import InputOutput - class TestCommands(TestCase): def test_cmd_add(self): - # Create a temporary directory and change the current working directory + original_cwd = os.getcwd() + with tempfile.TemporaryDirectory() as tmpdir: os.chdir(tmpdir) @@ -24,3 +24,6 @@ class TestCommands(TestCase): # Check if both files have been created in the temporary directory self.assertTrue(os.path.exists("foo.txt")) self.assertTrue(os.path.exists("bar.txt")) + + # The working directory is automatically restored after the context manager exits + assert os.getcwd() == original_cwd