Keep exported files and continue batches despite item errors

This commit is contained in:
阿白叔 committed 2026-11-09 11:12:14 +08:00
1 parent 40fbb1893a
commit 5b974c9b45
10 files changed
+430 -100

No files matched your search

+64 -2
View File
@@ -12,8 +12,8 @@ def test_batch_does_not_stop_after_one_project_failure(tmp_path):
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 code == 0 and state["state"] == "complete"
assert [p["status"] for p in state["projects"]] == ["skipped", "success"]
assert (batch / "导出汇总.html").is_file()
@@ -69,3 +69,65 @@ def test_retry_cannot_claim_running_owner_has_exited():
import pytest
with pytest.raises(ValueError, match="运行"):
assert_retry_safe({"state": "running", "owner": process_identity(os.getpid())})
def test_uncertain_cleanup_does_not_cancel_following_project(tmp_path):
from mentorexport.cli import run_batch
visited = []
def exporter(project, installation, folder, shared, **options):
visited.append(project["name"])
return {"status": "skipped", "cleanup_uncertain": True, "items": []}
projects = [{"name": name, "path": str(tmp_path / (name + ".prj"))} for name in ("工程A", "工程B")]
stop = Event()
batch, code = run_batch(projects, [], {}, tmp_path / "out", tmp_path, exporter=exporter, stop_event=stop)
state = json.loads((batch / "batch.json").read_text(encoding="utf-8"))
assert visited == ["工程A", "工程B"]
assert code == 0 and state["state"] == "complete" and not stop.is_set()
assert state["warnings"]
def test_report_error_does_not_block_batch(tmp_path, monkeypatch):
from mentorexport import cli
visited = []
def broken_report(*args):
raise OSError("HTML file is locked")
def exporter(project, installation, folder, shared, **options):
visited.append(project["name"])
return {"status": "success", "items": []}
monkeypatch.setattr(cli, "write_report", broken_report)
projects = [{"name": name, "path": str(tmp_path / (name + ".prj"))} for name in ("工程A", "工程B")]
batch, code = cli.run_batch(projects, [], {}, tmp_path / "out", tmp_path, exporter=exporter)
record = json.loads((batch / "batch.json").read_text(encoding="utf-8"))
assert visited == ["工程A", "工程B"] and code == 0
assert any("HTML file is locked" in message for message in record["warnings"])
def test_scan_problems_and_no_projects_do_not_return_failed_batch(tmp_path):
from mentorexport.cli import run_batch
batch, code = run_batch([], [{"path": "missing.prj", "reason": "missing database"}], {}, tmp_path / "out", tmp_path)
state = json.loads((batch / "batch.json").read_text(encoding="utf-8"))
assert code == 0 and state["state"] == "complete" and state["problems"]
def test_export_continues_despite_help_preflight_failure(tmp_path, monkeypatch):
from mentorexport import cli
monkeypatch.setattr(cli, "runtime_check", lambda: [])
monkeypatch.setattr(cli, "_choose_installation", lambda *args: {"root": str(tmp_path), "version": "test"})
monkeypatch.setattr(cli, "preflight_installation", lambda *args: {"status": "failed", "reason": "help did not match"})
monkeypatch.setattr(cli, "_settings_paths", lambda: [])
monkeypatch.setattr(cli, "_save_settings", lambda value: None)
result = cli.main(["--input", str(tmp_path), "--output", str(tmp_path / "out"), "--yes"])
assert result == 0
assert list((tmp_path / "out").glob("批次_*/batch.json"))
def test_preflight_exception_is_a_warning_not_a_batch_blocker(tmp_path, monkeypatch):
from mentorexport import cli
monkeypatch.setattr(cli, "runtime_check", lambda: [])
monkeypatch.setattr(cli, "_choose_installation", lambda *args: {"root": str(tmp_path), "version": "test"})
def locked(*args): raise PermissionError("diagnostic directory locked")
monkeypatch.setattr(cli, "preflight_installation", locked)
monkeypatch.setattr(cli, "_settings_paths", lambda: [])
monkeypatch.setattr(cli, "_save_settings", lambda value: None)
assert cli.main(["--input", str(tmp_path), "--output", str(tmp_path / "out"), "--yes"]) == 0
assert list((tmp_path / "out").glob("批次_*/batch.json"))