Files
2026-01-11 12:05:37 +09:00

110 lines
3.8 KiB
Python

"""Tests for config module including project discovery utilities."""
from pathlib import Path
from deepagents_cli.config import _find_project_agent_md, _find_project_root
class TestProjectRootDetection:
"""Test project root detection via .git directory."""
def test_find_project_root_with_git(self, tmp_path: Path) -> None:
"""Test that project root is found when .git directory exists."""
# Create a mock project structure
project_root = tmp_path / "my-project"
project_root.mkdir()
git_dir = project_root / ".git"
git_dir.mkdir()
# Create a subdirectory to search from
subdir = project_root / "src" / "components"
subdir.mkdir(parents=True)
# Should find project root from subdirectory
result = _find_project_root(subdir)
assert result == project_root
def test_find_project_root_no_git(self, tmp_path: Path) -> None:
"""Test that None is returned when no .git directory exists."""
# Create directory without .git
no_git_dir = tmp_path / "no-git"
no_git_dir.mkdir()
result = _find_project_root(no_git_dir)
assert result is None
def test_find_project_root_nested_git(self, tmp_path: Path) -> None:
"""Test that nearest .git directory is found (not parent repos)."""
# Create nested git repos
outer_repo = tmp_path / "outer"
outer_repo.mkdir()
(outer_repo / ".git").mkdir()
inner_repo = outer_repo / "inner"
inner_repo.mkdir()
(inner_repo / ".git").mkdir()
# Should find inner repo, not outer
result = _find_project_root(inner_repo)
assert result == inner_repo
class TestProjectAgentMdFinding:
"""Test finding project-specific AGENTS.md files."""
def test_find_agent_md_in_deepagents_dir(self, tmp_path: Path) -> None:
"""Test finding AGENTS.md in .deepagents/ directory."""
project_root = tmp_path / "project"
project_root.mkdir()
# Create .deepagents/AGENTS.md
deepagents_dir = project_root / ".deepagents"
deepagents_dir.mkdir()
agent_md = deepagents_dir / "AGENTS.md"
agent_md.write_text("Project instructions")
result = _find_project_agent_md(project_root)
assert len(result) == 1
assert result[0] == agent_md
def test_find_agent_md_in_root(self, tmp_path: Path) -> None:
"""Test finding AGENTS.md in project root (fallback)."""
project_root = tmp_path / "project"
project_root.mkdir()
# Create root-level AGENTS.md (no .deepagents/)
agent_md = project_root / "AGENTS.md"
agent_md.write_text("Project instructions")
result = _find_project_agent_md(project_root)
assert len(result) == 1
assert result[0] == agent_md
def test_both_agent_md_files_combined(self, tmp_path: Path) -> None:
"""Test that both AGENTS.md files are returned when both exist."""
project_root = tmp_path / "project"
project_root.mkdir()
# Create both locations
deepagents_dir = project_root / ".deepagents"
deepagents_dir.mkdir()
deepagents_md = deepagents_dir / "AGENTS.md"
deepagents_md.write_text("In .deepagents/")
root_md = project_root / "AGENTS.md"
root_md.write_text("In root")
# Should return both, with .deepagents/ first
result = _find_project_agent_md(project_root)
assert len(result) == 2
assert result[0] == deepagents_md
assert result[1] == root_md
def test_find_agent_md_not_found(self, tmp_path: Path) -> None:
"""Test that empty list is returned when no AGENTS.md exists."""
project_root = tmp_path / "project"
project_root.mkdir()
result = _find_project_agent_md(project_root)
assert result == []