|
20 | 20 | from cocoindex_code.cli import app |
21 | 21 | from cocoindex_code.client import stop_daemon |
22 | 22 | from cocoindex_code.settings import ( |
| 23 | + _reset_db_path_mapping_cache, |
23 | 24 | default_project_settings, |
24 | 25 | find_parent_with_marker, |
25 | 26 | save_project_settings, |
@@ -580,6 +581,87 @@ def test_session_daemon_restart_missing_global_settings() -> None: |
580 | 581 | assert "User settings not found" in result.output |
581 | 582 |
|
582 | 583 |
|
| 584 | +# --------------------------------------------------------------------------- |
| 585 | +# DB path mapping tests |
| 586 | +# --------------------------------------------------------------------------- |
| 587 | + |
| 588 | + |
| 589 | +@pytest.fixture() |
| 590 | +def e2e_project_with_db_mapping() -> Iterator[tuple[Path, Path]]: |
| 591 | + """Set up a project with COCOINDEX_CODE_DB_PATH_MAPPING pointing to a separate db dir. |
| 592 | +
|
| 593 | + Yields (project_dir, db_base_dir). |
| 594 | + """ |
| 595 | + base_dir = Path(tempfile.mkdtemp(prefix="ccc_e2e_")) |
| 596 | + project_dir = base_dir / "workspace" / "myproject" |
| 597 | + project_dir.mkdir(parents=True) |
| 598 | + db_base_dir = base_dir / "db-files" |
| 599 | + db_base_dir.mkdir() |
| 600 | + |
| 601 | + (project_dir / "main.py").write_text(SAMPLE_MAIN_PY) |
| 602 | + (project_dir / ".git").mkdir() |
| 603 | + |
| 604 | + old_env = { |
| 605 | + k: os.environ.get(k) for k in ("COCOINDEX_CODE_DIR", "COCOINDEX_CODE_DB_PATH_MAPPING") |
| 606 | + } |
| 607 | + os.environ["COCOINDEX_CODE_DIR"] = str(base_dir) |
| 608 | + workspace = str(base_dir / "workspace") |
| 609 | + os.environ["COCOINDEX_CODE_DB_PATH_MAPPING"] = f"{workspace}={db_base_dir}" |
| 610 | + _reset_db_path_mapping_cache() |
| 611 | + old_cwd = os.getcwd() |
| 612 | + os.chdir(project_dir) |
| 613 | + |
| 614 | + try: |
| 615 | + yield project_dir, db_base_dir |
| 616 | + finally: |
| 617 | + os.chdir(project_dir) |
| 618 | + runner.invoke(app, ["reset", "--all", "-f"]) |
| 619 | + stop_daemon() |
| 620 | + os.chdir(old_cwd) |
| 621 | + _reset_db_path_mapping_cache() |
| 622 | + for k, v in old_env.items(): |
| 623 | + if v is None: |
| 624 | + os.environ.pop(k, None) |
| 625 | + else: |
| 626 | + os.environ[k] = v |
| 627 | + |
| 628 | + |
| 629 | +def test_session_db_path_mapping( |
| 630 | + e2e_project_with_db_mapping: tuple[Path, Path], |
| 631 | +) -> None: |
| 632 | + """Init → index → verify databases are in the mapped directory → search works.""" |
| 633 | + project_dir, db_base_dir = e2e_project_with_db_mapping |
| 634 | + mapped_db_dir = db_base_dir / "myproject" |
| 635 | + |
| 636 | + # Init |
| 637 | + result = runner.invoke(app, ["init"], catch_exceptions=False) |
| 638 | + assert result.exit_code == 0, result.output |
| 639 | + |
| 640 | + # Settings should be in the project dir, NOT the mapped dir |
| 641 | + assert (project_dir / ".cocoindex_code" / "settings.yml").exists() |
| 642 | + |
| 643 | + # Index |
| 644 | + result = runner.invoke(app, ["index"], catch_exceptions=False) |
| 645 | + assert result.exit_code == 0, result.output |
| 646 | + |
| 647 | + # Databases should be in the mapped directory |
| 648 | + assert (mapped_db_dir / "target_sqlite.db").exists() |
| 649 | + # Databases should NOT be in the project's .cocoindex_code dir |
| 650 | + assert not (project_dir / ".cocoindex_code" / "target_sqlite.db").exists() |
| 651 | + |
| 652 | + # Search should work |
| 653 | + result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False) |
| 654 | + assert result.exit_code == 0, result.output |
| 655 | + assert "main.py" in result.output |
| 656 | + |
| 657 | + # Reset should clean databases from the mapped dir |
| 658 | + result = runner.invoke(app, ["reset", "-f"], catch_exceptions=False) |
| 659 | + assert result.exit_code == 0 |
| 660 | + assert not (mapped_db_dir / "target_sqlite.db").exists() |
| 661 | + # Settings still in place |
| 662 | + assert (project_dir / ".cocoindex_code" / "settings.yml").exists() |
| 663 | + |
| 664 | + |
583 | 665 | # --------------------------------------------------------------------------- |
584 | 666 | # Unit tests (not session-based) |
585 | 667 | # --------------------------------------------------------------------------- |
|
0 commit comments