Merge pull request #380 from VariousForks/message_as_file_with_unittest

This commit is contained in:
paul-gauthier 2023-12-05 15:03:57 -08:00 committed by GitHub
commit 5cd0efecee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -380,6 +380,12 @@ def main(argv=None, input=None, output=None, force_git_root=None):
metavar="COMMAND",
help="Specify a single message to send GPT, process reply then exit (disables chat mode)",
)
other_group.add_argument(
"--message-file",
"-f",
metavar="MESSAGE_FILE",
help="Specify a file containing the message to send GPT, process reply, then exit (disables chat mode)",
)
other_group.add_argument(
"--encoding",
default="utf-8",
@ -563,6 +569,17 @@ def main(argv=None, input=None, output=None, force_git_root=None):
io.add_to_input_history(args.message)
io.tool_output()
coder.run(with_message=args.message)
elif args.message_file:
try:
message_from_file = io.read_text(args.message_file)
io.tool_output()
coder.run(with_message=message_from_file)
except FileNotFoundError:
io.tool_error(f"Message file not found: {args.message_file}")
return 1
except IOError as e:
io.tool_error(f"Error reading message file: {e}")
return 1
else:
coder.run()

View file

@ -182,6 +182,19 @@ class TestMain(TestCase):
_, kwargs = MockCoder.call_args
assert kwargs["dirty_commits"] is True
def test_message_file_flag(self):
message_file_content = "This is a test message from a file."
message_file_path = tempfile.mktemp()
with open(message_file_path, 'w', encoding='utf-8') as message_file:
message_file.write(message_file_content)
with patch("aider.main.Coder.create") as MockCoder:
MockCoder.return_value.run = MagicMock()
main(["--yes", "--message-file", message_file_path], input=DummyInput(), output=DummyOutput())
MockCoder.return_value.run.assert_called_once_with(with_message=message_file_content)
os.remove(message_file_path)
def test_encodings_arg(self):
fname = "foo.py"