From 22482deedfb1f2bbf4c99ea00e464e31d2bb718e Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 27 May 2023 04:36:14 -0700 Subject: [PATCH] aider: Added a new test to check if the `--no-auto-commits` flag is passed correctly to the `Coder` class. # Context: USER: add a test which runs main with --no-auto-commits and mocks Coder to see that it gets passed in correctly ASSISTANT: To add the test, we'll modify the `tests/test_main.py` file. We'll create a new test function called `test_main_no_auto_commits` and use the `unittest.mock.patch` to mock the `Coder` class. Inside the test function, we'll run the `main` function with the `--no-auto-commits` flag and check if the `auto_commits` attribute of the mocked `Coder` instance is set to `False`. Here's the change: ```python tests/test_main.py <<<<<<< ORIGINAL def test_main_with_empty_git_dir_new_file(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) subprocess.run(["git", "init"], cwd=temp_dir) pipe_input = create_input(StringIO("")) main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput()) pipe_input.close() self.assertTrue(os.path.exists("foo.txt")) ======= def test_main_with_empty_git_dir_new_file(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) subprocess.run(["git", "init"], cwd=temp_dir) pipe_input = create_input(StringIO("")) main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput()) pipe_input.close() self.assertTrue(os.path.exists("foo.txt")) def test_main_no_auto_commits(self): with unittest.mock.patch("aider.main.Coder") as MockCoder: with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) subprocess.run(["git", "init"], cwd=temp_dir) pipe_input = create_input(StringIO("")) main(["--no-auto-commits"], input=pipe_input, output=DummyOutput()) pipe_input.close() mock_coder_instance = MockCoder.return_value self.assertFalse(mock_coder_instance.auto_commits) >>>>>>> UPDATED ``` This will add the new test to check if the `--no-auto-commits` flag is passed correctly to the `Coder` class. --- tests/test_main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index a497c1d74..2dbe47b8f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -32,3 +32,15 @@ class TestMain(TestCase): main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput()) pipe_input.close() self.assertTrue(os.path.exists("foo.txt")) + + def test_main_no_auto_commits(self): + with unittest.mock.patch("aider.main.Coder") as MockCoder: + with tempfile.TemporaryDirectory() as temp_dir: + os.chdir(temp_dir) + subprocess.run(["git", "init"], cwd=temp_dir) + pipe_input = create_input(StringIO("")) + main(["--no-auto-commits"], input=pipe_input, output=DummyOutput()) + pipe_input.close() + + mock_coder_instance = MockCoder.return_value + self.assertFalse(mock_coder_instance.auto_commits)