Files
mentor-pdf-exporter/tests/test_exporter.py
T

165 lines
7.1 KiB
Python

from pathlib import Path
import threading
import pytest
from pypdf import PdfWriter
from mentor_pdf_export.models import ExportOptions
from mentor_pdf_export.scanner import read_project
from mentor_pdf_export.validation import validate_pdf
from mentor_pdf_export.exporter import Exporter, output_path
def pdf(path):
writer = PdfWriter()
writer.add_blank_page(width=500, height=400)
with Path(path).open("wb") as f:
writer.write(f)
def fake_runner(args, cwd, log_dir, cancel, timeout, on_event=None):
pdf(args[args.index("-a") + 1])
return 0
def test_validate_pdf(tmp_path):
file = tmp_path / "a.pdf"; pdf(file)
assert validate_pdf(file).pages == 1
file.write_bytes(b"not a pdf")
with pytest.raises(ValueError):
validate_pdf(file)
def test_invalid_zero_page_pdf(tmp_path):
file = tmp_path / "empty.pdf"
with file.open("wb") as f:
PdfWriter().write(f)
with pytest.raises(ValueError, match="页"):
validate_pdf(file)
def test_success_and_json_report(project_factory, tmp_path):
info = read_project(project_factory())
exporter = Exporter(Path("fake.exe"), tmp_path / "runs", runner=fake_runner)
result = exporter.export(info, tmp_path / "pdfs", ExportOptions(), threading.Event())
assert result.status == "success" and result.pages == 1
assert Path(result.output).is_file()
assert (Path(result.run_dir) / "result.json").is_file()
def test_old_output_preserved_on_failed_export(project_factory, tmp_path):
info = read_project(project_factory())
output = output_path(info, tmp_path / "pdfs"); output.parent.mkdir(); output.write_bytes(b"previous")
def failure(*args):
return 2
result = Exporter(Path("fake.exe"), tmp_path / "runs", runner=failure).export(info, output.parent, ExportOptions(overwrite=True), threading.Event())
assert result.status == "failed"
assert output.read_bytes() == b"previous"
def test_existing_output_needs_explicit_overwrite(project_factory, tmp_path):
info = read_project(project_factory())
output = output_path(info, tmp_path / "pdfs"); output.parent.mkdir(); output.write_bytes(b"previous")
result = Exporter(Path("fake.exe"), tmp_path / "runs", runner=fake_runner).export(info, output.parent, ExportOptions(), threading.Event())
assert result.status == "skipped"
assert output.read_bytes() == b"previous"
def test_failure_not_published_even_when_file_exists(project_factory, tmp_path):
info = read_project(project_factory())
def bad(args, *rest):
pdf(args[args.index("-a") + 1]); return 9
result = Exporter(Path("fake.exe"), tmp_path / "runs", runner=bad).export(info, tmp_path / "pdfs", ExportOptions(), threading.Event())
assert result.status == "failed"
assert not output_path(info, tmp_path / "pdfs").exists()
def test_source_change_never_claims_success(project_factory, tmp_path):
project = project_factory(); info = read_project(project)
def changing(args, *rest):
pdf(args[args.index("-a") + 1])
(project.parent / "database/icdb.dat").write_bytes(b"edited elsewhere")
return 0
result = Exporter(Path("fake.exe"), tmp_path / "runs", runner=changing).export(info, tmp_path / "pdfs", ExportOptions(), threading.Event())
assert result.status == "uncertain"
assert not output_path(info, tmp_path / "pdfs").exists()
def test_duplicate_project_names_have_distinct_outputs(project_factory, tmp_path):
a = read_project(project_factory("a", "same")); b = read_project(project_factory("b", "same"))
assert output_path(a, tmp_path / "out") != output_path(b, tmp_path / "out")
def test_cannot_output_inside_source(project_factory, tmp_path):
info = read_project(project_factory())
result = Exporter(Path("fake.exe"), tmp_path / "runs", runner=fake_runner).export(info, info.path.parent / "pdf", ExportOptions(), threading.Event())
assert result.status == "failed"
def test_pre_cancel_returns_cancelled(project_factory, tmp_path):
info = read_project(project_factory())
cancel = threading.Event(); cancel.set()
result = Exporter(Path("fake.exe"), tmp_path / "runs", runner=fake_runner).export(info, tmp_path / "pdfs", ExportOptions(), cancel)
assert result.status == "cancelled"
def test_final_source_check_is_before_publish(project_factory,tmp_path,monkeypatch):
import mentor_pdf_export.exporter as module
info=read_project(project_factory())
output=output_path(info,tmp_path/"pdfs");output.parent.mkdir();output.write_bytes(b"previous")
original=module.fingerprint
count=0
def checked(*args):
nonlocal count
count+=1
assert output.read_bytes()==b"previous", "final source validation happened after publication"
return original(*args)
monkeypatch.setattr(module,"fingerprint",checked)
result=Exporter(Path("fake.exe"),tmp_path/"runs",runner=fake_runner).export(info,output.parent,ExportOptions(overwrite=True),threading.Event())
assert result.status=="success"
def test_result_record_failure_never_overwrites_old_pdf(project_factory,tmp_path,monkeypatch):
import mentor_pdf_export.exporter as module
info=read_project(project_factory())
output=output_path(info,tmp_path/"pdfs");output.parent.mkdir();output.write_bytes(b"previous")
original=module.write_json
def fail_result(path,data):
if Path(path).name=="result.json":raise OSError("disk full")
return original(path,data)
monkeypatch.setattr(module,"write_json",fail_result)
result=Exporter(Path("fake.exe"),tmp_path/"runs",runner=fake_runner).export(info,output.parent,ExportOptions(overwrite=True),threading.Event())
assert result.status in {"failed","uncertain"}
assert output.read_bytes()==b"previous"
def test_post_publish_record_failure_returns_traceable_uncertain_result(project_factory,tmp_path,monkeypatch):
import mentor_pdf_export.exporter as module
info=read_project(project_factory())
original=module.write_json
count=0
def fail_final(path,data):
nonlocal count
if Path(path).name=="result.json":
count+=1
if count>=2:raise OSError("disk full after publish")
return original(path,data)
monkeypatch.setattr(module,"write_json",fail_final)
result=Exporter(Path("fake.exe"),tmp_path/"runs",runner=fake_runner).export(info,tmp_path/"pdfs",ExportOptions(),threading.Event())
assert result.status=="uncertain"
assert Path(result.output).is_file()
assert (Path(result.run_dir)/"result.json").is_file()
def test_staging_stat_failure_still_returns_and_records_result(project_factory,tmp_path,monkeypatch):
info=read_project(project_factory())
original=Path.exists
def denied(path):
if path.name.endswith(".partial.pdf"):raise OSError("staging stat denied")
return original(path)
monkeypatch.setattr(Path,"exists",denied)
def failure(args,*rest):
pdf(args[args.index("-a")+1]);return 2
result=Exporter(Path("fake.exe"),tmp_path/"runs",runner=failure).export(info,tmp_path/"pdfs",ExportOptions(),threading.Event())
assert result.status=="failed"
assert (Path(result.run_dir)/"result.json").is_file()
assert "暂存" in result.message