diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4799e5ca2068..d21869d5c997 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -79,7 +79,7 @@ repos: pass_filenames: false language: script types: [text] - files: ^(homeassistant/.+/manifest\.json|homeassistant/brands/.+\.json|pyproject\.toml|\.pre-commit-config\.yaml|script/gen_requirements_all\.py|script/hassfest/requirements_util\.py)$ + files: ^(homeassistant/.+/manifest\.json|homeassistant/brands/.+\.json|pyproject\.toml|requirements_test\.txt|packages/pytest-home-assistant/pyproject\.toml|\.pre-commit-config\.yaml|script/gen_requirements_all\.py|script/hassfest/requirements_util\.py)$ - id: gen_copilot_instructions name: gen_copilot_instructions entry: script/run-in-env.sh python3 -m script.gen_copilot_instructions diff --git a/packages/pytest-home-assistant/pyproject.toml b/packages/pytest-home-assistant/pyproject.toml index 8d317bd846ce..a1f37078bb8f 100644 --- a/packages/pytest-home-assistant/pyproject.toml +++ b/packages/pytest-home-assistant/pyproject.toml @@ -9,11 +9,41 @@ license = "Apache-2.0" license-files = ["LICENSE.md"] description = "Pytest fixtures and test helpers for Home Assistant." readme = "README.md" -requires-python = ">=3.14.0" +requires-python = ">=3.14.2" authors = [ { name = "The Home Assistant Authors", email = "hello@home-assistant.io" }, ] +# Dependency pins are synchronized by script.gen_requirements_all. +dependencies = [ + "homeassistant", + "aiohasupervisor==0.7.0", + "bleak-retry-connector==4.7.1", + "freezegun==1.5.5", + "paho-mqtt==2.1.0", + "pytest==9.0.3", + "pytest-aiohttp==1.1.1", + "pytest-asyncio==1.4.0", + "pytest-freezer==0.4.9", + "pytest-socket==0.8.1", + "pytest-xdist==3.8.0", + "requests-mock==1.12.1", + "respx==0.23.1", + "syrupy==6.1.1", +] + +[project.optional-dependencies] +bluetooth = [ + "aiousbwatcher==1.1.2", + "bluetooth-adapters==2.4.0", + "bluetooth-auto-recovery==1.6.4", + "bluetooth-data-tools==1.29.24", + "habluetooth==7.1.2", + "serialx==1.14.0", +] +infrared = ["infrared-protocols==10.3.0"] +radio-frequency = ["rf-protocols==4.3.0"] + [project.urls] Repository = "https://github.com/home-assistant/core" Documentation = "https://contributors.home-assistant.io" diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 9183d86e5eb7..83f57a306712 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -3,6 +3,7 @@ import difflib import importlib +import json from operator import itemgetter from pathlib import Path import pkgutil @@ -271,6 +272,8 @@ GENERATED_MESSAGE = ( f"# Automatically generated by {Path(__file__).name}, do not edit\n\n" ) +PYTEST_PACKAGE_PATH = Path("packages/pytest-home-assistant/pyproject.toml") + MAP_HOOK_ID_TO_PACKAGE = { "ruff-check": "ruff", } @@ -558,6 +561,38 @@ def requirements_pre_commit_output() -> str: return "\n".join(output) + "\n" +def pytest_package_output(reqs: dict[str, list[str]]) -> str: + """Keep pytest helper dependencies aligned with Core and test requirements.""" + test_requirements = [ + line + for line in Path("requirements_test.txt").read_text().splitlines() + if line and not line.startswith(("#", "-")) + ] + versions = {} + for value in [*reqs, *core_requirements(), *test_requirements]: + requirement = Requirement(value) + versions[canonicalize_name(requirement.name)] = requirement.specifier + content = PYTEST_PACKAGE_PATH.read_text() + project = tomllib.loads(content)["project"] + dependencies = [ + *project["dependencies"], + *( + dependency + for extra in project["optional-dependencies"].values() + for dependency in extra + ), + ] + for dependency in dependencies: + requirement = Requirement(dependency) + name = canonicalize_name(requirement.name) + if name == "homeassistant": + # The matching Home Assistant version is set when publishing. + continue + requirement.specifier = versions[name] + content = content.replace(json.dumps(dependency), json.dumps(str(requirement))) + return content + + def gather_constraints() -> str: """Construct output for constraint file.""" return ( @@ -618,6 +653,7 @@ def main(validate: bool, ci: bool) -> int: ("requirements_all.txt", reqs_all_file), ("requirements_test_pre_commit.txt", reqs_pre_commit_file), ("homeassistant/package_constraints.txt", constraints), + (str(PYTEST_PACKAGE_PATH), pytest_package_output(data)), ] if ci: files.extend( diff --git a/tests/script/test_gen_requirements_all.py b/tests/script/test_gen_requirements_all.py index a96b91866897..b2d16d58cf62 100644 --- a/tests/script/test_gen_requirements_all.py +++ b/tests/script/test_gen_requirements_all.py @@ -1,5 +1,7 @@ """Tests for the gen_requirements_all script.""" +from pathlib import Path +import tomllib from unittest.mock import patch import pytest @@ -8,6 +10,47 @@ from script import gen_requirements_all from script.hassfest.requirements_util import _normalize_package_name +def test_pytest_package_dependency_versions( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Sync base and optional pins without changing publish metadata or extras.""" + monkeypatch.chdir(tmp_path) + Path("pyproject.toml").write_text('[project]\ndependencies = ["core-lib==2.0.0"]\n') + Path("requirements_test.txt").write_text( + "# Test dependencies\n-c constraints.txt\n\ntest_lib==3.0.0\n" + ) + package = gen_requirements_all.PYTEST_PACKAGE_PATH + package.parent.mkdir(parents=True) + package.write_text( + """[project] +version = "0.0.0" +dependencies = ["homeassistant", "core-lib==1.0.0", "test-lib==1.0.0"] + +[project.optional-dependencies] +radio = ["radio-lib[scan]==1.0.0; sys_platform == 'linux'"] + +[project.entry-points.pytest11] +pytest_homeassistant = "pytest_homeassistant.bootstrap" +""" + ) + output = gen_requirements_all.pytest_package_output( + {"radio-lib[other]==4.0.0": ["homeassistant.components.radio"]} + ) + project = tomllib.loads(output)["project"] + assert project["version"] == "0.0.0" + assert project["dependencies"] == [ + "homeassistant", + "core-lib==2.0.0", + "test-lib==3.0.0", + ] + assert project["optional-dependencies"] == { + "radio": ['radio-lib[scan]==4.0.0; sys_platform == "linux"'] + } + assert project["entry-points"] == { + "pytest11": {"pytest_homeassistant": "pytest_homeassistant.bootstrap"} + } + + def test_overrides_normalized() -> None: """Test override lists are using normalized package names.""" for req in gen_requirements_all.EXCLUDED_REQUIREMENTS_ALL: