from pathlib import Path from threading import Event import json def test_batch_does_not_stop_after_one_project_failure(tmp_path): from mentorexport.cli import run_batch def exporter(project, installation, folder, shared, **options): if project["name"] == "坏工程": raise ValueError("无法读取数据库") return {"status": "success", "items": [{"key": "ascii", "status": "success", "outputs": []}]} projects = [{"name": name, "path": str(tmp_path / (name + ".prj"))} for name in ("坏工程", "好工程")] batch, code = run_batch(projects, [], {}, tmp_path / "out", tmp_path, exporter=exporter) state = json.loads((batch / "batch.json").read_text(encoding="utf-8")) assert code == 1 and state["state"] == "complete" assert [p["status"] for p in state["projects"]] == ["failed", "success"] assert (batch / "导出汇总.html").is_file() def test_pre_cancelled_batch_does_not_start_exporter(tmp_path): from mentorexport.cli import run_batch stop = Event() stop.set() def exporter(*args, **kwargs): raise AssertionError("cancelled batch started native work") batch, code = run_batch([{"name": "工程", "path": str(tmp_path / "a.prj")}], [], {}, tmp_path / "out", tmp_path, stop_event=stop, exporter=exporter) state = json.loads((batch / "batch.json").read_text(encoding="utf-8")) assert code == 130 and state["projects"][0]["status"] == "cancelled" assert (Path(state["projects"][0]["folder"]) / "export.json").is_file() def test_report_escapes_project_names_and_failure_details(tmp_path): from mentorexport.report import write_report record = {"state": "complete", "projects": [ {"project": {"name": "", "path": "a.prj"}, "folder": str(tmp_path / "a"), "status": "failed", "result": {"status": "failed", "reason": "", "items": []}}], "problems": []} write_report(tmp_path, record) html = (tmp_path / "导出汇总.html").read_text(encoding="utf-8") assert "