Repository navigation
Expand file tree
/
Copy pathtest_repo.py
More file actions
296 lines (226 loc) · 10 KB
/
Copy pathtest_repo.py
File metadata and controls
296 lines (226 loc) · 10 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
import git
from aider.dump import dump # noqa: F401
from aider.io import InputOutput
from aider.models import Model
from aider.repo import GitRepo
from aider.utils import GitTemporaryDirectory
class TestRepo(unittest.TestCase):
def setUp(self):
self.GPT35 = Model("gpt-3.5-turbo")
def test_diffs_empty_repo(self):
with GitTemporaryDirectory():
repo = git.Repo()
# Add a change to the index
fname = Path("foo.txt")
fname.write_text("index\n")
repo.git.add(str(fname))
# Make a change in the working dir
fname.write_text("workingdir\n")
git_repo = GitRepo(InputOutput(), None, ".")
diffs = git_repo.get_diffs()
self.assertIn("index", diffs)
self.assertIn("workingdir", diffs)
def test_diffs_nonempty_repo(self):
with GitTemporaryDirectory():
repo = git.Repo()
fname = Path("foo.txt")
fname.touch()
repo.git.add(str(fname))
fname2 = Path("bar.txt")
fname2.touch()
repo.git.add(str(fname2))
repo.git.commit("-m", "initial")
fname.write_text("index\n")
repo.git.add(str(fname))
fname2.write_text("workingdir\n")
git_repo = GitRepo(InputOutput(), None, ".")
diffs = git_repo.get_diffs()
self.assertIn("index", diffs)
self.assertIn("workingdir", diffs)
def test_diffs_detached_head(self):
with GitTemporaryDirectory():
repo = git.Repo()
fname = Path("foo.txt")
fname.touch()
repo.git.add(str(fname))
repo.git.commit("-m", "foo")
fname2 = Path("bar.txt")
fname2.touch()
repo.git.add(str(fname2))
repo.git.commit("-m", "bar")
fname3 = Path("baz.txt")
fname3.touch()
repo.git.add(str(fname3))
repo.git.commit("-m", "baz")
repo.git.checkout("HEAD^")
fname.write_text("index\n")
repo.git.add(str(fname))
fname2.write_text("workingdir\n")
git_repo = GitRepo(InputOutput(), None, ".")
diffs = git_repo.get_diffs()
self.assertIn("index", diffs)
self.assertIn("workingdir", diffs)
def test_diffs_between_commits(self):
with GitTemporaryDirectory():
repo = git.Repo()
fname = Path("foo.txt")
fname.write_text("one\n")
repo.git.add(str(fname))
repo.git.commit("-m", "initial")
fname.write_text("two\n")
repo.git.add(str(fname))
repo.git.commit("-m", "second")
git_repo = GitRepo(InputOutput(), None, ".")
diffs = git_repo.diff_commits(False, "HEAD~1", "HEAD")
self.assertIn("two", diffs)
@patch("aider.repo.simple_send_with_retries")
def test_get_commit_message(self, mock_send):
mock_send.return_value = "a good commit message"
repo = GitRepo(InputOutput(), None, None, models=[self.GPT35])
# Call the get_commit_message method with dummy diff and context
result = repo.get_commit_message("dummy diff", "dummy context")
# Assert that the returned message is the expected one
self.assertEqual(result, "a good commit message")
@patch("aider.repo.simple_send_with_retries")
def test_get_commit_message_strip_quotes(self, mock_send):
mock_send.return_value = '"a good commit message"'
repo = GitRepo(InputOutput(), None, None, models=[self.GPT35])
# Call the get_commit_message method with dummy diff and context
result = repo.get_commit_message("dummy diff", "dummy context")
# Assert that the returned message is the expected one
self.assertEqual(result, "a good commit message")
@patch("aider.repo.simple_send_with_retries")
def test_get_commit_message_no_strip_unmatched_quotes(self, mock_send):
mock_send.return_value = 'a good "commit message"'
repo = GitRepo(InputOutput(), None, None, models=[self.GPT35])
# Call the get_commit_message method with dummy diff and context
result = repo.get_commit_message("dummy diff", "dummy context")
# Assert that the returned message is the expected one
self.assertEqual(result, 'a good "commit message"')
def test_get_tracked_files(self):
# Create a temporary directory
tempdir = Path(tempfile.mkdtemp())
# Initialize a git repository in the temporary directory and set user name and email
repo = git.Repo.init(tempdir)
repo.config_writer().set_value("user", "name", "Test User").release()
repo.config_writer().set_value("user", "email", "testuser@example.com").release()
# Create three empty files and add them to the git repository
filenames = ["README.md", "subdir/fänny.md", "systemüber/blick.md", 'file"with"quotes.txt']
created_files = []
for filename in filenames:
file_path = tempdir / filename
try:
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.touch()
repo.git.add(str(file_path))
created_files.append(Path(filename))
except OSError:
# windows won't allow files with quotes, that's ok
self.assertIn('"', filename)
self.assertEqual(os.name, "nt")
self.assertTrue(len(created_files) >= 3)
repo.git.commit("-m", "added")
tracked_files = GitRepo(InputOutput(), [tempdir], None).get_tracked_files()
# On windows, paths will come back \like\this, so normalize them back to Paths
tracked_files = [Path(fn) for fn in tracked_files]
# Assert that coder.get_tracked_files() returns the three filenames
self.assertEqual(set(tracked_files), set(created_files))
def test_get_tracked_files_with_new_staged_file(self):
with GitTemporaryDirectory():
# new repo
raw_repo = git.Repo()
# add it, but no commits at all in the raw_repo yet
fname = Path("new.txt")
fname.touch()
raw_repo.git.add(str(fname))
git_repo = GitRepo(InputOutput(), None, None)
# better be there
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
# commit it, better still be there
raw_repo.git.commit("-m", "new")
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
# new file, added but not committed
fname2 = Path("new2.txt")
fname2.touch()
raw_repo.git.add(str(fname2))
# both should be there
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
self.assertIn(str(fname2), fnames)
def test_get_tracked_files_with_aiderignore(self):
with GitTemporaryDirectory():
# new repo
raw_repo = git.Repo()
# add it, but no commits at all in the raw_repo yet
fname = Path("new.txt")
fname.touch()
raw_repo.git.add(str(fname))
aiderignore = Path(".aiderignore")
git_repo = GitRepo(InputOutput(), None, None, str(aiderignore))
# better be there
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
# commit it, better still be there
raw_repo.git.commit("-m", "new")
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
# new file, added but not committed
fname2 = Path("new2.txt")
fname2.touch()
raw_repo.git.add(str(fname2))
# both should be there
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
self.assertIn(str(fname2), fnames)
aiderignore.write_text("new.txt\n")
# new.txt should be gone!
fnames = git_repo.get_tracked_files()
self.assertNotIn(str(fname), fnames)
self.assertIn(str(fname2), fnames)
# This does not work in github actions?!
# The mtime doesn't change, even if I time.sleep(1)
# Before doing this write_text()!?
#
# aiderignore.write_text("new2.txt\n")
# new2.txt should be gone!
# fnames = git_repo.get_tracked_files()
# self.assertIn(str(fname), fnames)
# self.assertNotIn(str(fname2), fnames)
def test_get_tracked_files_from_subdir(self):
with GitTemporaryDirectory():
# new repo
raw_repo = git.Repo()
# add it, but no commits at all in the raw_repo yet
fname = Path("subdir/new.txt")
fname.parent.mkdir()
fname.touch()
raw_repo.git.add(str(fname))
os.chdir(fname.parent)
git_repo = GitRepo(InputOutput(), None, None)
# better be there
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
# commit it, better still be there
raw_repo.git.commit("-m", "new")
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
@patch("aider.repo.simple_send_with_retries")
def test_noop_commit(self, mock_send):
mock_send.return_value = '"a good commit message"'
with GitTemporaryDirectory():
# new repo
raw_repo = git.Repo()
# add it, but no commits at all in the raw_repo yet
fname = Path("file.txt")
fname.touch()
raw_repo.git.add(str(fname))
raw_repo.git.commit("-m", "new")
git_repo = GitRepo(InputOutput(), None, None)
git_repo.commit(fnames=[str(fname)])