Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
test(kit): strengthen ZIP export isolation regressions
myhloli4d86be567ebf
diff --git a/mineru/kit/common.py b/mineru/kit/common.pyindex 822509f1..2d1930da 100644--- a/mineru/kit/common.py+++ b/mineru/kit/common.py@@ -106,7 +106,7 @@ def build_remote_api_url(remote: bool, remote_url: str | None) -> str | None:def save_parse_result(result: ParseResult, dest: Path, format: KitFormat) -> None:- """Write a result, staging ZIP contents in an isolated directory cleaned up on errors."""+ """保存解析结果,ZIP 使用独立临时目录并在成功或异常退出时清理。"""dest.parent.mkdir(parents=True, exist_ok=True)if format == "markdown":_write_utf8_text(dest, result.markdown())@@ -115,6 +115,7 @@ def save_parse_result(result: ParseResult, dest: Path, format: KitFormat) -> Non_write_utf8_text(dest, result.to_json())returnif format == "zip":+ # 临时素材沿用输出盘,避免大文档占满系统临时盘;只清理本次导出创建的独立目录。with TemporaryDirectory(prefix=".mineru-kit-", dir=dest.parent) as tmp_dir_name:tmp_dir = Path(tmp_dir_name)writer = FileBasedDataWriter(tmp_dir_name)diff --git a/tests/unittest/test_kit_zip_export.py b/tests/unittest/test_kit_zip_export.pyindex eb88da9e..2b090a29 100644--- a/tests/unittest/test_kit_zip_export.py+++ b/tests/unittest/test_kit_zip_export.py@@ -1,4 +1,4 @@-"""Regression coverage for isolated ZIP export staging directories."""+"""验证 ZIP 导出的临时目录隔离、用户文件保护与异常清理。"""from __future__ import annotations@@ -19,6 +19,7 @@ from mineru.types import ImageBlock, ImageBodyBlock, MiddleJson, PageInfo@pytest.fixturedef image_bytes() -> bytes:+ """生成可逐字节核对的最小 PNG 素材。"""output = BytesIO()Image.new("RGB", (2, 2), "red").save(output, format="PNG")return output.getvalue()@@ -26,6 +27,7 @@ def image_bytes() -> bytes:@pytest.fixturedef parse_result(image_bytes: bytes) -> ParseResult:+ """构造包含内嵌图片的解析结果,覆盖素材落盘与引用。"""return ParseResult(MiddleJson(pages=[@@ -60,7 +62,7 @@ def test_zip_export_preserves_existing_sibling(image_bytes: bytes,sibling_kind: Literal["file", "directory"],) -> None:- """A same-stem hidden sibling must neither enter the ZIP nor be removed."""+ """与输出同名的隐藏文件或目录不得混入 ZIP,也不得被清理。"""dest = tmp_path / "report.zip"sibling = tmp_path / ".report"if sibling_kind == "directory":@@ -86,10 +88,15 @@ def test_zip_export_preserves_existing_sibling(def test_zip_export_cleans_up_after_save_failure(tmp_path: Path, parse_result: ParseResult, monkeypatch: pytest.MonkeyPatch) -> None:- """Partial result files must be removed when result serialization fails."""+ """结果保存失败时清理部分素材,同时保留已有用户目录和内容。"""dest = tmp_path / "report.zip"+ sibling = tmp_path / ".report"+ sibling.mkdir()+ sentinel = sibling / "private.txt"+ sentinel.write_bytes(b"unrelated user data")def fail_save(self: ParseResult, writer: DataWriter) -> None:+ """模拟写出部分图片后序列化失败。"""writer.write("images/partial.png", b"partial image")raise RuntimeError("save failed")@@ -98,16 +105,23 @@ def test_zip_export_cleans_up_after_save_failure(with pytest.raises(RuntimeError, match="save failed"):save_parse_result(parse_result, dest, "zip")- assert list(tmp_path.iterdir()) == []+ assert sentinel.read_bytes() == b"unrelated user data"+ assert set(tmp_path.iterdir()) == {sibling}+ assert set(sibling.iterdir()) == {sentinel}def test_zip_export_cleans_up_after_archive_failure(tmp_path: Path, parse_result: ParseResult, monkeypatch: pytest.MonkeyPatch) -> None:- """An archive write error must not leave the staged document or images behind."""+ """ZIP 写入失败时清理暂存文档和图片,同时保留已有用户目录和内容。"""dest = tmp_path / "report.zip"+ sibling = tmp_path / ".report"+ sibling.mkdir()+ sentinel = sibling / "private.txt"+ sentinel.write_bytes(b"unrelated user data")def fail_write(self: zipfile.ZipFile, filename: Path, arcname: str) -> None:+ """模拟归档写入过程中发生 I/O 错误。"""raise OSError("archive write failed")monkeypatch.setattr(zipfile.ZipFile, "write", fail_write)@@ -115,4 +129,6 @@ def test_zip_export_cleans_up_after_archive_failure(with pytest.raises(OSError, match="archive write failed"):save_parse_result(parse_result, dest, "zip")- assert not any(path != dest for path in tmp_path.iterdir())+ assert sentinel.read_bytes() == b"unrelated user data"+ assert set(tmp_path.iterdir()) == {sibling, dest}+ assert set(sibling.iterdir()) == {sentinel}