Repository navigation
Expand file tree
/
Copy pathtest_main.py
More file actions
221 lines (176 loc) · 8.46 KB
/
Copy pathtest_main.py
File metadata and controls
221 lines (176 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from unittest import TestCase
from unittest.mock import MagicMock, patch
import git
from prompt_toolkit.input import DummyInput
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
class TestMain(TestCase):
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, ignore_errors=True)
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):
main(["foo.txt", "--yes", "--no-git"], input=DummyInput(), output=DummyOutput())
self.assertTrue(os.path.exists("foo.txt"))
@patch("aider.repo.GitRepo.get_commit_message", return_value="mock commit message")
def test_main_with_empty_git_dir_new_file(self, _):
make_repo()
main(["--yes", "foo.txt"], input=DummyInput(), output=DummyOutput())
self.assertTrue(os.path.exists("foo.txt"))
@patch("aider.repo.GitRepo.get_commit_message", return_value="mock commit message")
def test_main_with_empty_git_dir_new_files(self, _):
make_repo()
main(["--yes", "foo.txt", "bar.txt"], input=DummyInput(), output=DummyOutput())
self.assertTrue(os.path.exists("foo.txt"))
self.assertTrue(os.path.exists("bar.txt"))
def test_main_with_dname_and_fname(self):
subdir = Path("subdir")
subdir.mkdir()
make_repo(str(subdir))
res = main(["subdir", "foo.txt"], input=DummyInput(), output=DummyOutput())
self.assertNotEqual(res, None)
@patch("aider.repo.GitRepo.get_commit_message", return_value="mock commit message")
def test_main_with_subdir_repo_fnames(self, _):
subdir = Path("subdir")
subdir.mkdir()
make_repo(str(subdir))
main(
["--yes", str(subdir / "foo.txt"), str(subdir / "bar.txt")],
input=DummyInput(),
output=DummyOutput(),
)
self.assertTrue((subdir / "foo.txt").exists())
self.assertTrue((subdir / "bar.txt").exists())
def test_main_with_git_config_yml(self):
make_repo()
Path(".aider.conf.yml").write_text("auto-commits: false\n")
with patch("aider.main.Coder.create") as MockCoder:
main(["--yes"], input=DummyInput(), output=DummyOutput())
_, kwargs = MockCoder.call_args
assert kwargs["auto_commits"] is False
Path(".aider.conf.yml").write_text("auto-commits: true\n")
with patch("aider.main.Coder.create") as MockCoder:
main([], input=DummyInput(), output=DummyOutput())
_, kwargs = MockCoder.call_args
assert kwargs["auto_commits"] is True
def test_main_with_empty_git_dir_new_subdir_file(self):
make_repo()
subdir = Path("subdir")
subdir.mkdir()
fname = subdir / "foo.txt"
fname.touch()
subprocess.run(["git", "add", str(subdir)])
subprocess.run(["git", "commit", "-m", "added"])
# This will throw a git error on windows if get_tracked_files doesn't
# properly convert git/posix/paths to git\posix\paths.
# Because aider will try and `git add` a file that's already in the repo.
main(["--yes", str(fname)], input=DummyInput(), output=DummyOutput())
def test_setup_git(self):
io = InputOutput(pretty=False, yes=True)
git_root = setup_git(None, io)
git_root = Path(git_root).resolve()
self.assertEqual(git_root, Path(self.tempdir).resolve())
self.assertTrue(git.Repo(self.tempdir))
gitignore = Path.cwd() / ".gitignore"
self.assertTrue(gitignore.exists())
self.assertEqual(".aider*", gitignore.read_text().splitlines()[0])
def test_check_gitignore(self):
with GitTemporaryDirectory():
os.environ["GIT_CONFIG_GLOBAL"] = "globalgitconfig"
io = InputOutput(pretty=False, yes=True)
cwd = Path.cwd()
gitignore = cwd / ".gitignore"
self.assertFalse(gitignore.exists())
check_gitignore(cwd, io)
self.assertTrue(gitignore.exists())
self.assertEqual(".aider*", gitignore.read_text().splitlines()[0])
gitignore.write_text("one\ntwo\n")
check_gitignore(cwd, io)
self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text())
del os.environ["GIT_CONFIG_GLOBAL"]
def test_main_git_ignore(self):
cwd = Path().cwd()
self.assertFalse((cwd / ".git").exists())
self.assertFalse((cwd / ".gitignore").exists())
with patch("aider.main.Coder.create"):
main(["--yes"], input=DummyInput())
self.assertTrue((cwd / ".git").exists())
self.assertTrue((cwd / ".gitignore").exists())
def test_main_args(self):
with patch("aider.main.Coder.create") as MockCoder:
# --yes will just ok the git repo without blocking on input
# following calls to main will see the new repo already
main(["--no-auto-commits", "--yes"], input=DummyInput())
_, kwargs = MockCoder.call_args
assert kwargs["auto_commits"] is False
with patch("aider.main.Coder.create") as MockCoder:
main(["--auto-commits"], input=DummyInput())
7750
_, kwargs = MockCoder.call_args
assert kwargs["auto_commits"] is True
with patch("aider.main.Coder.create") as MockCoder:
main([], input=DummyInput())
_, kwargs = MockCoder.call_args
assert kwargs["dirty_commits"] is True
assert kwargs["auto_commits"] is True
assert kwargs["pretty"] is True
with patch("aider.main.Coder.create") as MockCoder:
main(["--no-pretty"], input=DummyInput())
_, kwargs = MockCoder.call_args
assert kwargs["pretty"] is False
with patch("aider.main.Coder.create") as MockCoder:
main(["--pretty"], input=DummyInput())
_, kwargs = MockCoder.call_args
assert kwargs["pretty"] is True
with patch("aider.main.Coder.create") as MockCoder:
main(["--no-dirty-commits"], input=DummyInput())
_, kwargs = MockCoder.call_args
assert kwargs["dirty_commits"] is False
with patch("aider.main.Coder.create") as MockCoder:
main(["--dirty-commits"], input=DummyInput())
_, 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"
with GitTemporaryDirectory():
with patch("aider.main.Coder.create") as MockCoder: # noqa: F841
with patch("aider.main.InputOutput") as MockSend:
def side_effect(*args, **kwargs):
self.assertEqual(kwargs["encoding"], "iso-8859-15")
return MagicMock()
MockSend.side_effect = side_effect
main(["--yes", fname, "--encoding", "iso-8859-15"])
@patch("aider.main.InputOutput")
@patch("aider.coders.base_coder.Coder.run")
def test_main_message_adds_to_input_history(self, mock_run, MockInputOutput):
test_message = "test message"
mock_io_instance = MockInputOutput.return_value
main(["--message", test_message], input=DummyInput(), output=DummyOutput())
mock_io_instance.add_to_input_history.assert_called_once_with(test_message)