mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 06:15:00 +00:00
aider: Refactored test_main_with_empty_dir_new_file
to use @patch
decorator to mock get_commit_message
method.
# Aider chat conversation: USER: @patch("aider.repo.AiderRepo.get_commit_message") is not patching it ASSISTANT: It seems like you're having trouble with the `@patch` decorator not correctly patching the `get_commit_message` method of the `AiderRepo` class in your tests. To help you with this, I need to understand the context better. Could you please provide more details about the issue? Are you getting any error messages? If so, could you share them? USER: in `test_main_with_empty_dir_new_file` i don't want `get_commit_message` to be actually called. just to return a mock value ASSISTANT: The `@patch` decorator from the `unittest.mock` module is used to replace the actual method with a mock method during testing. If `get_commit_message` is still being called, it might be due to the incorrect import path in the `@patch` decorator. In your case, the `@patch` decorator should be used as follows: ```python @patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!") ``` This will replace the `get_commit_message` method with a mock method that always returns "commit message!". Here is the updated `test_main_with_empty_dir_new_file` method: ```python tests/test_main.py <<<<<<< ORIGINAL @patch("aider.repo.AiderRepo.get_commit_message") def test_main_with_empty_dir_new_file(self, get_commit_message): get_commit_message.return_value = "commit message!" main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput()) self.assertTrue(os.path.exists("foo.txt")) ======= @patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!") def test_main_with_empty_dir_new_file(self, get_commit_message): main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput()) self.assertTrue(os.path.exists("foo.txt")) >>>>>>> UPDATED ``` This change should ensure that the actual `get_commit_message` method is not called during the test. Instead, the mock method will be used, returning the specified mock value.
This commit is contained in:
parent
e74bfc21d0
commit
6d22dd06f8
3 changed files with 15 additions and 12 deletions
|
@ -440,8 +440,6 @@ def main(args=None, input=None, output=None):
|
||||||
io.tool_output(repo_map)
|
io.tool_output(repo_map)
|
||||||
return
|
return
|
||||||
|
|
||||||
coder.dirty_commit()
|
|
||||||
|
|
||||||
if args.apply:
|
if args.apply:
|
||||||
content = io.read_text(args.apply)
|
content = io.read_text(args.apply)
|
||||||
if content is None:
|
if content is None:
|
||||||
|
@ -450,6 +448,9 @@ def main(args=None, input=None, output=None):
|
||||||
return
|
return
|
||||||
|
|
||||||
io.tool_output("Use /help to see in-chat commands, run with --help to see cmd line args")
|
io.tool_output("Use /help to see in-chat commands, run with --help to see cmd line args")
|
||||||
|
|
||||||
|
coder.dirty_commit()
|
||||||
|
|
||||||
if args.message:
|
if args.message:
|
||||||
io.tool_output()
|
io.tool_output()
|
||||||
coder.run(with_message=args.message)
|
coder.run(with_message=args.message)
|
||||||
|
|
|
@ -129,6 +129,15 @@ class AiderRepo:
|
||||||
return commit_message
|
return commit_message
|
||||||
|
|
||||||
def get_diffs(self, pretty, *args):
|
def get_diffs(self, pretty, *args):
|
||||||
|
try:
|
||||||
|
commits = self.repo.iter_commits(self.repo.active_branch)
|
||||||
|
current_branch_has_commits = any(commits)
|
||||||
|
except git.exc.GitCommandError:
|
||||||
|
current_branch_has_commits = False
|
||||||
|
|
||||||
|
if not current_branch_has_commits:
|
||||||
|
return ""
|
||||||
|
|
||||||
if pretty:
|
if pretty:
|
||||||
args = ["--color"] + list(args)
|
args = ["--color"] + list(args)
|
||||||
if not args:
|
if not args:
|
||||||
|
@ -138,14 +147,6 @@ class AiderRepo:
|
||||||
return diffs
|
return diffs
|
||||||
|
|
||||||
def show_diffs(self, pretty):
|
def show_diffs(self, pretty):
|
||||||
try:
|
|
||||||
current_branch_has_commits = any(self.repo.iter_commits(self.repo.active_branch))
|
|
||||||
except git.exc.GitCommandError:
|
|
||||||
current_branch_has_commits = False
|
|
||||||
|
|
||||||
if not current_branch_has_commits:
|
|
||||||
return ""
|
|
||||||
|
|
||||||
diffs = self.get_diffs(pretty)
|
diffs = self.get_diffs(pretty)
|
||||||
print(diffs)
|
print(diffs)
|
||||||
|
|
||||||
|
|
|
@ -34,8 +34,9 @@ class TestMain(TestCase):
|
||||||
def test_main_with_empty_dir_no_files_on_command(self):
|
def test_main_with_empty_dir_no_files_on_command(self):
|
||||||
main(["--no-git"], input=DummyInput(), output=DummyOutput())
|
main(["--no-git"], input=DummyInput(), output=DummyOutput())
|
||||||
|
|
||||||
def test_main_with_empty_dir_new_file(self):
|
@patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
|
||||||
main(["foo.txt", "--yes"], input=DummyInput(), output=DummyOutput())
|
def test_main_with_empty_dir_new_file(self, get_commit_message):
|
||||||
|
main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
|
||||||
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):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue