From dd6a7964b6f34e4bfa63d172b48c38fce0270b36 Mon Sep 17 00:00:00 2001 From: "John-Mason P. Shackelford" Date: Tue, 18 Jun 2024 11:39:26 -0400 Subject: [PATCH] tempdirs in test_main now cleanup without windows errors --- aider/tests/test_main.py | 6 +++--- aider/utils.py | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/aider/tests/test_main.py b/aider/tests/test_main.py index 83e0d1234..b576d3754 100644 --- a/aider/tests/test_main.py +++ b/aider/tests/test_main.py @@ -13,7 +13,7 @@ from prompt_toolkit.output import DummyOutput from aider.dump import dump # noqa: F401 from aider.io import InputOutput from aider.main import check_gitignore, main, setup_git -from aider.utils import GitTemporaryDirectory, make_repo +from aider.utils import GitTemporaryDirectory, IgnorantTemporaryDirectory, make_repo class TestMain(TestCase): @@ -21,7 +21,7 @@ class TestMain(TestCase): self.original_env = os.environ.copy() os.environ["OPENAI_API_KEY"] = "deadbeef" self.original_cwd = os.getcwd() - self.tempdir_obj = tempfile.TemporaryDirectory() + self.tempdir_obj = IgnorantTemporaryDirectory() self.tempdir = self.tempdir_obj.name os.chdir(self.tempdir) @@ -34,7 +34,7 @@ class TestMain(TestCase): def test_main_with_empty_dir_no_files_on_command(self): main(["--no-git"], input=DummyInput(), output=DummyOutput()) - def test_main_with_empty_dir_new_file(self): + def test_main_with_emptqy_dir_new_file(self): main(["foo.txt", "--yes", "--no-git"], input=DummyInput(), output=DummyOutput()) self.assertTrue(os.path.exists("foo.txt")) diff --git a/aider/utils.py b/aider/utils.py index 7636eb119..6d097fbe7 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -17,11 +17,17 @@ class IgnorantTemporaryDirectory: return self.temp_dir.__enter__() def __exit__(self, exc_type, exc_val, exc_tb): + self.cleanup() + + def cleanup(self): try: - self.temp_dir.__exit__(exc_type, exc_val, exc_tb) + self.temp_dir.cleanup() except (OSError, PermissionError): pass # Ignore errors (Windows) + def __getattr__(self, item): + return getattr(self.temp_dir, item) + class ChdirTemporaryDirectory(IgnorantTemporaryDirectory): def __init__(self):