90 lines
4.3 KiB
Python
90 lines
4.3 KiB
Python
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from kicad_backend.runtime import KiCadError, KiCadRuntime, net_mapping, parse_check
|
|
|
|
|
|
class CheckReports(unittest.TestCase):
|
|
def test_native_check_must_include_all_evidence(self):
|
|
erc = {"kicad_version": "10.0.7", "sheets": [{"violations": []}]}
|
|
drc = {"kicad_version": "10.0.7", "violations": [], "unconnected_items": [], "schematic_parity": []}
|
|
self.assertTrue(parse_check(erc, "erc", 0)["passed"])
|
|
self.assertTrue(parse_check(drc, "drc", 0)["passed"])
|
|
for kind, data in [("erc", {}), ("erc", {"kicad_version": "10.0.7", "sheets": []}), ("drc", None), ("drc", {"kicad_version": "10.0.7", "violations": []})]:
|
|
self.assertEqual(parse_check(data, kind, 0)["status"], "unverified")
|
|
self.assertFalse(parse_check(drc, "drc", 5)["passed"])
|
|
|
|
def test_warnings_exclusions_and_unrouted_items_block(self):
|
|
for group in ["violations", "unconnected_items", "schematic_parity"]:
|
|
data = {"kicad_version": "10.0.7", "violations": [], "unconnected_items": [], "schematic_parity": []}
|
|
data[group] = [{"type": "unconnected_items", "severity": "warning", "excluded": True}]
|
|
self.assertFalse(parse_check(data, "drc", 0)["passed"])
|
|
|
|
|
|
class NativePinParity(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.temp.cleanup)
|
|
self.path = Path(self.temp.name) / "netlist.xml"
|
|
self.path.write_text('''<export><components><comp ref="J1"/><comp ref="J2"/></components><nets>
|
|
<net name="/VIN"><node ref="J1" pin="1"/><node ref="J2" pin="1"/></net>
|
|
<net name="/GND"><node ref="J1" pin="2"/><node ref="J2" pin="2"/></net></nets></export>''')
|
|
self.board = {"footprints": [{"reference": ref, "pads": [{"pin": "1", "net": "/VIN"}, {"pin": "2", "net": "/GND"}]} for ref in ["J1", "J2"]]}
|
|
|
|
def test_exact_mapping_passes(self):
|
|
self.assertTrue(net_mapping(self.path, self.board)["passed"])
|
|
|
|
def test_network_names_are_not_lossily_normalized(self):
|
|
self.board["footprints"][0]["pads"][0]["net"] = "VIN"
|
|
self.assertFalse(net_mapping(self.path, self.board)["passed"])
|
|
|
|
def test_missing_and_duplicate_footprints_are_detected(self):
|
|
self.board["footprints"][1]["reference"] = "J1"
|
|
result = net_mapping(self.path, self.board)
|
|
self.assertFalse(result["passed"])
|
|
self.assertIn("missing_footprint", [issue["type"] for issue in result["issues"]])
|
|
self.assertIn("duplicate_reference", [issue["type"] for issue in result["issues"]])
|
|
|
|
def test_extra_copper_net_is_detected(self):
|
|
self.board["footprints"][0]["pads"].append({"pin": "3", "net": "/VIN"})
|
|
self.assertFalse(net_mapping(self.path, self.board)["passed"])
|
|
|
|
|
|
class ProcessIsolation(unittest.TestCase):
|
|
def test_subprocess_timeout(self):
|
|
runtime = KiCadRuntime(timeout=1)
|
|
with self.assertRaisesRegex(KiCadError, "timed out"):
|
|
runtime.run([sys.executable, "-c", "import time; time.sleep(30)"])
|
|
|
|
def test_source_copy_excludes_keys_history_and_symlinks(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root = Path(temp)
|
|
source, target = root / "source", root / "target"
|
|
source.mkdir()
|
|
target.mkdir()
|
|
(source / "board.kicad_pcb").write_text("pcb")
|
|
(source / "auth.json").write_text("private")
|
|
(source / "api.sqlite3").write_text("history")
|
|
KiCadRuntime._copy(source, target)
|
|
self.assertEqual([p.name for p in target.iterdir()], ["board.kicad_pcb"])
|
|
(source / "linked.kicad_pcb").symlink_to(source / "board.kicad_pcb")
|
|
with self.assertRaisesRegex(KiCadError, "symlink"):
|
|
KiCadRuntime._copy(source, target)
|
|
|
|
def test_existing_output_is_immutable(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
project = Path(temp) / "input" / "board.kicad_pro"
|
|
project.parent.mkdir()
|
|
project.write_text("{}")
|
|
output = Path(temp) / "existing"
|
|
output.mkdir()
|
|
with self.assertRaisesRegex(KiCadError, "already exists"):
|
|
KiCadRuntime().process(project, output)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|