mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-21 21:04:59 +00:00

# 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.
29 lines
1,021 B
Python
29 lines
1,021 B
Python
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
|