move tempdir handling to setup/teardown

This commit is contained in:
Paul Gauthier 2023-05-28 22:42:25 -07:00
parent e50fe2469d
commit 48e9df2282

View file

@ -1,6 +1,7 @@
import os import os
import tempfile import tempfile
import subprocess import subprocess
import shutil
from unittest import TestCase from unittest import TestCase
from unittest.mock import patch from unittest.mock import patch
from aider.main import main from aider.main import main
@ -12,37 +13,35 @@ from prompt_toolkit.output import DummyOutput
class TestMain(TestCase): class TestMain(TestCase):
def setUp(self): def setUp(self):
os.environ["OPENAI_API_KEY"] = "deadbeef" os.environ["OPENAI_API_KEY"] = "deadbeef"
self.original_cwd = os.getcwd()
self.tempdir = tempfile.mkdtemp()
os.chdir(self.tempdir)
def tearDown(self):
os.chdir(self.original_cwd)
shutil.rmtree(self.tempdir)
def test_main_with_empty_dir_no_files_on_command(self): def test_main_with_empty_dir_no_files_on_command(self):
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
pipe_input = create_input(StringIO("")) pipe_input = create_input(StringIO(""))
main([], input=pipe_input, output=DummyOutput()) main([], input=pipe_input, output=DummyOutput())
pipe_input.close() pipe_input.close()
def test_main_with_empty_dir_new_file(self): def test_main_with_empty_dir_new_file(self):
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
pipe_input = create_input(StringIO("")) pipe_input = create_input(StringIO(""))
main(["foo.txt"], input=pipe_input, output=DummyOutput()) main(["foo.txt"], input=pipe_input, output=DummyOutput())
pipe_input.close() pipe_input.close()
self.assertTrue(os.path.exists("foo.txt")) self.assertTrue(os.path.exists("foo.txt"))
def test_main_with_empty_git_dir_new_file(self): def test_main_with_empty_git_dir_new_file(self):
with tempfile.TemporaryDirectory() as temp_dir: subprocess.run(["git", "init"])
os.chdir(temp_dir) subprocess.run(["git", "config", "user.email", "dummy@example.com"])
subprocess.run(["git", "init"], cwd=temp_dir) subprocess.run(["git", "config", "user.name", "Dummy User"])
subprocess.run(["git", "config", "user.email", "dummy@example.com"], cwd=temp_dir)
subprocess.run(["git", "config", "user.name", "Dummy User"], cwd=temp_dir)
pipe_input = create_input(StringIO("")) pipe_input = create_input(StringIO(""))
main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput()) main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
pipe_input.close() pipe_input.close()
self.assertTrue(os.path.exists("foo.txt")) self.assertTrue(os.path.exists("foo.txt"))
def test_main_args(self): def test_main_args(self):
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
with patch("aider.main.Coder") as MockCoder: with patch("aider.main.Coder") as MockCoder:
main(["--no-auto-commits"]) main(["--no-auto-commits"])
_, kwargs = MockCoder.call_args _, kwargs = MockCoder.call_args