Files
mytoolkit/tests/test_templates_registry.py
T
Zhengshou Lai 06704acf60 test: add pytest suite — CLI smoke + config/templates/md_to_pdf units
- tests/conftest.py redirects MYTOOLKIT_HOME to a temp dir before imports
  so the module-level Config singleton never touches the real ~/.mytoolkit
- CLI smoke: every registered command answers --help with exit code 0
- unit tests: Config get/set/migrate, templates_registry override/fallback,
  md_to_pdf pure helpers (pandoc-dependent cases skipif-guarded)
- pyproject: dev dependency group (pytest) + pytest config; Makefile: make test
2026-07-18 11:45:40 +08:00

45 lines
1.3 KiB
Python

"""Tests for mytoolkit.templates_registry (external root override + fallback)."""
import click
import pytest
from mytoolkit import templates_registry as tr
@pytest.fixture(autouse=True)
def clean_registry(tmp_path, monkeypatch):
"""Point the registry config file at a temp location for every test."""
monkeypatch.setattr(tr, "CONFIG_PATH", tmp_path / "templates.json")
yield
def test_default_root_is_bundled_package():
root = tr.get_root()
assert (root / "md-to-pdf").is_dir()
assert (root / "md-to-docx").is_dir()
def test_set_root_overrides(tmp_path):
ext = tmp_path / "ext-templates"
(ext / "md-to-pdf").mkdir(parents=True)
(ext / "md-to-docx").mkdir()
assert tr.set_root(ext) == ext.resolve()
assert tr.get_root() == ext.resolve()
assert tr.get_md_to_pdf_root() == ext.resolve() / "md-to-pdf"
assert tr.get_md_to_docx_root() == ext.resolve() / "md-to-docx"
def test_invalid_registered_dir_falls_back_to_bundled(tmp_path):
tr.set_root(tmp_path / "does-not-exist")
root = tr.get_root()
assert (root / "md-to-pdf").is_dir()
assert root != tmp_path / "does-not-exist"
def test_missing_subdir_raises_usage_error(tmp_path):
ext = tmp_path / "ext-templates"
ext.mkdir() # no md-to-pdf inside
tr.set_root(ext)
with pytest.raises(click.UsageError):
tr.get_md_to_pdf_root()