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

73 lines
3.1 KiB
Python

import json
from pathlib import Path
from mentor_pdf_export.cli import main
def test_self_check_writes_machine_readable_report(tmp_path):
report=tmp_path / "check.json"
assert main(["--self-check","--report",str(report)]) == 0
data=json.loads(report.read_text(encoding="utf-8"))
assert data["version"] == "0.1.0"
assert "engine_available" in data
def test_invalid_cli_export_returns_nonzero(tmp_path):
assert main(["--export",str(tmp_path / "missing.prj"),"--output",str(tmp_path / "pdf")]) != 0
def test_report_never_overwrites_project_even_for_self_check(project_factory):
project=project_factory()
before=project.read_bytes()
assert main(["--self-check","--report",str(project)]) == 2
assert project.read_bytes()==before
def test_report_inside_source_is_rejected_before_export(project_factory,tmp_path,monkeypatch):
from mentor_pdf_export import cli
from mentor_pdf_export.exporter import Exporter
from pypdf import PdfWriter
project=project_factory()
protected=project.parent/"settings.json";protected.write_text("original")
engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake")
def runner(args,*rest):
writer=PdfWriter();writer.add_blank_page(width=500,height=400)
with Path(args[args.index("-a")+1]).open("wb") as out:writer.write(out)
return 0
monkeypatch.setattr(cli,"Exporter",lambda engine,runs:Exporter(engine,runs,runner=runner))
rc=main(["--export",str(project),"--engine",str(engine),"--output",str(tmp_path/"pdfs"),"--runs-root",str(tmp_path/"runs"),"--report",str(protected)])
assert rc==2
assert protected.read_text()=="original"
assert not (tmp_path/"pdfs").exists()
def test_queue_destinations_protect_every_source_before_first_export(project_factory,tmp_path,monkeypatch):
from mentor_pdf_export import cli
from mentor_pdf_export.exporter import Exporter
from mentor_pdf_export.workspace import fingerprint
from pypdf import PdfWriter
a=project_factory("a");b=project_factory("b")
engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake")
before=fingerprint(b.parent)
def runner(args,*rest):
writer=PdfWriter();writer.add_blank_page(width=500,height=400)
with Path(args[args.index("-a")+1]).open("wb") as out:writer.write(out)
return 0
monkeypatch.setattr(cli,"Exporter",lambda engine,runs:Exporter(engine,runs,runner=runner))
rc=main(["--export",str(a),str(b),"--engine",str(engine),"--output",str(b.parent/"database"),"--runs-root",str(tmp_path/"runs")])
assert rc==2
assert fingerprint(b.parent)==before
assert not (tmp_path/"runs").exists()
def test_self_check_cannot_overwrite_unowned_json(tmp_path):
report=tmp_path/"critical.json";report.write_text('{"user_setting":123}')
before=report.read_bytes()
assert main(["--self-check","--report",str(report)]) == 2
assert report.read_bytes()==before
def test_self_check_can_refresh_its_own_report(tmp_path):
report=tmp_path/"self-check.json"
assert main(["--self-check","--report",str(report)]) == 0
assert main(["--self-check","--report",str(report)]) == 0