Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
476328a26This file is pinned to the selected release. Download the original to open it in your design tool.
"""Tests for the electrical-rule check (ERC) (SPEC §3.6).
Unit-tested with synthetic ``Schematic``/``Net``/``Component``/``Pin`` objects
covering every rule, plus a smoke run against committed ``*.SchDoc`` fixtures
read through the frozen Altium reader. Assertion style mirrors test_bom.py.
"""
from __future__ import annotations
import os
import pytest
from altium_kicad_cli.checks import erc
from altium_kicad_cli.config import Config
from altium_kicad_cli.model import Component, Net, Pin, PinType, Schematic
from altium_kicad_cli.readers import altium_sch
from altium_kicad_cli.report import Finding, Severity
FIXTURES = os.path.join(os.path.dirname(__file__), "fixtures")
# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------
def _pin(
number: str,
*,
name: str | None = None,
etype: PinType = PinType.PASSIVE,
x: float = 0.0,
y: float = 0.0,
) -> Pin:
return Pin(number=number, name=name, x_mil=x, y_mil=y, electrical_type=etype)
def _comp(designator: str, pins: list[Pin], *, undesignated: bool = False) -> Component:
return Component(
designator=designator,
library_ref="Device:U",
x_mil=0.0,
y_mil=0.0,
pins=pins,
undesignated=undesignated,
)
def _net(name, members, *, aliases=None, source_names=None) -> Net:
return Net(
name=name,
members=sorted(members),
aliases=list(aliases or []),
source_names=list(source_names or ([name] if name else [])),
is_named=name is not None,
)
def _sch(
components: list[Component],
nets: list[Net],
*,
no_erc_points=None,
) -> Schematic:
return Schematic(
source_path="<test>",
source_format="altium",
components=components,
nets=nets,
no_erc_points=list(no_erc_points or []),
)
def _cfg(*, rails=None, waivers=None) -> Config:
return Config(rails=list(rails or []), erc_waivers=list(waivers or []))
def _codes(findings: list[Finding]) -> list[str]:
return [f.code for f in findings]
def _by_code(findings: list[Finding], code: str) -> list[Finding]:
return [f for f in findings if f.code == code]
# ---------------------------------------------------------------------------
# return contract
# ---------------------------------------------------------------------------
def test_returns_list_of_findings():
sch = _sch([_comp("U1", [_pin("1")])], [_net("STAT", [("U1", "1")])])
out = erc.run(sch, _cfg())
assert isinstance(out, list)
assert all(isinstance(f, Finding) for f in out)
def test_runs_with_cfg_none():
sch = _sch([_comp("U1", [_pin("1")])], [_net(None, [("U1", "1")])])
assert isinstance(erc.run(sch, None), list)
# ---------------------------------------------------------------------------
# dangling single-pin net
# ---------------------------------------------------------------------------
def test_single_pin_net_is_dangling():
sch = _sch([_comp("U1", [_pin("1")])], [_net(None, [("U1", "1")])])
dang = _by_code(erc.run(sch, _cfg()), erc.ERC_DANGLING_NET)
assert len(dang) == 1
assert dang[0].refs == ["U1.1"]
assert dang[0].severity is Severity.WARNING
def test_multi_pin_net_is_not_dangling():
sch = _sch(
[_comp("U1", [_pin("1")]), _comp("U2", [_pin("1")])],
[_net("NET1", [("U1", "1"), ("U2", "1")])],
)
assert _by_code(erc.run(sch, _cfg()), erc.ERC_DANGLING_NET) == []
def test_no_connect_pin_is_not_dangling():
# An explicit NO_CONNECT pin is intentionally unconnected -> not flagged.
sch = _sch([_comp("U1", [_pin("1", etype=PinType.NO_CONNECT)])],
[_net(None, [("U1", "1")])])
assert _by_code(erc.run(sch, _cfg()), erc.ERC_DANGLING_NET) == []
# ---------------------------------------------------------------------------
# No-ERC suppression (geo-match the pin tip within grid tolerance)
# ---------------------------------------------------------------------------
def test_no_erc_marker_suppresses_dangling():
pin = _pin("1", x=1000.0, y=2000.0)
sch = _sch([_comp("U1", [pin])], [_net(None, [("U1", "1")])],
no_erc_points=[(1000.0, 2000.0)])
assert _by_code(erc.run(sch, _cfg()), erc.ERC_DANGLING_NET) == []
def test_no_erc_marker_elsewhere_does_not_suppress():
pin = _pin("1", x=1000.0, y=2000.0)
sch = _sch([_comp("U1", [pin])], [_net(None, [("U1", "1")])],
no_erc_points=[(5000.0, 9000.0)]) # far from the pin tip
assert len(_by_code(erc.run(sch, _cfg()), erc.ERC_DANGLING_NET)) == 1
# ---------------------------------------------------------------------------
# driver conflict (TYPE-gated by confidence)
# ---------------------------------------------------------------------------
def test_driver_conflict_high_confidence_is_warning():
# Two OUTPUT pins on one net, all pins typed -> confidence 1.0 -> WARNING.
sch = _sch(
[
_comp("U1", [_pin("1", etype=PinType.OUTPUT)]),
_comp("U2", [_pin("1", etype=PinType.OUTPUT)]),
],
[_net("BUS", [("U1", "1"), ("U2", "1")])],
)
conf = _by_code(erc.run(sch, _cfg()), erc.ERC_DRIVER_CONFLICT)
assert len(conf) == 1
assert conf[0].severity is Severity.WARNING
assert {"U1.1", "U2.1"} <= set(conf[0].refs)
def test_driver_conflict_low_confidence_is_downgraded_to_note():
# Two OUTPUT pins drowned by many Passive pins -> confidence < 0.2 -> NOTE.
extra = _comp("R1", [_pin(str(i)) for i in range(1, 10)]) # 9 passive pins
sch = _sch(
[
_comp("U1", [_pin("1", etype=PinType.OUTPUT)]),
_comp("U2", [_pin("1", etype=PinType.OUTPUT)]),
extra,
],
[_net("BUS", [("U1", "1"), ("U2", "1")])],
)
conf = _by_code(erc.run(sch, _cfg()), erc.ERC_DRIVER_CONFLICT)
assert len(conf) == 1
assert conf[0].severity is Severity.NOTE
assert "type-confidence low" in conf[0].message
def test_two_bidirectional_pins_are_not_a_conflict():
# Bidirectional (bus) pins may legitimately share a net.
sch = _sch(
[
_comp("U1", [_pin("1", etype=PinType.BIDIRECTIONAL)]),
_comp("U2", [_pin("1", etype=PinType.BIDIRECTIONAL)]),
],
[_net("SDA", [("U1", "1"), ("U2", "1")])],
)
assert _by_code(erc.run(sch, _cfg()), erc.ERC_DRIVER_CONFLICT) == []
def test_driver_conflict_waiver_suppresses():
sch = _sch(
[
_comp("U1", [_pin("1", etype=PinType.OUTPUT)]),
_comp("U2", [_pin("1", etype=PinType.OUTPUT)]),
],
[_net("LED1_GPIO_RD", [("U1", "1"), ("U2", "1")])],
)
cfg = _cfg(waivers=[{"net": "LED1_GPIO_RD", "rule": "driver_conflict",
"reason": "shared open-drain STAT by design"}])
assert _by_code(erc.run(sch, cfg), erc.ERC_DRIVER_CONFLICT) == []
# without the waiver it fires
assert len(_by_code(erc.run(sch, _cfg()), erc.ERC_DRIVER_CONFLICT)) == 1
# ---------------------------------------------------------------------------
# floating input (TYPE-gated by confidence)
# ---------------------------------------------------------------------------
def test_floating_input_high_confidence_is_warning():
# INPUT + PASSIVE, no driver -> floating. confidence 0.5 -> WARNING.
sch = _sch(
[_comp("U1", [_pin("1", name="P0.25", etype=PinType.INPUT)]),
_comp("R1", [_pin("1")])],
[_net("SIG", [("U1", "1"), ("R1", "1")])],
)
fl = _by_code(erc.run(sch, _cfg()), erc.ERC_FLOATING_INPUT)
assert len(fl) == 1
assert fl[0].refs == ["U1.1"]
assert fl[0].severity is Severity.WARNING
def test_floating_input_low_confidence_is_downgraded_to_note():
extra = _comp("R2", [_pin(str(i)) for i in range(1, 9)]) # dilute confidence
sch = _sch(
[_comp("U1", [_pin("1", etype=PinType.INPUT)]),
_comp("R1", [_pin("1")]), extra],
[_net("SIG", [("U1", "1"), ("R1", "1")])],
)
fl = _by_code(erc.run(sch, _cfg()), erc.ERC_FLOATING_INPUT)
assert len(fl) == 1
assert fl[0].severity is Severity.NOTE
def test_input_with_driver_is_not_floating():
sch = _sch(
[_comp("U1", [_pin("1", etype=PinType.INPUT)]),
_comp("U2", [_pin("1", etype=PinType.OUTPUT)])],
[_net("SIG", [("U1", "1"), ("U2", "1")])],
)
assert _by_code(erc.run(sch, _cfg()), erc.ERC_FLOATING_INPUT) == []
def test_input_on_power_rail_is_not_floating():
# An INPUT tied to a detected rail is driven by the rail, not floating.
sch = _sch(
[_comp("U1", [_pin("1", etype=PinType.INPUT)]),
_comp("U2", [_pin("1")])],
[_net("GND", [("U1", "1"), ("U2", "1")], source_names=["GND"])],
)
assert _by_code(erc.run(sch, _cfg()), erc.ERC_FLOATING_INPUT) == []
# ---------------------------------------------------------------------------
# IC power / ground by NET IDENTITY (robust; not type-gated)
# ---------------------------------------------------------------------------
def _powered_board() -> Schematic:
# GND {U1.1, R1.1}; V3V3 {U1.2, U2.1}. U1 has both rails; U2 only power.
return _sch(
[
_comp("U1", [_pin("1"), _pin("2")]),
_comp("U2", [_pin("1")]),
_comp("R1", [_pin("1")]),
],
[
_net("GND", [("U1", "1"), ("R1", "1")], source_names=["GND"]),
_net("V3V3", [("U1", "2"), ("U2", "1")], source_names=["V3V3"]),
],
)
def test_ic_with_power_and_ground_is_clean():
out = erc.run(_powered_board(), _cfg())
assert not any(f.refs == ["U1"] for f in out
if f.code in (erc.ERC_NO_POWER, erc.ERC_NO_GROUND))
def test_ic_missing_ground_flagged_by_identity():
out = erc.run(_powered_board(), _cfg())
ng = _by_code(out, erc.ERC_NO_GROUND)
assert any(f.refs == ["U2"] for f in ng) # U2 touches no ground net
assert _by_code(out, erc.ERC_NO_POWER) == [] # U2 touches power -> ok
def test_no_power_or_ground_infra_skips_per_ic_checks():
# No GND/power net anywhere -> do not spam every IC (vacuous-board guard).
sch = _sch(
[_comp("U1", [_pin("1")]), _comp("U2", [_pin("1")])],
[_net("STAT", [("U1", "1"), ("U2", "1")])],
)
out = erc.run(sch, _cfg())
assert _by_code(out, erc.ERC_NO_POWER) == []
assert _by_code(out, erc.ERC_NO_GROUND) == []
def test_config_rail_name_detected_as_power():
# "MIDRAIL" matches no built-in pattern; config [[rail]] makes it power.
sch = _sch(
[_comp("U1", [_pin("1"), _pin("2")]), _comp("R1", [_pin("1")])],
[
_net("GND", [("U1", "1"), ("R1", "1")], source_names=["GND"]),
_net("MIDRAIL", [("U1", "2"), ("R1", "1")], source_names=["MIDRAIL"]),
],
)
cfg = _cfg(rails=[{"name": "MIDRAIL", "voltage": 1.65}])
out = erc.run(sch, cfg)
# U1 now touches both a (config) power net and ground -> not flagged.
assert not any(f.refs == ["U1"] for f in out
if f.code in (erc.ERC_NO_POWER, erc.ERC_NO_GROUND))
def test_no_power_waiver_suppresses():
# U2 lacks ground; a no_ground waiver keyed by its designator silences it.
cfg = _cfg(waivers=[{"net": "U2", "rule": "no_ground",
"reason": "analog-only part, no ground pin"}])
assert _by_code(erc.run(_powered_board(), cfg), erc.ERC_NO_GROUND) == []
# ---------------------------------------------------------------------------
# net-alias conflict -> NOTE (never an error)
# ---------------------------------------------------------------------------
def test_net_alias_is_a_note():
net = _net(
"STAT",
[("U1", "1"), ("U2", "1")],
aliases=["LED1_GPIO_RD"],
source_names=["STAT", "LED1_GPIO_RD"],
)
sch = _sch(
[_comp("U1", [_pin("1")]), _comp("U2", [_pin("1")])],
[net],
)
alias = _by_code(erc.run(sch, _cfg()), erc.ERC_NET_ALIAS)
assert len(alias) == 1
assert alias[0].severity is Severity.NOTE
assert "STAT" in alias[0].message and "LED1_GPIO_RD" in alias[0].message
def test_single_name_net_has_no_alias_note():
sch = _sch(
[_comp("U1", [_pin("1")]), _comp("U2", [_pin("1")])],
[_net("STAT", [("U1", "1"), ("U2", "1")])],
)
assert _by_code(erc.run(sch, _cfg()), erc.ERC_NET_ALIAS) == []
def test_net_alias_waiver_suppresses():
net = _net("STAT", [("U1", "1"), ("U2", "1")],
aliases=["LED1_GPIO_RD"], source_names=["STAT", "LED1_GPIO_RD"])
sch = _sch([_comp("U1", [_pin("1")]), _comp("U2", [_pin("1")])], [net])
cfg = _cfg(waivers=[{"net": "STAT", "rule": "net_alias", "reason": "ok"}])
assert _by_code(erc.run(sch, cfg), erc.ERC_NET_ALIAS) == []
# ---------------------------------------------------------------------------
# real-fixture smoke tests (parsed via the frozen Altium reader)
# ---------------------------------------------------------------------------
def test_real_fixture_runs():
sch = altium_sch.read(os.path.join(FIXTURES, "shared_name_label.SchDoc"))
out = erc.run(sch, _cfg())
assert all(isinstance(f, Finding) for f in out)
# All four pins are Passive -> the type-based rules must stay silent.
assert _by_code(out, erc.ERC_DRIVER_CONFLICT) == []
assert _by_code(out, erc.ERC_FLOATING_INPUT) == []
def test_real_fixture_no_erc_suppresses_dangling():
# no_erc.SchDoc: U1.2 is an open pin carrying a RECORD-22 No-ERC marker.
sch = altium_sch.read(os.path.join(FIXTURES, "no_erc.SchDoc"))
assert sch.no_erc_points # reader surfaced the marker
out = erc.run(sch, _cfg())
# the lone single-pin net (U1.2) is suppressed by the No-ERC marker
assert _by_code(out, erc.ERC_DANGLING_NET) == []
@pytest.mark.parametrize(
"fixture",
[
"shared_name_label.SchDoc",
"junction_cross.SchDoc",
"no_erc.SchDoc",
"t_junction.SchDoc",
"two_gnd_ports.SchDoc",
],
)
def test_all_fixtures_run_without_error(fixture):
sch = altium_sch.read(os.path.join(FIXTURES, fixture))
out = erc.run(sch, _cfg())
assert isinstance(out, list)
assert all(isinstance(f, Finding) for f in out)