Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions deeptutor_cli/kb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ def _collect_documents(docs: list[str], docs_dir: Optional[str]) -> list[str]:

for doc in docs:
path = Path(doc).expanduser().resolve()
if path.exists() and path.is_file():
candidates.append(path)
if not path.exists():
raise typer.BadParameter(f"document does not exist: {path}")
if not path.is_file():
raise typer.BadParameter(f"document is not a file: {path}")
candidates.append(path)

if docs_dir:
base = Path(docs_dir).expanduser().resolve()
Expand Down
23 changes: 23 additions & 0 deletions tests/cli/test_kb_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from pathlib import Path

import pytest
import typer

from deeptutor_cli.kb import _collect_documents


Expand All @@ -18,3 +21,23 @@ def test_collect_documents_from_directory_matches_uppercase_extensions(tmp_path:
collected = [Path(path).name for path in _collect_documents([], str(docs_dir))]

assert collected == ["README.MD", "报告.PDF"]


def test_collect_documents_rejects_a_missing_doc_by_name(tmp_path: Path) -> None:
# A mistyped --doc used to vanish silently, leaving only "Provide at least one
# supported document" or, next to a valid file, a KB built without it.
good = tmp_path / "notes.md"
good.write_text("# Notes\n", encoding="utf-8")
missing = tmp_path / "typo.md"

for docs in ([str(missing)], [str(good), str(missing)]):
with pytest.raises(typer.BadParameter, match=r"does not exist: .*typo\.md"):
_collect_documents(docs, None)


def test_collect_documents_rejects_a_directory_passed_as_doc(tmp_path: Path) -> None:
folder = tmp_path / "chapter"
folder.mkdir()

with pytest.raises(typer.BadParameter, match="not a file"):
_collect_documents([str(folder)], None)
Loading