Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat(parse): enhance output path validation and add tests for single and multi-file scenarios

myhloliee87e8e6911e
diff --git a/mineru/kit/common.py b/mineru/kit/common.py
index 771390a9..9fe6e302 100644
--- a/mineru/kit/common.py
+++ b/mineru/kit/common.py
@@ -66,24 +66,19 @@ def resolve_single_output_path(source: Path, output: Path, format: KitFormat) ->
def resolve_batch_output_paths(paths: list[Path], output: Path, format: KitFormat) -> dict[Path, Path]:
if any(path.parent == path for path in paths):
raise ValueError(t("Invalid input path."))
- multi_input = len(paths) > 1
- has_directory_input = False
- if multi_input or has_directory_input:
- if is_output_path_file_like(output):
- raise ValueError(t("When input is multiple files or directories, --output must be a directory path."))
-
- output_dir = output
- if output.exists() and output.is_file():
+ if len(paths) > 1 and is_output_path_file_like(output):
raise ValueError(t("When input is multiple files or directories, --output must be a directory path."))
destinations: dict[Path, Path] = {}
seen: dict[Path, Path] = {}
for source in paths:
dest = (
- resolve_single_output_path(source, output_dir, format)
+ resolve_single_output_path(source, output, format)
if len(paths) == 1
- else output_dir / f"{source.stem}{OUTPUT_FILE_SUFFIXES[format]}"
+ else output / f"{source.stem}{OUTPUT_FILE_SUFFIXES[format]}"
)
+ if dest.resolve() == source.resolve():
+ raise ValueError(t("Output path must not be the same as the input file: {path}", path=source))
existing = seen.get(dest)
if existing is not None:
raise ValueError(
diff --git a/mineru/utils/translations.py b/mineru/utils/translations.py
index 667957ef..418412b7 100644
--- a/mineru/utils/translations.py
+++ b/mineru/utils/translations.py
@@ -453,6 +453,7 @@ ZH_MESSAGES: dict[str, str] = {
"When input is multiple files or directories, --output must be a directory path.": (
"当输入为多个文件或目录时,--output 必须是目录路径。"
),
+ "Output path must not be the same as the input file: {path}": "输出路径不能与输入文件相同: {path}",
"Failed to parse {path}: {error}": "解析 {path} 失败: {error}",
"Parsed {count} input(s).": "已解析 {count} 个输入。",
# ------------------------------------------------------------ kit webui
diff --git a/tests/unittest/test_kit_commands.py b/tests/unittest/test_kit_commands.py
index 6870c221..7060850e 100644
--- a/tests/unittest/test_kit_commands.py
+++ b/tests/unittest/test_kit_commands.py
@@ -900,6 +900,64 @@ def test_parse_single_file_markdown(monkeypatch: Any, tmp_path: Path) -> None:
assert output.read_text(encoding="utf-8") == "# demo\n"
+def test_parse_single_file_overwrites_existing_output(monkeypatch: Any, tmp_path: Path) -> None:
+ """单文件输入时,已存在的输出文件应被覆盖而非误报批量输出错误。"""
+ source = tmp_path / "demo.pdf"
+ output = tmp_path / "out.md"
+ source.write_bytes(b"%PDF-1.7\n")
+ output.write_text("stale", encoding="utf-8")
+
+ class _Result:
+ def markdown(self) -> str:
+ return "# demo\n"
+
+ def to_json(self) -> str:
+ return '{"pages":[]}'
+
+ def images(self) -> dict[str, bytes]:
+ return {}
+
+ def save(self, writer: Any) -> None:
+ writer.write_string("markdown.md", self.markdown())
+ writer.write_string("middle_json.json", self.to_json())
+
+ monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
+
+ result = runner.invoke(app, ["parse", str(source), "-o", str(output)])
+
+ assert result.exit_code == 0
+ assert "multiple files" not in " ".join(result.output.split())
+ assert output.read_text(encoding="utf-8") == "# demo\n"
+
+
+def test_parse_rejects_output_path_equal_to_input(tmp_path: Path) -> None:
+ """-o 指向输入文件本身时应在解析前报错,不得覆盖输入。"""
+ source = tmp_path / "demo.pdf"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ result = runner.invoke(app, ["parse", str(source), "-o", str(source)])
+
+ assert result.exit_code == 1
+ assert "must not be the same as the input file" in " ".join(result.output.split())
+ assert source.read_bytes() == b"%PDF-1.7\n"
+
+
+def test_parse_multi_input_rejects_existing_suffixless_output_file(tmp_path: Path) -> None:
+ """多文件输入指向已存在的无后缀输出文件时,仍要求 --output 为目录路径。"""
+ first = tmp_path / "a.pdf"
+ second = tmp_path / "b.pdf"
+ output = tmp_path / "out"
+ first.write_bytes(b"%PDF-1.7\n")
+ second.write_bytes(b"%PDF-1.7\n")
+ output.write_text("existing", encoding="utf-8")
+
+ result = runner.invoke(app, ["parse", str(first), str(second), "-o", str(output)])
+
+ assert result.exit_code == 1
+ assert "directory path" in " ".join(result.output.split())
+ assert output.read_text(encoding="utf-8") == "existing"
+
+
@pytest.mark.parametrize(
"backend", ["pipeline", "hybrid-engine", "hybrid-auto-engine", "flash", "vlm-auto-engine", "hybrid-http-client"]
)