Added GitTemporaryDirectory

This commit is contained in:
Paul Gauthier 2023-07-16 09:39:07 -03:00
parent 86cea01434
commit a9cca05680
2 changed files with 59 additions and 30 deletions

View file

@ -12,6 +12,7 @@ from aider import models
from aider.coders import Coder from aider.coders import Coder
from aider.dump import dump # noqa: F401 from aider.dump import dump # noqa: F401
from aider.io import InputOutput from aider.io import InputOutput
from tests.utils import GitTemporaryDirectory
class TestCoder(unittest.TestCase): class TestCoder(unittest.TestCase):
@ -95,11 +96,9 @@ class TestCoder(unittest.TestCase):
self.assertEqual(coder.abs_fnames, expected_files) self.assertEqual(coder.abs_fnames, expected_files)
def test_check_for_ambiguous_filename_mentions_of_longer_paths(self): def test_check_for_ambiguous_filename_mentions_of_longer_paths(self):
# Mock the IO object with GitTemporaryDirectory():
mock_io = MagicMock() io = InputOutput(pretty=False, yes=True)
coder = Coder.create(models.GPT4, None, io)
# Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, mock_io)
fname = Path("file1.txt") fname = Path("file1.txt")
fname.touch() fname.touch()
@ -118,11 +117,9 @@ class TestCoder(unittest.TestCase):
self.assertEqual(coder.abs_fnames, set([str(fname.resolve())])) self.assertEqual(coder.abs_fnames, set([str(fname.resolve())]))
def test_check_for_subdir_mention(self): def test_check_for_subdir_mention(self):
# Mock the IO object with GitTemporaryDirectory():
mock_io = MagicMock() io = InputOutput(pretty=False, yes=True)
coder = Coder.create(models.GPT4, None, io)
# Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, mock_io)
fname = Path("other") / "file1.txt" fname = Path("other") / "file1.txt"
fname.parent.mkdir(parents=True, exist_ok=True) fname.parent.mkdir(parents=True, exist_ok=True)
@ -132,6 +129,7 @@ class TestCoder(unittest.TestCase):
mock.return_value = set([str(fname)]) mock.return_value = set([str(fname)])
coder.get_tracked_files = mock coder.get_tracked_files = mock
dump(fname)
# Call the check_for_file_mentions method # Call the check_for_file_mentions method
coder.check_for_file_mentions(f"Please check `{fname}`") coder.check_for_file_mentions(f"Please check `{fname}`")

View file

@ -1,5 +1,10 @@
import os
import tempfile import tempfile
import git
from aider.dump import dump # noqa: F401
class IgnorantTemporaryDirectory: class IgnorantTemporaryDirectory:
def __init__(self): def __init__(self):
@ -13,3 +18,29 @@ class IgnorantTemporaryDirectory:
self.temp_dir.__exit__(exc_type, exc_val, exc_tb) self.temp_dir.__exit__(exc_type, exc_val, exc_tb)
except OSError: except OSError:
pass # Ignore errors (Windows) pass # Ignore errors (Windows)
class ChdirTemporaryDirectory(IgnorantTemporaryDirectory):
def __init__(self):
self.cwd = os.getcwd()
super().__init__()
def __enter__(self):
res = super().__enter__()
os.chdir(self.temp_dir.name)
return res
def __exit__(self, exc_type, exc_val, exc_tb):
os.chdir(self.cwd)
super().__exit__(exc_type, exc_val, exc_tb)
class GitTemporaryDirectory(ChdirTemporaryDirectory):
def __enter__(self):
res = super().__enter__()
repo = git.Repo.init()
repo.config_writer().set_value("user", "name", "Test User").release()
repo.config_writer().set_value("user", "email", "testuser@example.com").release()
return res