402 lines
20 KiB
Python
402 lines
20 KiB
Python
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"])
|