From 2cc87a4afe1fbcc889b30c11725588133ee91215 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 1 Jun 2023 08:54:15 -0700 Subject: [PATCH] aider: Added patch to setUp method and stored mock object as an instance variable. # Context: USER: how can i move the patch to setUp so that it is active for all tests? ASSISTANT: To move the patch to the `setUp` method and make it active for all tests, you can use the `patch` as a context manager in the `setUp` method and store the mock object as an instance variable. Then, in the `tearDown` method, you can stop the patch. Here's how to do it: ```python tests/test_main.py <<<<<<< ORIGINAL def setUp(self): 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 setUp(self): os.environ["OPENAI_API_KEY"] = "deadbeef" self.original_cwd = os.getcwd() self.tempdir = tempfile.mkdtemp() os.chdir(self.tempdir) self.patcher = patch("aider.main.Coder.check_model_availability") self.mock_check = self.patcher.start() self.mock_check.return_value = True def tearDown(self): os.chdir(self.original_cwd) shutil.rmtree(self.tempdir) self.patcher.stop() >>>>>>> UPDATED ``` Now the patch is active for all tests, and you can remove the `with` statement from the `test_main_with_empty_dir_no_files_on_command` method: ```python tests/test_main.py <<<<<<< ORIGINAL def test_main_with_empty_dir_no_files_on_command(self): with patch("aider.main.Coder.check_model_availability") as mock_check: mock_check.return_value = True pipe_input = create_input(StringIO("")) main([], input=pipe_input, output=DummyOutput()) pipe_input.close() ======= def test_main_with_empty_dir_no_files_on_command(self): pipe_input = create_input(StringIO("")) main([], input=pipe_input, output=DummyOutput()) pipe_input.close() >>>>>>> UPDATED ``` --- tests/test_main.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 491d75f51..097063208 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -18,18 +18,19 @@ class TestMain(TestCase): self.original_cwd = os.getcwd() self.tempdir = tempfile.mkdtemp() os.chdir(self.tempdir) + self.patcher = patch("aider.main.Coder.check_model_availability") + self.mock_check = self.patcher.start() + self.mock_check.return_value = True def tearDown(self): os.chdir(self.original_cwd) shutil.rmtree(self.tempdir) + self.patcher.stop() def test_main_with_empty_dir_no_files_on_command(self): - with patch("aider.main.Coder.check_model_availability") as mock_check: - mock_check.return_value = True - - pipe_input = create_input(StringIO("")) - main([], input=pipe_input, output=DummyOutput()) - pipe_input.close() + pipe_input = create_input(StringIO("")) + main([], input=pipe_input, output=DummyOutput()) + pipe_input.close() def test_main_with_empty_dir_new_file(self): pipe_input = create_input(StringIO(""))