Files

125 lines
4.4 KiB
Python

from __future__ import annotations
import zipfile
from pathlib import Path
from mdhelper.app import parse_arguments, project_and_initial_path, resolve_launch_path
from mdhelper.main_window import available_new_markdown_path
from mdhelper.docx_exporter import export_markdown_to_docx, resolve_pandoc_executable
from mdhelper.logging_config import setup_logging
from mdhelper.markdown_editor import convert_latex_display_delimiters
def test_parse_arguments_accepts_folder_alias() -> None:
args = parse_arguments(["--folder", "docs"])
assert args.project_root == "docs"
def test_resolve_launch_path_finds_relative_folder_name(tmp_path: Path, monkeypatch) -> None:
launch_root = tmp_path / "launcher"
docs_root = launch_root / "docs_project"
work_root = tmp_path / "work"
docs_root.mkdir(parents=True)
work_root.mkdir()
monkeypatch.chdir(work_root)
resolved = resolve_launch_path("docs_project", (Path.cwd(), launch_root))
assert resolved == docs_root.resolve()
def test_project_and_initial_path_uses_file_parent(tmp_path: Path) -> None:
markdown_file = tmp_path / "guide.md"
markdown_file.write_text("# Guide\n", encoding="utf-8")
project_root, initial_path = project_and_initial_path(markdown_file, None)
assert project_root == tmp_path
assert initial_path == markdown_file
def test_project_and_initial_path_uses_folder_directly(tmp_path: Path) -> None:
project_root, initial_path = project_and_initial_path(tmp_path, None)
assert project_root == tmp_path
assert initial_path is None
def test_convert_latex_display_delimiters_to_markdown() -> None:
source = "正文\n\\[\nx^2 + y^2 = z^2\n\\]\n行内公式 $x$ 保持不变\n"
converted, replacement_count = convert_latex_display_delimiters(source)
assert converted == "正文\n$$\nx^2 + y^2 = z^2\n$$\n行内公式 $x$ 保持不变\n"
assert replacement_count == 2
def test_available_new_markdown_path_avoids_existing_names(tmp_path: Path) -> None:
assert available_new_markdown_path(tmp_path).name == "新建文件.md"
(tmp_path / "新建文件.md").write_text("", encoding="utf-8")
(tmp_path / "新建文件 (2).md").write_text("", encoding="utf-8")
assert available_new_markdown_path(tmp_path).name == "新建文件 (3).md"
def test_export_markdown_to_docx_writes_word_document(tmp_path: Path) -> None:
output_path = tmp_path / "guide.docx"
export_markdown_to_docx(
"# 标题\n\n正文 **加粗** 和 `代码`。\n\n- 项目一\n- 项目二\n\n| A | B |\n| - | - |\n| 1 | 2 |\n",
output_path,
engine="builtin",
)
assert output_path.exists()
with zipfile.ZipFile(output_path) as package:
names = set(package.namelist())
document_xml = package.read("word/document.xml").decode("utf-8")
assert {"[Content_Types].xml", "_rels/.rels", "word/document.xml", "word/styles.xml"} <= names
assert "标题" in document_xml
assert "项目一" in document_xml
assert "<w:tbl>" in document_xml
def test_export_markdown_to_docx_python_docx_engine(tmp_path: Path) -> None:
output_path = tmp_path / "python_docx.docx"
export_markdown_to_docx("# 标题\n\n正文\n", output_path, engine="python-docx")
assert output_path.exists()
with zipfile.ZipFile(output_path) as package:
document_xml = package.read("word/document.xml").decode("utf-8")
assert "标题" in document_xml
def test_resolve_pandoc_executable_uses_bundled_copy() -> None:
pandoc_path = resolve_pandoc_executable()
assert pandoc_path is not None
assert pandoc_path.name == "pandoc.exe"
assert pandoc_path.parent.name == "pandoc"
def test_export_markdown_to_docx_pandoc_engine(tmp_path: Path) -> None:
output_path = tmp_path / "pandoc.docx"
source_path = tmp_path / "source.md"
source_path.write_text("# 标题\n\n正文\n", encoding="utf-8")
export_markdown_to_docx(source_path.read_text(encoding="utf-8"), output_path, engine="pandoc", source_path=source_path)
assert output_path.exists()
with zipfile.ZipFile(output_path) as package:
assert "word/document.xml" in package.namelist()
def test_setup_logging_uses_configured_writable_user_directory(tmp_path: Path, monkeypatch) -> None:
log_dir = tmp_path / "user_logs"
monkeypatch.setenv("MDHELPER_LOG_DIR", str(log_dir))
log_path = setup_logging(tmp_path / "install_root")
assert log_path == log_dir / "mdhelper.log"
assert log_path.exists()