|
| 1 | +# SPDX-FileCopyrightText: 2026 Melissa LeBlanc-Williams for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +"""Tests for runtime platform dependency handling.""" |
| 6 | + |
| 7 | +from types import SimpleNamespace |
| 8 | + |
| 9 | +from adafruit_blinka import platform_dependencies |
| 10 | + |
| 11 | + |
| 12 | +def _detector(chip_id=None, **board_values): |
| 13 | + values = { |
| 14 | + "any_raspberry_pi_5_board": False, |
| 15 | + "any_raspberry_pi": False, |
| 16 | + "any_jetson_board": False, |
| 17 | + "any_beaglebone": False, |
| 18 | + } |
| 19 | + values.update(board_values) |
| 20 | + return SimpleNamespace( |
| 21 | + board=SimpleNamespace(**values), chip=SimpleNamespace(id=chip_id) |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def test_raspberry_pi_5_uses_adafruit_lgpio_on_python_313(): |
| 26 | + dependencies = platform_dependencies.get_platform_dependencies( |
| 27 | + _detector(any_raspberry_pi_5_board=True, any_raspberry_pi=True), |
| 28 | + python_version=(3, 13), |
| 29 | + ) |
| 30 | + |
| 31 | + assert dependencies == [ |
| 32 | + ("lgpio", "adafruit-lgpio>=0.2.2.0"), |
| 33 | + ( |
| 34 | + "adafruit_raspberry_pi5_neopixel_write", |
| 35 | + "Adafruit-Blinka-Raspberry-Pi5-Neopixel", |
| 36 | + ), |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +def test_raspberry_pi_5_uses_upstream_lgpio_before_python_313(): |
| 41 | + dependencies = platform_dependencies.get_platform_dependencies( |
| 42 | + _detector(any_raspberry_pi_5_board=True, any_raspberry_pi=True), |
| 43 | + python_version=(3, 12), |
| 44 | + ) |
| 45 | + |
| 46 | + assert ("lgpio", "lgpio>=0.2.2.0") in dependencies |
| 47 | + |
| 48 | + |
| 49 | +def test_raspberry_pi_5_neopixel_requires_python_311(): |
| 50 | + dependencies = platform_dependencies.get_platform_dependencies( |
| 51 | + _detector(any_raspberry_pi_5_board=True, any_raspberry_pi=True), |
| 52 | + python_version=(3, 10), |
| 53 | + ) |
| 54 | + |
| 55 | + assert all( |
| 56 | + module_name != "adafruit_raspberry_pi5_neopixel_write" |
| 57 | + for module_name, _ in dependencies |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def test_earlier_raspberry_pi_does_not_install_lgpio(): |
| 62 | + dependencies = platform_dependencies.get_platform_dependencies( |
| 63 | + _detector(any_raspberry_pi=True), python_version=(3, 13) |
| 64 | + ) |
| 65 | + |
| 66 | + assert dependencies == [ |
| 67 | + ("RPi.GPIO", "RPi.GPIO"), |
| 68 | + ("_rpi_ws281x", "rpi_ws281x>=4.0.0"), |
| 69 | + ] |
| 70 | + |
| 71 | + |
| 72 | +def test_import_requirement_uses_detected_python_version(): |
| 73 | + detector = _detector(any_raspberry_pi_5_board=True, any_raspberry_pi=True) |
| 74 | + |
| 75 | + requirement = platform_dependencies.get_platform_requirement_for_import( |
| 76 | + detector, "lgpio", python_version=(3, 12) |
| 77 | + ) |
| 78 | + |
| 79 | + assert requirement == "lgpio>=0.2.2.0" |
| 80 | + |
| 81 | + |
| 82 | +def test_installer_uses_running_python(monkeypatch): |
| 83 | + detector = _detector(chip_id="AM33XX", any_beaglebone=True) |
| 84 | + commands = [] |
| 85 | + |
| 86 | + monkeypatch.setattr( |
| 87 | + platform_dependencies, |
| 88 | + "get_missing_platform_dependencies", |
| 89 | + lambda *_args, **_kwargs: ["Adafruit_BBIO>=1.2.4"], |
| 90 | + ) |
| 91 | + monkeypatch.setattr(platform_dependencies.sys.stdin, "isatty", lambda: True) |
| 92 | + monkeypatch.setattr(platform_dependencies.sys.stdout, "isatty", lambda: True) |
| 93 | + monkeypatch.setattr( |
| 94 | + platform_dependencies.subprocess, |
| 95 | + "run", |
| 96 | + lambda command, check: commands.append((command, check)), |
| 97 | + ) |
| 98 | + |
| 99 | + installed = platform_dependencies.install_missing_platform_dependencies( |
| 100 | + detector, input_func=lambda _prompt: "y" |
| 101 | + ) |
| 102 | + |
| 103 | + assert installed is True |
| 104 | + assert commands == [ |
| 105 | + ( |
| 106 | + [ |
| 107 | + platform_dependencies.sys.executable, |
| 108 | + "-m", |
| 109 | + "pip", |
| 110 | + "install", |
| 111 | + "Adafruit_BBIO>=1.2.4", |
| 112 | + ], |
| 113 | + True, |
| 114 | + ) |
| 115 | + ] |
| 116 | + |
| 117 | + |
| 118 | +def test_installer_skips_prompt_without_terminal(monkeypatch): |
| 119 | + detector = _detector(chip_id="AM33XX", any_beaglebone=True) |
| 120 | + prompted = [] |
| 121 | + |
| 122 | + monkeypatch.setattr( |
| 123 | + platform_dependencies, |
| 124 | + "get_missing_platform_dependencies", |
| 125 | + lambda *_args, **_kwargs: ["Adafruit_BBIO>=1.2.4"], |
| 126 | + ) |
| 127 | + monkeypatch.setattr(platform_dependencies.sys.stdin, "isatty", lambda: False) |
| 128 | + |
| 129 | + installed = platform_dependencies.install_missing_platform_dependencies( |
| 130 | + detector, input_func=prompted.append |
| 131 | + ) |
| 132 | + |
| 133 | + assert installed is False |
| 134 | + assert not prompted |
0 commit comments