|
65 | 65 |
|
66 | 66 | def _make_hass(tmp_path) -> MagicMock: |
67 | 67 | hass = MagicMock(name="hass") |
| 68 | + hass.config.skip_pip = False |
68 | 69 | hass.config.path = lambda sub: str(tmp_path / sub) |
69 | 70 |
|
70 | 71 | async def _executor(func, *args): |
@@ -413,6 +414,95 @@ def test_explicit_override_wins_over_auto_update_off(self, tmp_path, monkeypatch |
413 | 414 |
|
414 | 415 |
|
415 | 416 | class TestEnsurePackage: |
| 417 | + async def test_skip_pip_uses_compatible_externally_managed_package( |
| 418 | + self, tmp_path, monkeypatch |
| 419 | + ): |
| 420 | + """skip_pip must bypass every package mutation and preserve markers.""" |
| 421 | + data = { |
| 422 | + DATA_SECRET_PATH: "/p", |
| 423 | + DATA_LAST_PIP_SPEC: "ha-mcp==7.11.0", |
| 424 | + DATA_PENDING_INSTALL_VERSION: "7.12.0", |
| 425 | + } |
| 426 | + mgr, hass, entry = _manager( |
| 427 | + tmp_path, |
| 428 | + options={OPT_PIP_SPEC: "ha-mcp==99.0.0"}, |
| 429 | + data=data, |
| 430 | + ) |
| 431 | + hass.config.skip_pip = True |
| 432 | + process = AsyncMock(side_effect=AssertionError("requirements mutation")) |
| 433 | + force_install = MagicMock(side_effect=AssertionError("package install")) |
| 434 | + uninstall = MagicMock(side_effect=AssertionError("package uninstall")) |
| 435 | + monkeypatch.setattr(es, "async_process_requirements", process) |
| 436 | + monkeypatch.setattr(es, "_force_install_package", force_install) |
| 437 | + monkeypatch.setattr(es, "_uninstall_distribution", uninstall) |
| 438 | + monkeypatch.setattr(es, "_installed_ha_mcp_version", lambda: "7.12.1") |
| 439 | + monkeypatch.setattr( |
| 440 | + es, |
| 441 | + "_installed_dist_version", |
| 442 | + lambda dist: "7.12.1" if dist == DIST_NAME_STABLE else None, |
| 443 | + ) |
| 444 | + |
| 445 | + version = await mgr._async_ensure_package() |
| 446 | + |
| 447 | + assert version == "7.12.1" |
| 448 | + assert entry.data == data |
| 449 | + |
| 450 | + async def test_skip_pip_reports_missing_externally_managed_package( |
| 451 | + self, tmp_path, monkeypatch |
| 452 | + ): |
| 453 | + mgr, hass, _entry = _manager(tmp_path) |
| 454 | + hass.config.skip_pip = True |
| 455 | + monkeypatch.setattr(es, "_installed_ha_mcp_version", lambda: None) |
| 456 | + monkeypatch.setattr(es, "_installed_dist_version", lambda _dist: None) |
| 457 | + |
| 458 | + with pytest.raises( |
| 459 | + es.EmbeddedServerError, |
| 460 | + match=r"skip_pip.*system package manager.*7\.10\.0", |
| 461 | + ) as exc_info: |
| 462 | + await mgr._async_ensure_package() |
| 463 | + |
| 464 | + assert exc_info.value.kind == "package" |
| 465 | + |
| 466 | + async def test_skip_pip_reports_incompatible_externally_managed_package( |
| 467 | + self, tmp_path, monkeypatch |
| 468 | + ): |
| 469 | + mgr, hass, _entry = _manager(tmp_path) |
| 470 | + hass.config.skip_pip = True |
| 471 | + monkeypatch.setattr(es, "_installed_ha_mcp_version", lambda: "7.9.0") |
| 472 | + monkeypatch.setattr( |
| 473 | + es, |
| 474 | + "_installed_dist_version", |
| 475 | + lambda dist: "7.9.0" if dist == DIST_NAME_STABLE else None, |
| 476 | + ) |
| 477 | + |
| 478 | + with pytest.raises( |
| 479 | + es.EmbeddedServerError, |
| 480 | + match=r"externally managed ha-mcp 7\.9\.0.*7\.10\.0 or newer", |
| 481 | + ) as exc_info: |
| 482 | + await mgr._async_ensure_package() |
| 483 | + |
| 484 | + assert exc_info.value.kind == "package" |
| 485 | + |
| 486 | + async def test_skip_pip_reports_ambiguous_externally_managed_packages( |
| 487 | + self, tmp_path, monkeypatch |
| 488 | + ): |
| 489 | + mgr, hass, _entry = _manager(tmp_path) |
| 490 | + hass.config.skip_pip = True |
| 491 | + versions = { |
| 492 | + DIST_NAME_STABLE: "7.12.1", |
| 493 | + DIST_NAME_DEV: "7.13.0.dev1", |
| 494 | + } |
| 495 | + monkeypatch.setattr(es, "_installed_ha_mcp_version", lambda: "7.12.1") |
| 496 | + monkeypatch.setattr(es, "_installed_dist_version", versions.get) |
| 497 | + |
| 498 | + with pytest.raises( |
| 499 | + es.EmbeddedServerError, |
| 500 | + match=r"Both ha-mcp 7\.12\.1 and ha-mcp-dev 7\.13\.0\.dev1", |
| 501 | + ) as exc_info: |
| 502 | + await mgr._async_ensure_package() |
| 503 | + |
| 504 | + assert exc_info.value.kind == "package" |
| 505 | + |
416 | 506 | async def test_fast_path_only_for_unchanged_override(self, tmp_path, monkeypatch): |
417 | 507 | # The fast path is reserved for an explicit pip-spec override: an |
418 | 508 | # unchanged, already-installed pin delegates the "already satisfied?" |
|
0 commit comments