forked from Aider-AI/aider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_repomap.py
419 lines (340 loc) · 15.5 KB
/
test_repomap.py
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import difflib
import os
import re
import time
import unittest
from pathlib import Path
import git
from aider.dump import dump # noqa: F401
from aider.io import InputOutput
from aider.models import Model
from aider.repomap import RepoMap
from aider.utils import GitTemporaryDirectory, IgnorantTemporaryDirectory
class TestRepoMap(unittest.TestCase):
def setUp(self):
self.GPT35 = Model("gpt-3.5-turbo")
def test_get_repo_map(self):
# Create a temporary directory with sample files for testing
test_files = [
"test_file1.py",
"test_file2.py",
"test_file3.md",
"test_file4.json",
]
with IgnorantTemporaryDirectory() as temp_dir:
for file in test_files:
with open(os.path.join(temp_dir, file), "w") as f:
f.write("")
io = InputOutput()
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io)
other_files = [os.path.join(temp_dir, file) for file in test_files]
result = repo_map.get_repo_map([], other_files)
# Check if the result contains the expected tags map
self.assertIn("test_file1.py", result)
self.assertIn("test_file2.py", result)
self.assertIn("test_file3.md", result)
self.assertIn("test_file4.json", result)
# close the open cache files, so Windows won't error
del repo_map
def test_repo_map_refresh_files(self):
with GitTemporaryDirectory() as temp_dir:
repo = git.Repo(temp_dir)
# Create three source files with one function each
file1_content = "def function1():\n return 'Hello from file1'\n"
file2_content = "def function2():\n return 'Hello from file2'\n"
file3_content = "def function3():\n return 'Hello from file3'\n"
with open(os.path.join(temp_dir, "file1.py"), "w") as f:
f.write(file1_content)
with open(os.path.join(temp_dir, "file2.py"), "w") as f:
f.write(file2_content)
with open(os.path.join(temp_dir, "file3.py"), "w") as f:
f.write(file3_content)
# Add files to git
repo.index.add(["file1.py", "file2.py", "file3.py"])
repo.index.commit("Initial commit")
# Initialize RepoMap with refresh="files"
io = InputOutput()
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io, refresh="files")
other_files = [
os.path.join(temp_dir, "file1.py"),
os.path.join(temp_dir, "file2.py"),
os.path.join(temp_dir, "file3.py"),
]
# Get initial repo map
initial_map = repo_map.get_repo_map([], other_files)
dump(initial_map)
self.assertIn("function1", initial_map)
self.assertIn("function2", initial_map)
self.assertIn("function3", initial_map)
# Add a new function to file1.py
with open(os.path.join(temp_dir, "file1.py"), "a") as f:
f.write("\ndef functionNEW():\n return 'Hello NEW'\n")
# Get another repo map
second_map = repo_map.get_repo_map([], other_files)
self.assertEqual(
initial_map, second_map, "RepoMap should not change with refresh='files'"
)
other_files = [
os.path.join(temp_dir, "file1.py"),
os.path.join(temp_dir, "file2.py"),
]
second_map = repo_map.get_repo_map([], other_files)
self.assertIn("functionNEW", second_map)
# close the open cache files, so Windows won't error
del repo_map
del repo
def test_repo_map_refresh_auto(self):
with GitTemporaryDirectory() as temp_dir:
repo = git.Repo(temp_dir)
# Create two source files with one function each
file1_content = "def function1():\n return 'Hello from file1'\n"
file2_content = "def function2():\n return 'Hello from file2'\n"
with open(os.path.join(temp_dir, "file1.py"), "w") as f:
f.write(file1_content)
with open(os.path.join(temp_dir, "file2.py"), "w") as f:
f.write(file2_content)
# Add files to git
repo.index.add(["file1.py", "file2.py"])
repo.index.commit("Initial commit")
# Initialize RepoMap with refresh="auto"
io = InputOutput()
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io, refresh="auto")
chat_files = []
other_files = [os.path.join(temp_dir, "file1.py"), os.path.join(temp_dir, "file2.py")]
# Force the RepoMap computation to take more than 1 second
original_get_ranked_tags = repo_map.get_ranked_tags
def slow_get_ranked_tags(*args, **kwargs):
time.sleep(1.1) # Sleep for 1.1 seconds to ensure it's over 1 second
return original_get_ranked_tags(*args, **kwargs)
repo_map.get_ranked_tags = slow_get_ranked_tags
# Get initial repo map
initial_map = repo_map.get_repo_map(chat_files, other_files)
self.assertIn("function1", initial_map)
self.assertIn("function2", initial_map)
self.assertNotIn("functionNEW", initial_map)
# Add a new function to file1.py
with open(os.path.join(temp_dir, "file1.py"), "a") as f:
f.write("\ndef functionNEW():\n return 'Hello NEW'\n")
# Get another repo map without force_refresh
second_map = repo_map.get_repo_map(chat_files, other_files)
self.assertEqual(
initial_map, second_map, "RepoMap should not change without force_refresh"
)
# Get a new repo map with force_refresh
final_map = repo_map.get_repo_map(chat_files, other_files, force_refresh=True)
self.assertIn("functionNEW", final_map)
self.assertNotEqual(initial_map, final_map, "RepoMap should change with force_refresh")
# close the open cache files, so Windows won't error
del repo_map
del repo
def test_get_repo_map_with_identifiers(self):
# Create a temporary directory with a sample Python file containing identifiers
test_file1 = "test_file_with_identifiers.py"
file_content1 = """\
class MyClass:
def my_method(self, arg1, arg2):
return arg1 + arg2
def my_function(arg1, arg2):
return arg1 * arg2
"""
test_file2 = "test_file_import.py"
file_content2 = """\
from test_file_with_identifiers import MyClass
obj = MyClass()
print(obj.my_method(1, 2))
print(my_function(3, 4))
"""
test_file3 = "test_file_pass.py"
file_content3 = "pass"
with IgnorantTemporaryDirectory() as temp_dir:
with open(os.path.join(temp_dir, test_file1), "w") as f:
f.write(file_content1)
with open(os.path.join(temp_dir, test_file2), "w") as f:
f.write(file_content2)
with open(os.path.join(temp_dir, test_file3), "w") as f:
f.write(file_content3)
io = InputOutput()
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io)
other_files = [
os.path.join(temp_dir, test_file1),
os.path.join(temp_dir, test_file2),
os.path.join(temp_dir, test_file3),
]
result = repo_map.get_repo_map([], other_files)
# Check if the result contains the expected tags map with identifiers
self.assertIn("test_file_with_identifiers.py", result)
self.assertIn("MyClass", result)
self.assertIn("my_method", result)
self.assertIn("my_function", result)
self.assertIn("test_file_pass.py", result)
# close the open cache files, so Windows won't error
del repo_map
def test_get_repo_map_all_files(self):
test_files = [
"test_file0.py",
"test_file1.txt",
"test_file2.md",
"test_file3.json",
"test_file4.html",
"test_file5.css",
"test_file6.js",
]
with IgnorantTemporaryDirectory() as temp_dir:
for file in test_files:
with open(os.path.join(temp_dir, file), "w") as f:
f.write("")
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=InputOutput())
other_files = [os.path.join(temp_dir, file) for file in test_files]
result = repo_map.get_repo_map([], other_files)
dump(other_files)
dump(repr(result))
# Check if the result contains each specific file in the expected tags map without ctags
for file in test_files:
self.assertIn(file, result)
# close the open cache files, so Windows won't error
del repo_map
def test_get_repo_map_excludes_added_files(self):
# Create a temporary directory with sample files for testing
test_files = [
"test_file1.py",
"test_file2.py",
"test_file3.md",
"test_file4.json",
]
with IgnorantTemporaryDirectory() as temp_dir:
for file in test_files:
with open(os.path.join(temp_dir, file), "w") as f:
f.write("def foo(): pass\n")
io = InputOutput()
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io)
test_files = [os.path.join(temp_dir, file) for file in test_files]
result = repo_map.get_repo_map(test_files[:2], test_files[2:])
dump(result)
# Check if the result contains the expected tags map
self.assertNotIn("test_file1.py", result)
self.assertNotIn("test_file2.py", result)
self.assertIn("test_file3.md", result)
self.assertIn("test_file4.json", result)
# close the open cache files, so Windows won't error
del repo_map
class TestRepoMapTypescript(unittest.TestCase):
def setUp(self):
self.GPT35 = Model("gpt-3.5-turbo")
class TestRepoMapAllLanguages(unittest.TestCase):
def setUp(self):
self.GPT35 = Model("gpt-3.5-turbo")
def test_get_repo_map_all_languages(self):
language_files = {
"c": ("c", "main"),
"cpp": ("cpp", "main"),
"elixir": ("ex", "Greeter"),
"java": ("java", "Greeting"),
"javascript": ("js", "Person"),
"kotlin": ("kt", "Greeting"),
# "ocaml": ("ml", "Greeter"), # not supported in tsl-pack (yet?)
"php": ("php", "greet"),
"python": ("py", "Person"),
# "ql": ("ql", "greet"), # not supported in tsl-pack (yet?)
"ruby": ("rb", "greet"),
"rust": ("rs", "Person"),
"typescript": ("ts", "greet"),
"tsx": ("tsx", "UserProps"),
"csharp": ("cs", "IGreeter"),
"elisp": ("el", "greeter"),
"elm": ("elm", "Person"),
"go": ("go", "Greeter"),
"hcl": ("tf", "aws_vpc"),
}
fixtures_dir = Path(__file__).parent.parent / "fixtures" / "languages"
for lang, key_symbol in language_files.items():
# Get the fixture file path and name based on language
fixture_dir = fixtures_dir / lang
ext, key_symbol = language_files[lang]
filename = f"test.{ext}"
fixture_path = fixture_dir / filename
self.assertTrue(
fixture_path.exists(), f"Fixture file missing for {lang}: {fixture_path}"
)
# Read the fixture content
with open(fixture_path, "r", encoding="utf-8") as f:
content = f.read()
with GitTemporaryDirectory() as temp_dir:
test_file = os.path.join(temp_dir, filename)
with open(test_file, "w", encoding="utf-8") as f:
f.write(content)
io = InputOutput()
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io)
other_files = [filename]
result = repo_map.get_repo_map([], other_files)
dump(lang)
dump(result)
self.assertGreater(len(result.strip().splitlines()), 1)
# Check if the result contains all the expected files and symbols
self.assertIn(
filename, result, f"File for language {lang} not found in repo map: {result}"
)
self.assertIn(
key_symbol,
result,
(
f"Key symbol '{key_symbol}' for language {lang} not found in repo map:"
f" {result}"
),
)
# close the open cache files, so Windows won't error
del repo_map
def test_repo_map_sample_code_base(self):
# Path to the sample code base
sample_code_base = Path(__file__).parent.parent / "fixtures" / "sample-code-base"
# Path to the expected repo map file
expected_map_file = (
Path(__file__).parent.parent / "fixtures" / "sample-code-base-repo-map.txt"
)
# Ensure the paths exist
self.assertTrue(sample_code_base.exists(), "Sample code base directory not found")
self.assertTrue(expected_map_file.exists(), "Expected repo map file not found")
# Initialize RepoMap with the sample code base as root
io = InputOutput()
repomap_root = Path(__file__).parent.parent.parent
repo_map = RepoMap(
main_model=self.GPT35,
root=str(repomap_root),
io=io,
)
# Get all files in the sample code base
other_files = [str(f) for f in sample_code_base.rglob("*") if f.is_file()]
# Generate the repo map
generated_map_str = repo_map.get_repo_map([], other_files).strip()
# Read the expected map from the file using UTF-8 encoding
with open(expected_map_file, "r", encoding="utf-8") as f:
expected_map = f.read().strip()
# Normalize path separators for Windows
if os.name == "nt": # Check if running on Windows
expected_map = re.sub(
r"tests/fixtures/sample-code-base/([^:]+)",
r"tests\\fixtures\\sample-code-base\\\1",
expected_map,
)
generated_map_str = re.sub(
r"tests/fixtures/sample-code-base/([^:]+)",
r"tests\\fixtures\\sample-code-base\\\1",
generated_map_str,
)
# Compare the generated map with the expected map
if generated_map_str != expected_map:
# If they differ, show the differences and fail the test
diff = list(
difflib.unified_diff(
expected_map.splitlines(),
generated_map_str.splitlines(),
fromfile="expected",
tofile="generated",
lineterm="",
)
)
diff_str = "\n".join(diff)
self.fail(f"Generated map differs from expected map:\n{diff_str}")
# If we reach here, the maps are identical
self.assertEqual(generated_map_str, expected_map, "Generated map matches expected map")
if __name__ == "__main__":
unittest.main()