706 lines
35 KiB
Python
706 lines
35 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 test_parallel_exports_overlap_on_distinct_clones_and_keep_receipt_order(sample, monkeypatch):
|
|
from threading import Barrier, Lock
|
|
exports = backend()
|
|
project, install, root = sample
|
|
barrier, lock = Barrier(2), Lock()
|
|
calls, workspaces = [], []
|
|
run_ascii = ascii_runner(calls)
|
|
active = maximum = 0
|
|
def runner(command, cwd, environment, log_dir, **kwargs):
|
|
nonlocal active, maximum
|
|
with lock:
|
|
active += 1
|
|
maximum = max(maximum, active)
|
|
workspaces.append(Path(cwd))
|
|
try:
|
|
barrier.wait(timeout=5)
|
|
return run_ascii(command, cwd, environment, log_dir, **kwargs)
|
|
finally:
|
|
with lock:
|
|
active -= 1
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
result = exports.export_project(project, install, root / "parallel", root / "shared", jobs=2, progress=lambda _: None)
|
|
assert maximum == 2 and len(set(workspaces)) == 2
|
|
assert [item["key"] for item in result["items"]] == [spec[0] for spec in exports.ITEMS]
|
|
assert [i["key"] for i in result["items"] if i["status"] == "success"] == ["ascii_flat", "ascii_hier"]
|
|
assert result["source_unchanged"] and result["execution"]["jobs"] == 2
|
|
|
|
|
|
def test_library_starts_after_flat_without_waiting_for_other_exports(sample, monkeypatch):
|
|
from threading import Event
|
|
exports = backend()
|
|
project, install, root = sample
|
|
library_started = Event()
|
|
runner = ascii_runner([])
|
|
def run(command, *args, **kwargs):
|
|
if "-h" in command:
|
|
assert library_started.wait(5), "library waited for unrelated exports"
|
|
return runner(command, *args, **kwargs)
|
|
def library(item, project, installation, result, *args):
|
|
flat = next(i for i in result["items"] if i["key"] == "ascii_flat")
|
|
assert flat["status"] == "success" and Path(flat["outputs"][0]).is_file()
|
|
library_started.set()
|
|
item.update(status="unavailable", reason="fixture without library")
|
|
monkeypatch.setattr(exports, "_run", run)
|
|
monkeypatch.setattr(exports, "_export_library", library)
|
|
result = exports.export_project(project, install, root / "parallel", root / "shared", jobs=2, progress=lambda _: None)
|
|
assert library_started.is_set() and result["items"][1]["status"] == "success"
|
|
|
|
|
|
def test_snapshot_listing_overlaps_with_exports(sample, monkeypatch):
|
|
from threading import Barrier
|
|
exports = backend()
|
|
project, install, root = sample
|
|
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
|
barrier = Barrier(2)
|
|
runner = ascii_runner([])
|
|
def run(command, cwd, environment, log_dir, **kwargs):
|
|
if "-list" in command or "-f" in command:
|
|
barrier.wait(timeout=5)
|
|
if "-list" in command:
|
|
Path(log_dir).mkdir(parents=True)
|
|
(Path(log_dir) / "stdout.txt").write_text(" -> Saved\n")
|
|
return {"exit_code": 0}
|
|
return runner(command, cwd, environment, log_dir, **kwargs)
|
|
monkeypatch.setattr(exports, "_run", run)
|
|
result = exports.export_project(project, install, root / "parallel", root / "shared", jobs=2, progress=lambda _: None)
|
|
assert result["snapshot_discovery"]["status"] == "success"
|
|
assert result["items"][0]["status"] == "success"
|
|
|
|
|
|
def test_forced_descendant_shutdown_keeps_files_but_never_reuses_as_verified(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
calls = []
|
|
ascii_run = ascii_runner(calls)
|
|
def runner(*args, **kwargs):
|
|
run = ascii_run(*args, **kwargs)
|
|
run["cleanup"] = {"errors": [], "processes": [{"forced_termination": True,
|
|
"forced_root_termination": False, "forced_descendant_pids": [123],
|
|
"termination_reason": "descendant_grace_expired", "job_active_processes": 0}]}
|
|
return run
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
first = exports.export_project(project, install, root / "first", root / "shared", jobs=2, progress=lambda _: None)
|
|
assert first["items"][0]["status"] == "exported" and first["items"][0]["outputs"]
|
|
assert first["items"][0]["validation"]["status"] == "warning"
|
|
exports.export_project(project, install, root / "retry", root / "shared", jobs=2, previous=first, progress=lambda _: None)
|
|
assert len(calls) == 4
|
|
|
|
|
|
def test_forced_library_shutdown_retains_records_without_trusted_cache(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
library = root / "library/PartsDBLibs"
|
|
library.mkdir(parents=True)
|
|
(library / "a.pdb").write_bytes(b"parts")
|
|
project["library"] = str(library.parent)
|
|
monkeypatch.setattr(exports, "VALIDATED_DLLS", {})
|
|
monkeypatch.setattr(exports, "_LIBRARY_CACHE", {})
|
|
calls = []
|
|
def runner(command, cwd, environment, log_dir, **kwargs):
|
|
if "-f" in command or "-h" in command:
|
|
output = Path(next(arg.split("=", 1)[1] for arg in command if arg.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": []})
|
|
calls.append(command)
|
|
Path(log_dir).mkdir(parents=True, exist_ok=True)
|
|
(Path(log_dir) / "stdout.txt").write_text(json.dumps({"mode": "parts", "count": 1, "records": [{"name": "USED"}]}), encoding="utf-8")
|
|
return dict(exit_code=0, cleanup={"errors": [], "processes": [{"forced_termination": True}]})
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
first = exports.export_project(project, install, root / "first", root / "shared", jobs=2, progress=lambda _: None)
|
|
item = next(i for i in first["items"] if i["key"] == "library")
|
|
assert item["status"] == "exported" and item["validation"]["status"] == "warning"
|
|
data = next(json.loads(Path(path).read_text(encoding="utf-8")) for path in item["outputs"] if Path(path).name.startswith("used-"))
|
|
assert data["records"]["parts"][0]["name"] == "USED" and data["complete"] is False
|
|
exports.export_project(project, install, root / "retry", root / "shared", jobs=2, previous=first, progress=lambda _: None)
|
|
assert len(calls) == 2
|
|
|
|
|
|
def test_parallel_cancel_drains_active_tasks_without_launching_queued_vendor_work(sample, monkeypatch):
|
|
from threading import Barrier
|
|
exports = backend()
|
|
project, install, root = sample
|
|
stop, barrier = Event(), Barrier(2)
|
|
calls = []
|
|
def runner(command, cwd, *args, **kwargs):
|
|
calls.append(command)
|
|
barrier.wait(timeout=5)
|
|
stop.set()
|
|
return dict(exit_code=None, cancelled=True, cleanup={"errors": []})
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
result = exports.export_project(project, install, root / "cancel", root / "shared", jobs=2,
|
|
stop_event=stop, progress=lambda _: None)
|
|
assert len(calls) == 2 and result["status"] == "cancelled"
|
|
assert all(item["status"] == "cancelled" for item in result["items"])
|
|
|
|
|
|
def test_process_exit_notification_advances_without_polling_delay(tmp_path, monkeypatch):
|
|
exports = backend()
|
|
class Process:
|
|
pid = 123
|
|
finished = False
|
|
def poll(self): return 0 if self.finished else None
|
|
def wait(self, timeout=None):
|
|
self.finished = True
|
|
return 0
|
|
class Desktop:
|
|
cleanup_record = {"errors": [], "processes": []}
|
|
def __init__(self, *args, **kwargs): pass
|
|
def __enter__(self): return self
|
|
def __exit__(self, *args): pass
|
|
def start_process(self, *args, **kwargs): return Process()
|
|
def inspect_dialogs(self, pid): return []
|
|
monkeypatch.setattr(exports, "PrivateDesktop", Desktop)
|
|
result = exports._run(["fixture"], tmp_path, {}, tmp_path / "logs", stop_event=None, timeout=0.15)
|
|
assert result["exit_code"] == 0 and not result["timed_out"]
|
|
|
|
|
|
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
|
|
|
|
|
|
@pytest.mark.parametrize("ascii_warning", [False, True])
|
|
def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatch, ascii_warning):
|
|
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))
|
|
(out.parent / "companion.txt").write_text("additional exported data")
|
|
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=9 if ascii_warning and ("-f" in command or "-h" in command) else 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"] == ("exported" if ascii_warning else "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"] == ("exported" if ascii_warning else "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_continues_exports_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) == 3 and any("-list" in call for call in calls)
|
|
assert result["cleanup_uncertain"] is True
|
|
assert result["status"] == "skipped"
|
|
assert not any(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)
|
|
if Path(result["workspace_preserved"]) != preserved and Path(result["workspace_preserved"]).exists():
|
|
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_unrecognized_format_is_exported_with_warning_in_normal_category(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"] == "exported"
|
|
assert item["validation"]["status"] == "warning"
|
|
path = Path(item["outputs"][0])
|
|
assert "02_器件与引脚属性" in str(path) and "90_运行日志" not in str(path)
|
|
assert path.read_text() == "partial or malformed vendor output"
|
|
assert result["status"] == "exported"
|
|
again = exports.export_project(project, install, root / "retry", root / "shared", previous=result, progress=lambda _: None)
|
|
assert not again["items"][0]["provenance"]["reused"]
|
|
|
|
|
|
@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 "exported")
|
|
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_continues_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) == 2
|
|
assert result["cleanup_uncertain"] is True
|
|
preserved = Path(result["workspace_preserved"])
|
|
assert preserved.is_dir()
|
|
assert calls[0].is_dir()
|
|
assert result["status"] == "skipped"
|
|
assert not any(i["status"] == "cancelled" for i in result["items"])
|
|
# 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"] == "skipped"
|
|
assert "changed" in result["reason"].lower()
|
|
assert not result["items"]
|
|
|
|
|
|
def test_nonzero_tool_with_output_is_retained_and_later_tool_runs(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
calls = []
|
|
real_runner = ascii_runner(calls)
|
|
def runner(*args, **kwargs):
|
|
run = real_runner(*args, **kwargs)
|
|
run["exit_code"] = 7
|
|
return run
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
assert len(calls) == 2
|
|
assert all(item["status"] == "exported" and Path(item["outputs"][0]).is_file() for item in result["items"][:2])
|
|
assert result["items"][0]["provenance"]["exit_code"] == 7
|
|
|
|
|
|
@pytest.mark.parametrize("lock", ["copy", "metadata"])
|
|
def test_one_locked_output_does_not_hide_other_generated_files(sample, monkeypatch, lock):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
def runner(command, cwd, environment, log_dir, **kwargs):
|
|
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file="))).parent
|
|
(output / "a.txt").write_text("exported A")
|
|
(output / "b.txt").write_text("exported B")
|
|
return dict(exit_code=8, cleanup={"errors": []})
|
|
real_copy = exports.shutil.copyfile
|
|
def copy(source, target, *args, **kwargs):
|
|
if Path(source).name == "a.txt":
|
|
raise PermissionError("one output is locked")
|
|
return real_copy(source, target, *args, **kwargs)
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
if lock == "copy":
|
|
monkeypatch.setattr(exports.shutil, "copyfile", copy)
|
|
else:
|
|
info = exports._file_info
|
|
def locked_info(path):
|
|
if Path(path).name == "a.txt":
|
|
raise PermissionError("one output hash is unavailable")
|
|
return info(path)
|
|
monkeypatch.setattr(exports, "_file_info", locked_info)
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
assert result["items"][0]["status"] == "exported"
|
|
assert {Path(path).read_text() for path in result["items"][0]["outputs"]} == {"exported A", "exported B"}
|
|
assert result["workspace_preserved"]
|
|
exports.shutil.rmtree(Path(result["workspace_preserved"]))
|
|
|
|
|
|
def test_source_changed_after_capture_keeps_outputs_as_warning(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
real_runner = ascii_runner([])
|
|
def runner(*args, **kwargs):
|
|
run = real_runner(*args, **kwargs)
|
|
Path(project["database"], "icdb.dat").write_bytes(b"new source saved during export")
|
|
return run
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
assert result["source_unchanged"] is False and result["status"] == "exported"
|
|
assert all(item["status"] == "exported" and item["outputs"] for item in result["items"][:2])
|
|
|
|
|
|
def test_locked_receipt_does_not_stop_following_tools(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
calls = []
|
|
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
|
write = exports.write_json
|
|
def locked(path, value):
|
|
if Path(path).name == "export.json":
|
|
raise PermissionError("receipt is locked")
|
|
return write(path, value)
|
|
monkeypatch.setattr(exports, "write_json", locked)
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
assert len(calls) == 2 and result["items"][1]["outputs"]
|
|
assert result["warnings"]
|
|
|
|
|
|
def test_unreadable_library_fingerprint_does_not_block_schematic_tools(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
calls = []
|
|
hashes = exports._hashes
|
|
monkeypatch.setattr(exports, "_library_sources", lambda _: {"parts": root / "parts.pdb"})
|
|
def unreadable(files):
|
|
if "parts" in files:
|
|
raise PermissionError("central library unreadable")
|
|
return hashes(files)
|
|
monkeypatch.setattr(exports, "_hashes", unreadable)
|
|
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
assert len(calls) == 2 and result["items"][0]["outputs"]
|
|
|
|
|
|
def test_successful_export_also_retains_companion_files(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
run = ascii_runner([])
|
|
def runner(command, cwd, environment, log_dir, **kwargs):
|
|
result = run(command, cwd, environment, log_dir, **kwargs)
|
|
(Path(cwd) / "output/companion.txt").write_text("additional exported data")
|
|
return result
|
|
monkeypatch.setattr(exports, "_run", runner)
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
assert {Path(path).name for path in result["items"][0]["outputs"]} == {"connections.txt", "companion.txt"}
|
|
|
|
|
|
def test_workspace_cleanup_error_is_recorded_and_receipt_updated(sample, monkeypatch):
|
|
exports = backend()
|
|
project, install, root = sample
|
|
monkeypatch.setattr(exports, "_run", ascii_runner([]))
|
|
remove = exports.shutil.rmtree
|
|
def locked(path):
|
|
raise PermissionError("temporary workspace locked")
|
|
monkeypatch.setattr(exports.shutil, "rmtree", locked)
|
|
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
|
receipt = json.loads((root / "attempt/export.json").read_text(encoding="utf-8"))
|
|
assert receipt["workspace_preserved"] == result["workspace_preserved"]
|
|
assert "locked" in result["workspace_cleanup_error"]
|
|
remove(Path(result["workspace_preserved"]))
|
|
|
|
|
|
def test_run_receipt_write_error_keeps_cleanup_uncertainty(tmp_path, monkeypatch):
|
|
exports = backend()
|
|
class Desktop:
|
|
cleanup_record = {"errors": ["unconfirmed owned process"], "processes": []}
|
|
def __init__(self, *args, **kwargs): pass
|
|
def __enter__(self): return self
|
|
def __exit__(self, *args): pass
|
|
def start_process(self, *args, **kwargs): return self
|
|
def poll(self): return 0
|
|
pid = 123
|
|
monkeypatch.setattr(exports, "PrivateDesktop", Desktop)
|
|
monkeypatch.setattr(exports, "_UNCERTAIN_DESKTOPS", [])
|
|
def locked(*args): raise PermissionError("run receipt locked")
|
|
monkeypatch.setattr(exports, "write_json", locked)
|
|
result = exports._run(["test"], tmp_path, {}, tmp_path / "logs", stop_event=None, timeout=1)
|
|
assert result["cleanup_uncertain"] and result["warnings"]
|
|
|
|
|
|
@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"])
|