Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
"""
Test configuration for python/tests.
Sets up sys.modules stubs for heavy KiCAD modules (pcbnew, skip) before any
test module can trigger their import, preventing crashes on systems where the
real KiCAD environment is not fully initialised for testing.
"""
import os
import sys
import types
from pathlib import Path
from unittest.mock import MagicMock
# Make the repository's Python package tree importable in a clean checkout.
# Individual test modules must not depend on the caller's PYTHONPATH or on an
# editable install that CI does not perform.
_PYTHON_ROOT = Path(__file__).resolve().parents[1] / "python"
if str(_PYTHON_ROOT) not in sys.path:
sys.path.insert(0, str(_PYTHON_ROOT))
# Skip the speculative full-symbol-library warm-up in tests. Each KiCADInterface
# would otherwise spawn a daemon thread parsing every installed .kicad_sym file;
# constructing an interface per test then piles up concurrent warm threads that
# saturate the CPU. On-demand parses still work (and are cached process-wide).
os.environ.setdefault("KICAD_SKIP_SYMBOL_WARMUP", "1")
# ---------------------------------------------------------------------------
# pcbnew stub — kicad_interface.py accesses pcbnew.__file__ and
# pcbnew.GetBuildVersion() at module level. Use MagicMock so that any
# attribute access (pcbnew.BOARD, pcbnew.PCB_TRACK, …) returns a mock
# rather than raising AttributeError.
# ---------------------------------------------------------------------------
if os.environ.get("KICAD_USE_REAL_PCBNEW") != "1":
_pcbnew = MagicMock(name="pcbnew")
_pcbnew.__file__ = "/fake/pcbnew.cpython-313-x86_64-linux-gnu.so"
_pcbnew.__name__ = "pcbnew"
_pcbnew.__spec__ = None
_pcbnew.GetBuildVersion.return_value = "9.0.0-stub"
sys.modules["pcbnew"] = _pcbnew
# ---------------------------------------------------------------------------
# Stub: skip (kicad-skip — use real module if available, stub otherwise)
# ---------------------------------------------------------------------------
try:
import skip as _skip_test # noqa: F401 — try importing real skip
except ImportError:
skip_mod = types.ModuleType("skip")
class _FakeSchematic:
"""Minimal stand-in for skip.Schematic used in PinLocator cache."""
def __init__(self, path: str):
self.path = path
self.symbol = []
skip_mod.Schematic = _FakeSchematic # type: ignore[attr-defined]
sys.modules["skip"] = skip_mod
670d2a9c4This file is pinned to the selected release. Download the original to open it in your design tool.