Add portable Mentor schematic batch exporter
This commit is contained in:
commit
40fbb1893a
26 files changed
+3622
No files matched your search
@@ -0,0 +1,71 @@
|
||||
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": "<script>alert(1)</script>", "path": "a.prj"},
|
||||
"folder": str(tmp_path / "a"), "status": "failed",
|
||||
"result": {"status": "failed", "reason": "<img src=x>", "items": []}}], "problems": []}
|
||||
write_report(tmp_path, record)
|
||||
html = (tmp_path / "导出汇总.html").read_text(encoding="utf-8")
|
||||
assert "<script>" not in html and "<img src=x>" not in html
|
||||
assert "<script>" in html and "<img src=x>" in html
|
||||
|
||||
|
||||
def test_retry_loads_export_receipt_written_before_batch_crash(tmp_path):
|
||||
from mentorexport.cli import previous_results
|
||||
attempt = tmp_path / "工程"
|
||||
attempt.mkdir()
|
||||
(attempt / "export.json").write_text(json.dumps({"status": "partial", "items": [{"key": "csv", "status": "success"}]}))
|
||||
record = {"projects": [{"project": {"path": "a.prj"}, "folder": str(attempt), "status": "running"}]}
|
||||
values = previous_results(record)
|
||||
assert values["a.prj"]["items"][0]["key"] == "csv"
|
||||
|
||||
|
||||
def test_output_cannot_write_inside_source_database_or_library(tmp_path):
|
||||
from mentorexport.cli import validate_output
|
||||
import pytest
|
||||
project = {"path": str(tmp_path / "source/a.prj"), "database": str(tmp_path / "source/database"),
|
||||
"library": str(tmp_path / "library/mentor.lmc")}
|
||||
for output in (tmp_path / "source/database/results", tmp_path / "library/results"):
|
||||
with pytest.raises(ValueError):
|
||||
validate_output(output, tmp_path / "source", [project])
|
||||
validate_output(tmp_path / "results", tmp_path / "source", [project])
|
||||
|
||||
|
||||
def test_retry_cannot_claim_running_owner_has_exited():
|
||||
from mentorexport.cli import assert_retry_safe, process_identity
|
||||
import os
|
||||
import pytest
|
||||
with pytest.raises(ValueError, match="运行"):
|
||||
assert_retry_safe({"state": "running", "owner": process_identity(os.getpid())})
|
||||
@@ -0,0 +1,74 @@
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
|
||||
def make_project(root, name="Audio", block="Audio-Top", snapshot="DxD"):
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
(root / "database").mkdir(exist_ok=True)
|
||||
(root / "database/icdb.dat").write_bytes(b"saved database")
|
||||
path = root / (name + ".prj")
|
||||
path.write_text(
|
||||
f'SECTION Board1\nKEY RootBlock "{block}"\nENDSECTION\n'
|
||||
f'SECTION DesignInfo\nKEY FrontEndSnapshot "{snapshot}"\n'
|
||||
'KEY CentralLibrary "../库/mentor_lib.lmc"\nENDSECTION\n'
|
||||
'SECTION iCDB\nKEY iCDBDir "database"\nENDSECTION\n',
|
||||
encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def test_project_uses_configured_snapshot_and_relative_database(tmp_path):
|
||||
from mentorexport.core import read_project
|
||||
path = make_project(tmp_path / "工程 空格", snapshot="SchematicSaved")
|
||||
result = read_project(path)
|
||||
assert result["root_block"] == "Audio-Top"
|
||||
assert result["snapshot"] == "SchematicSaved"
|
||||
assert Path(result["database"]) == path.parent / "database"
|
||||
assert Path(result["library"]) == (path.parent / "../库/mentor_lib.lmc").resolve()
|
||||
|
||||
|
||||
def test_multiple_roots_cannot_silently_export_first(tmp_path):
|
||||
from mentorexport.core import read_project
|
||||
path = make_project(tmp_path)
|
||||
with path.open("a", encoding="utf-8") as stream:
|
||||
stream.write('SECTION Board2\nKEY RootBlock "Other"\nENDSECTION\n')
|
||||
with pytest.raises(ValueError, match="多个"):
|
||||
read_project(path)
|
||||
|
||||
|
||||
def test_scan_keeps_same_names_and_excludes_own_output(tmp_path):
|
||||
from mentorexport.core import discover_projects, project_folder_name
|
||||
make_project(tmp_path / "甲")
|
||||
make_project(tmp_path / "乙")
|
||||
output = tmp_path / "exports"
|
||||
make_project(output / "copied")
|
||||
projects, problems = discover_projects(tmp_path, excluded=(output,))
|
||||
assert len(projects) == 2 and not problems
|
||||
assert len({project_folder_name(p, tmp_path) for p in projects}) == 2
|
||||
|
||||
|
||||
def test_missing_database_is_visible_problem(tmp_path):
|
||||
from mentorexport.core import discover_projects
|
||||
path = make_project(tmp_path)
|
||||
(path.parent / "database/icdb.dat").unlink()
|
||||
projects, problems = discover_projects(tmp_path)
|
||||
assert not projects and len(problems) == 1
|
||||
assert problems[0]["path"] == str(path.resolve())
|
||||
assert "数据库" in problems[0]["reason"]
|
||||
|
||||
|
||||
def test_atomic_json_preserves_unicode_and_can_be_updated(tmp_path):
|
||||
from mentorexport.core import write_json
|
||||
import json
|
||||
p = tmp_path / "nested/state.json"
|
||||
write_json(p, {"工程": "甲"})
|
||||
write_json(p, {"工程": "乙"})
|
||||
assert json.loads(p.read_text(encoding="utf-8")) == {"工程": "乙"}
|
||||
assert list(p.parent.iterdir()) == [p]
|
||||
|
||||
|
||||
def test_dxp_without_prj_is_reported_instead_of_silently_ignored(tmp_path):
|
||||
from mentorexport.core import discover_projects
|
||||
(tmp_path / "Legacy.dxp").write_text("legacy")
|
||||
projects, problems = discover_projects(tmp_path)
|
||||
assert not projects and len(problems) == 1
|
||||
assert ".prj" in problems[0]["reason"]
|
||||
@@ -0,0 +1,29 @@
|
||||
def test_inspection_does_not_mix_tools_from_other_installation(tmp_path):
|
||||
from mentorexport.environment import inspect_installation
|
||||
install = tmp_path / "SDD_HOME"
|
||||
(install / "iCDB/win32/bin").mkdir(parents=True)
|
||||
(install / "iCDB/win32/bin/icdb2ascii.exe").write_bytes(b"fixture")
|
||||
result = inspect_installation(install)
|
||||
assert result["tools"]["ascii"] == str(install / "iCDB/win32/bin/icdb2ascii.exe")
|
||||
assert "keyin" not in result["tools"]
|
||||
assert result["native_compatible"] is False
|
||||
|
||||
|
||||
def test_explicit_nonexistent_install_is_rejected(tmp_path):
|
||||
from mentorexport.environment import discover_installations
|
||||
import pytest
|
||||
with pytest.raises(ValueError):
|
||||
discover_installations(explicit=tmp_path / "missing")
|
||||
|
||||
|
||||
def test_preflight_rejects_zero_exit_without_expected_help(tmp_path):
|
||||
from mentorexport.environment import preflight_installation
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
from pathlib import Path
|
||||
Path(log_dir).mkdir(parents=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text("")
|
||||
(Path(log_dir) / "stderr.txt").write_text("")
|
||||
return {"exit_code": 0, "cleanup": {"errors": []}}
|
||||
result = preflight_installation({"root": str(tmp_path), "tools": {"ascii": "ascii.exe"}},
|
||||
tmp_path / "logs", runner=runner)
|
||||
assert result["status"] == "failed"
|
||||
@@ -0,0 +1,401 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample(tmp_path):
|
||||
source = tmp_path / "source"
|
||||
(source / "database").mkdir(parents=True)
|
||||
(source / "database/icdb.dat").write_bytes(b"saved input")
|
||||
(source / "database/cdbsvr.addr").write_text("remote server")
|
||||
prj = source / "design.prj"
|
||||
prj.write_text('KEY iCDBDir "database"\nKEY DedicatedServerName "remote"\n')
|
||||
exe = tmp_path / "ascii.exe"
|
||||
exe.write_bytes(b"tool version one")
|
||||
project = dict(path=str(prj), database=str(source / "database"), root_block="Top", snapshot="Saved", name="design", library=None)
|
||||
install = dict(root=str(tmp_path), version="test", tools={"ascii": str(exe)}, com={})
|
||||
return project, install, tmp_path
|
||||
|
||||
|
||||
def backend():
|
||||
from mentorexport import exports
|
||||
return exports
|
||||
|
||||
|
||||
def ascii_runner(calls):
|
||||
def run(command, cwd, environment, log_dir, *, stop_event, timeout):
|
||||
calls.append(command)
|
||||
db = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-icdb=")))
|
||||
assert (db / "icdb.dat").read_bytes() == b"saved input"
|
||||
assert not (db / "cdbsvr.addr").exists()
|
||||
assert 'DedicatedServerName ""' in (db.parent / "input.prj").read_text()
|
||||
(db / "icdb.dat").write_bytes(b"tool mutation")
|
||||
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
output.write_text("iCDB Ascii Report\n" + ("Block Top\n" if "-h" in command else "Config default\n") + " Symbol instance\n Net GND\n")
|
||||
return dict(exit_code=0, cleanup={"errors": []}, seconds=0.01)
|
||||
return run
|
||||
|
||||
|
||||
def test_exit_zero_without_output_is_failed_and_durable(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
monkeypatch.setattr(exports, "_run", lambda *a, **k: dict(exit_code=0, cleanup={"errors": []}))
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
flat = next(i for i in result["items"] if i["key"] == "ascii_flat")
|
||||
assert flat["status"] == "failed"
|
||||
assert "output" in flat["reason"].lower()
|
||||
assert json.loads((root / "attempt/export.json").read_text(encoding="utf-8"))["items"] == result["items"]
|
||||
|
||||
|
||||
def test_fresh_clones_provenance_and_unchanged_source(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
good = [i for i in result["items"] if i["status"] == "success"]
|
||||
assert [i["key"] for i in good] == ["ascii_flat", "ascii_hier"]
|
||||
assert len(calls) == 2
|
||||
assert result["source_unchanged"] is True
|
||||
assert Path(project["database"], "icdb.dat").read_bytes() == b"saved input"
|
||||
assert good[0]["provenance"]["tool"]["sha256"] == exports.sha256(install["tools"]["ascii"])
|
||||
assert good[0]["provenance"]["output_files"][0]["sha256"]
|
||||
assert good[0]["provenance"]["snapshot"] == "Saved"
|
||||
|
||||
|
||||
def test_cancelled_before_start_does_not_launch(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
event = Event()
|
||||
event.set()
|
||||
def forbidden(*a, **k):
|
||||
raise AssertionError("should not launch")
|
||||
monkeypatch.setattr(exports, "_run", forbidden)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", stop_event=event, progress=lambda _: None)
|
||||
assert result["status"] == "cancelled"
|
||||
assert all(i["status"] == "cancelled" for i in result["items"])
|
||||
|
||||
|
||||
def test_retry_reuses_only_verified_matching_success(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
||||
first = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
second = exports.export_project(project, install, root / "two", root / "shared", previous=first, progress=lambda _: None)
|
||||
assert len(calls) == 2
|
||||
assert next(i for i in second["items"] if i["key"] == "ascii_flat")["provenance"]["reused"] is True
|
||||
Path(project["database"], "icdb.dat").write_bytes(b"new saved input")
|
||||
monkeypatch.setattr(exports, "_run", lambda *a, **k: dict(exit_code=0, cleanup={"errors": []}))
|
||||
third = exports.export_project(project, install, root / "three", root / "shared", previous=second, progress=lambda _: None)
|
||||
assert third["source_fingerprint"] != second["source_fingerprint"]
|
||||
assert next(i for i in third["items"] if i["key"] == "ascii_flat")["status"] == "failed"
|
||||
|
||||
|
||||
def test_modified_output_and_changed_tool_are_not_reused(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
||||
first = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
flat = next(i for i in first["items"] if i["key"] == "ascii_flat")
|
||||
Path(flat["outputs"][0]).write_text("tampered")
|
||||
exports.export_project(project, install, root / "two", root / "new-shared", previous=first, progress=lambda _: None)
|
||||
assert len(calls) == 3
|
||||
Path(install["tools"]["ascii"]).write_bytes(b"new tool")
|
||||
exports.export_project(project, install, root / "three", root / "shared", previous=first, progress=lambda _: None)
|
||||
assert len(calls) == 5
|
||||
|
||||
|
||||
def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
library = root / "library"
|
||||
(library / "PartsDBLibs").mkdir(parents=True)
|
||||
(library / "CellDBLibs").mkdir()
|
||||
(library / "PartsDBLibs/parts.pdb").write_bytes(b"parts")
|
||||
(library / "CellDBLibs/cells.cel").write_bytes(b"cells")
|
||||
(library / "lib.lmc").write_bytes(b"config")
|
||||
project["library"] = str(library / "lib.lmc")
|
||||
dll_dir = root / "common/win32/lib"
|
||||
dll_dir.mkdir(parents=True)
|
||||
for name in exports.VALIDATED_DLLS:
|
||||
(dll_dir / name).write_bytes(name.encode())
|
||||
monkeypatch.setattr(exports, "VALIDATED_DLLS", {name: exports.sha256(dll_dir / name) for name in exports.VALIDATED_DLLS})
|
||||
native_calls = []
|
||||
active_parts = ["USED"]
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
if "-f" in command or "-h" in command:
|
||||
out = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
out.write_text("iCDB Ascii Report\n" + ("Block Top\n" if "-h" in command else "Config default\n") +
|
||||
"".join(" Symbol id\n\tProp Part Number " + part + "\n\tProp Cell Name FOOTPRINT\n" for part in active_parts))
|
||||
else:
|
||||
native_calls.append(command)
|
||||
mode = command[1]
|
||||
rows = [{"name": "USED"}, {"name": "NEW"}, {"name": "UNUSED"}] if mode == "parts" else [{"name": "FOOTPRINT"}, {"name": "UNUSED"}]
|
||||
data = {"mode": mode, "count": len(rows), "records": rows}
|
||||
Path(log_dir).mkdir(parents=True, exist_ok=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text(json.dumps(data), encoding="utf-8")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
first = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
item = next(i for i in first["items"] if i["key"] == "library")
|
||||
assert item["status"] == "success", item
|
||||
assert len(native_calls) == 2
|
||||
assert all("UNUSED" not in p.read_text(encoding="utf-8") for p in (root / "shared").rglob("*.json"))
|
||||
exports.export_project(project, install, root / "two", root / "new-shared", previous=first, progress=lambda _: None)
|
||||
assert len(native_calls) == 2
|
||||
(library / "PartsDBLibs/parts.pdb").write_bytes(b"new parts")
|
||||
third = exports.export_project(project, install, root / "three", root / "shared", previous=first, progress=lambda _: None)
|
||||
assert first["source_fingerprint"] != third["source_fingerprint"]
|
||||
assert len(native_calls) == 4
|
||||
active_parts.append("NEW")
|
||||
fourth = exports.export_project(project, install, root / "four", root / "shared", progress=lambda _: None)
|
||||
assert next(i for i in fourth["items"] if i["key"] == "library")["status"] == "success"
|
||||
assert len(native_calls) == 5, "only partition containing previously unread NEW should rerun"
|
||||
|
||||
|
||||
def test_native_abi_mismatch_never_launches_helper(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
library = root / "library"
|
||||
(library / "PartsDBLibs").mkdir(parents=True)
|
||||
(library / "PartsDBLibs/part.pdb").write_bytes(b"parts")
|
||||
project["library"] = str(library)
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner([]))
|
||||
result = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
item = next(i for i in result["items"] if i["key"] == "library")
|
||||
assert item["status"] == "unavailable"
|
||||
assert "ABI" in item["reason"]
|
||||
|
||||
|
||||
def test_snapshot_listing_parses_vendor_arrows_without_selecting_root(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
Path(log_dir).mkdir(parents=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text("Snapshots and top blocks:\n -> Saved\n + Top\n -> Board1\n + AnotherRoot\n")
|
||||
return {"exit_code": 0}
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
assert exports.list_snapshots(project, install, root / "logs") == ["Saved", "Board1"]
|
||||
assert project["snapshot"] == "Saved"
|
||||
|
||||
|
||||
def test_batch_records_snapshot_names_from_captured_input(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
||||
ascii_run = ascii_runner([])
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
if "-list" not in command:
|
||||
return ascii_run(command, cwd, environment, log_dir, **kwargs)
|
||||
assert (Path(cwd) / "database/icdb.dat").read_bytes() == b"saved input"
|
||||
Path(log_dir).mkdir(parents=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text(" -> Saved\n -> Board1\n")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert result["snapshot_discovery"]["names"] == ["Saved", "Board1"]
|
||||
assert result["snapshot_discovery"]["status"] == "success"
|
||||
assert result["project"]["snapshot"] == "Saved"
|
||||
|
||||
|
||||
def test_uncertain_snapshot_cleanup_stops_batch_and_preserves_copy(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
||||
calls = []
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
calls.append(command)
|
||||
return dict(exit_code=0, cleanup_uncertain=True, cleanup={"errors": ["exit unconfirmed"]})
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert len(calls) == 1 and "-list" in calls[0]
|
||||
assert result["cleanup_uncertain"] is True
|
||||
assert result["status"] == "failed"
|
||||
assert all(i["status"] == "cancelled" for i in result["items"])
|
||||
preserved = Path(result["snapshot_discovery"]["workspace_preserved"])
|
||||
assert (preserved / "database/icdb.dat").is_file()
|
||||
exports.shutil.rmtree(preserved)
|
||||
exports.shutil.rmtree(Path(result["workspace_preserved"]))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key,name,text", [
|
||||
("ascii_hier", "connections.txt", "iCDB Ascii Report Generation Date: x\nMODE: HIER BLOCKS\nBlock Top\n\tNet Ground\n"),
|
||||
("edif_netlist", "design.edf", "(edif Saved (edifVersion 2 0 0) (viewType NETLIST) (net\n (rename GND Ground) (joined)))"),
|
||||
])
|
||||
def test_validated_vendor_formats_accept_hierarchy_block_and_net_whitespace(tmp_path, key, name, text):
|
||||
exports = backend()
|
||||
(tmp_path / name).write_text(text)
|
||||
assert exports._validate(key, tmp_path) == [tmp_path / name]
|
||||
|
||||
|
||||
def test_csv_manifest_cannot_make_malformed_tables_successful(tmp_path):
|
||||
exports = backend()
|
||||
folder = tmp_path / "work"
|
||||
folder.mkdir()
|
||||
tables = "sym spn spr ppr prm ppn prt pth sht inf npr thn".split()
|
||||
(folder / "design.csv").write_text("\n".join("file:design.csv." + t for t in tables))
|
||||
for table in tables:
|
||||
(folder / ("design.csv." + table)).write_text("not a tab separated table")
|
||||
with pytest.raises(ValueError, match="table"):
|
||||
exports._validate("csv", tmp_path)
|
||||
|
||||
|
||||
def test_empty_oat_is_not_a_successful_pair(tmp_path):
|
||||
exports = backend()
|
||||
for folder in ("wir", "oat"):
|
||||
(tmp_path / folder).mkdir()
|
||||
(tmp_path / "wir/top.1").write_text("| Wirelist created using version 6.0\nV 6.0\nDW Top\n")
|
||||
(tmp_path / "oat/top.oat").write_bytes(b"")
|
||||
with pytest.raises(ValueError, match="WIR/OAT"):
|
||||
exports._validate("wir_oat", tmp_path)
|
||||
|
||||
|
||||
def test_failed_format_output_is_retained_as_failure_evidence(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
def malformed(command, cwd, environment, log_dir, **kwargs):
|
||||
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
output.write_text("partial or malformed vendor output")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
monkeypatch.setattr(exports, "_run", malformed)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
item = result["items"][0]
|
||||
assert item["status"] == "failed"
|
||||
assert item["provenance"]["failed_output_files"]
|
||||
assert Path(item["provenance"]["failed_output_files"][0]["path"]).read_text() == "partial or malformed vendor output"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", [{"hr": -2147467259, "value": None}, {"hr": 0, "value": None}])
|
||||
def test_native_getter_failure_or_null_is_rejected(field):
|
||||
exports = backend()
|
||||
native = {"mode": "parts", "count": 1, "records": [{"name": "USED", "part_name": field}]}
|
||||
with pytest.raises(ValueError, match="getter"):
|
||||
exports._validate_native(native, "parts")
|
||||
|
||||
|
||||
def test_native_success_hr_with_null_coordinate_pointer_is_rejected():
|
||||
exports = backend()
|
||||
native = {"mode": "cell", "count": 1, "records": [{"name": "FOOTPRINT", "pins": [
|
||||
{"location_hr": 0, "xy_raw": None}]}]}
|
||||
with pytest.raises(ValueError, match="coordinate"):
|
||||
exports._validate_native(native, "cell")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("outcome", ["crash", "invalid_json", "cancel"])
|
||||
def test_native_partition_diagnostics_and_accumulated_data_survive_failure(sample, monkeypatch, outcome):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
library = root / "library/PartsDBLibs"
|
||||
library.mkdir(parents=True)
|
||||
(library / "a.pdb").write_bytes(b"a")
|
||||
(library / "b.pdb").write_bytes(b"b")
|
||||
project["library"] = str(library.parent)
|
||||
dll_dir = root / "common/win32/lib"
|
||||
dll_dir.mkdir(parents=True)
|
||||
for name in exports.VALIDATED_DLLS:
|
||||
(dll_dir / name).write_bytes(name.encode())
|
||||
monkeypatch.setattr(exports, "VALIDATED_DLLS", {name: exports.sha256(dll_dir / name) for name in exports.VALIDATED_DLLS})
|
||||
native_count = 0
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
nonlocal native_count
|
||||
if "-f" in command or "-h" in command:
|
||||
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
output.write_text("iCDB Ascii Report\n" + ("Block Top\n" if "-h" in command else "Config default\n") + "\tProp Part Number USED\n")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
native_count += 1
|
||||
log_dir = Path(log_dir)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
run = dict(exit_code=0, cleanup={"errors": []})
|
||||
payload = json.dumps({"mode": "parts", "count": 1, "records": [{"name": "USED"}]})
|
||||
if native_count == 2:
|
||||
payload = "incomplete raw output"
|
||||
run.update(exit_code=9 if outcome == "crash" else 0, cancelled=outcome == "cancel")
|
||||
(log_dir / "stdout.txt").write_text(payload, encoding="utf-8")
|
||||
(log_dir / "stderr.txt").write_text("native diagnostic evidence", encoding="utf-8")
|
||||
(log_dir / "run.json").write_text(json.dumps(run), encoding="utf-8")
|
||||
return run
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
item = next(i for i in result["items"] if i["key"] == "library")
|
||||
assert item["status"] == ("cancelled" if outcome == "cancel" else "failed")
|
||||
logs = root / "attempt/90_运行日志/library"
|
||||
assert len(list(logs.rglob("stderr.txt"))) == 2
|
||||
assert len(list(logs.rglob("run.json"))) == 2
|
||||
stdout = list(logs.rglob("stdout.txt"))
|
||||
assert len(stdout) == 1 and stdout[0].read_text() == "incomplete raw output"
|
||||
data_files = list((root / "shared").rglob("used-*.json"))
|
||||
assert len(data_files) == 1
|
||||
data = json.loads(data_files[0].read_text(encoding="utf-8"))
|
||||
assert data["complete"] is False
|
||||
assert len(data["runs"]) == 2
|
||||
assert data["records"]["parts"][0]["name"] == "USED"
|
||||
assert str(data_files[0]) in item["outputs"]
|
||||
|
||||
|
||||
def test_uncertain_cleanup_preserves_workspace_and_stops_remaining_tools(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
def uncertain(command, cwd, environment, log_dir, **kwargs):
|
||||
calls.append(Path(cwd))
|
||||
return dict(exit_code=0, cleanup_uncertain=True, cleanup={"errors": ["owned process exit unconfirmed"]})
|
||||
monkeypatch.setattr(exports, "_run", uncertain)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert len(calls) == 1
|
||||
assert result["cleanup_uncertain"] is True
|
||||
preserved = Path(result["workspace_preserved"])
|
||||
assert preserved.is_dir()
|
||||
assert calls[0].is_dir()
|
||||
assert result["status"] == "failed"
|
||||
assert all(i["status"] == "cancelled" for i in result["items"][1:])
|
||||
# No real process is running in this synthetic boundary failure.
|
||||
exports.shutil.rmtree(preserved)
|
||||
|
||||
|
||||
def test_capture_rejects_a_source_that_changes_while_copying(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
real_copy = exports.shutil.copyfile
|
||||
def changing(source, target, *args, **kwargs):
|
||||
value = real_copy(source, target, *args, **kwargs)
|
||||
if Path(source).name == "icdb.dat":
|
||||
Path(source).write_bytes(b"new saved state")
|
||||
return value
|
||||
monkeypatch.setattr(exports.shutil, "copyfile", changing)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert result["status"] == "failed"
|
||||
assert "changed" in result["reason"].lower()
|
||||
assert not result["items"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(__import__("os").name != "nt", reason="Windows private desktop")
|
||||
@pytest.mark.parametrize("cancel", [False, True])
|
||||
def test_owned_process_timeout_and_active_cancel_leave_no_job_processes(tmp_path, cancel):
|
||||
pytest.importorskip("win32job")
|
||||
import os
|
||||
import sys
|
||||
from threading import Timer
|
||||
exports = backend()
|
||||
event = Event()
|
||||
timer = Timer(0.3, event.set) if cancel else None
|
||||
if timer:
|
||||
timer.start()
|
||||
try:
|
||||
result = exports._run([sys.executable, "-c", "import time;time.sleep(30)"], tmp_path,
|
||||
dict(os.environ), tmp_path / "logs", stop_event=event,
|
||||
timeout=5 if cancel else 0.3)
|
||||
finally:
|
||||
if timer:
|
||||
timer.cancel()
|
||||
assert result["cancelled"] is cancel
|
||||
assert result["timed_out"] is (not cancel)
|
||||
assert "error" not in result
|
||||
assert not result["cleanup"]["errors"]
|
||||
assert result["cleanup"]["desktop_handle_closed"]
|
||||
assert all(p["job_active_processes"] == 0 for p in result["cleanup"]["processes"])
|
||||
Reference in new issue
Block a user