|
4 | 4 | import time |
5 | 5 | import unittest |
6 | 6 | from pathlib import Path |
| 7 | +from unittest import mock |
7 | 8 |
|
8 | 9 | import git |
9 | 10 |
|
10 | 11 | from aider.dump import dump # noqa: F401 |
11 | 12 | from aider.io import InputOutput |
12 | 13 | from aider.models import Model |
13 | | -from aider.repomap import RepoMap |
| 14 | +from aider.repomap import RepoMap, Tag |
14 | 15 | from aider.utils import GitTemporaryDirectory, IgnorantTemporaryDirectory |
15 | 16 |
|
16 | 17 |
|
@@ -273,6 +274,47 @@ def test_get_repo_map_excludes_added_files(self): |
273 | 274 | # close the open cache files, so Windows won't error |
274 | 275 | del repo_map |
275 | 276 |
|
| 277 | + def test_get_ranked_tags_falls_back_without_scipy(self): |
| 278 | + with IgnorantTemporaryDirectory() as temp_dir: |
| 279 | + file1 = os.path.join(temp_dir, "file1.py") |
| 280 | + file2 = os.path.join(temp_dir, "file2.py") |
| 281 | + Path(file1).write_text("def alpha():\n return 1\n", encoding="utf-8") |
| 282 | + Path(file2).write_text("def beta():\n return alpha()\n", encoding="utf-8") |
| 283 | + |
| 284 | + io = InputOutput() |
| 285 | + repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io) |
| 286 | + |
| 287 | + rel1 = repo_map.get_rel_fname(file1) |
| 288 | + rel2 = repo_map.get_rel_fname(file2) |
| 289 | + |
| 290 | + def fake_get_tags(fname, rel_fname): |
| 291 | + if rel_fname == rel1: |
| 292 | + return [Tag(rel1, file1, 1, "alpha", "def")] |
| 293 | + if rel_fname == rel2: |
| 294 | + return [ |
| 295 | + Tag(rel2, file2, 1, "beta", "def"), |
| 296 | + Tag(rel2, file2, 2, "alpha", "ref"), |
| 297 | + ] |
| 298 | + return [] |
| 299 | + |
| 300 | + fallback_ranks = {rel1: 0.4, rel2: 0.6} |
| 301 | + |
| 302 | + with ( |
| 303 | + mock.patch.object(repo_map, "get_tags", side_effect=fake_get_tags), |
| 304 | + mock.patch("networkx.pagerank", side_effect=ModuleNotFoundError("No module named 'scipy'")), |
| 305 | + mock.patch( |
| 306 | + "networkx.algorithms.link_analysis.pagerank_alg._pagerank_python", |
| 307 | + return_value=fallback_ranks, |
| 308 | + ) as mock_fallback, |
| 309 | + ): |
| 310 | + ranked_tags = repo_map.get_ranked_tags([], [file1, file2], set(), set()) |
| 311 | + |
| 312 | + self.assertTrue(ranked_tags) |
| 313 | + mock_fallback.assert_called_once() |
| 314 | + |
| 315 | + # close the open cache files, so Windows won't error |
| 316 | + del repo_map |
| 317 | + |
276 | 318 |
|
277 | 319 | class TestRepoMapTypescript(unittest.TestCase): |
278 | 320 | def setUp(self): |
|
0 commit comments