Merge pull request #238 from lucaspirola/fix/collision-warning-test-caplog

test: make collision-warning test robust to basicConfig(force=True)
This commit is contained in:
mixelpixx
2026-06-12 12:11:13 -04:00
committed by GitHub

View File

@@ -785,13 +785,31 @@ class TestSynthesizeTouchingPinWires:
class TestOldToNewCollision: class TestOldToNewCollision:
"""Verify that coincident pins do not silently overwrite each other in old_to_new.""" """Verify that coincident pins do not silently overwrite each other in old_to_new."""
def test_handler_logs_warning_on_collision(self, caplog: Any) -> None: def test_handler_logs_warning_on_collision(self) -> None:
""" """
When two pins share the same old position, a warning should be logged When two pins share the same old position, a warning should be logged
and the *first* mapping should be kept (not overwritten by the second). and the *first* mapping should be kept (not overwritten by the second).
""" """
import logging import logging
# Capture on the "kicad_interface" logger with our own handler rather
# than pytest's ``caplog``: the package calls
# ``logging.basicConfig(..., force=True)`` at import time, which detaches
# the root-level handler ``caplog`` reads from and silently drops records.
records = []
class _Capture(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
records.append(record)
kicad_logger = logging.getLogger("kicad_interface")
handler = _Capture(level=logging.WARNING)
prev_level = kicad_logger.level
prev_propagate = kicad_logger.propagate
kicad_logger.addHandler(handler)
kicad_logger.setLevel(logging.WARNING)
kicad_logger.propagate = False # keep the warning out of stderr/file handlers
try:
# Build a fake pin_positions dict with a deliberate collision # Build a fake pin_positions dict with a deliberate collision
pin_positions = { pin_positions = {
"1": ((0.0, 3.81), (10.0, 23.81)), "1": ((0.0, 3.81), (10.0, 23.81)),
@@ -799,24 +817,24 @@ class TestOldToNewCollision:
} }
old_to_new = {} old_to_new = {}
with caplog.at_level(logging.WARNING, logger="kicad_interface"):
for _pin, (old_xy, new_xy) in pin_positions.items(): for _pin, (old_xy, new_xy) in pin_positions.items():
if old_xy in old_to_new: if old_xy in old_to_new:
import logging as _logging kicad_logger.warning(
logger_inner = _logging.getLogger("kicad_interface")
logger_inner.warning(
f"move_schematic_component: pin {_pin!r} shares old position {old_xy} " f"move_schematic_component: pin {_pin!r} shares old position {old_xy} "
f"with another pin; keeping first entry, skipping duplicate" f"with another pin; keeping first entry, skipping duplicate"
) )
continue continue
old_to_new[old_xy] = new_xy old_to_new[old_xy] = new_xy
finally:
kicad_logger.removeHandler(handler)
kicad_logger.setLevel(prev_level)
kicad_logger.propagate = prev_propagate
# Only one entry should exist, and it should be the first one # Only one entry should exist, and it should be the first one
assert len(old_to_new) == 1 assert len(old_to_new) == 1
assert old_to_new[(0.0, 3.81)] == (10.0, 23.81) assert old_to_new[(0.0, 3.81)] == (10.0, 23.81)
# Warning should have been logged # Warning should have been logged
assert any("skipping duplicate" in r.message for r in caplog.records) assert any("skipping duplicate" in r.getMessage() for r in records)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------