Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure startup can proceed when there is package metadata cruft #47706

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion homeassistant/util/package.py
Expand Up @@ -34,14 +34,24 @@ def is_installed(package: str) -> bool:
Returns False when the package is not installed or doesn't meet req.
"""
try:
pkg_resources.get_distribution(package)
return True
except (pkg_resources.ResolutionError, pkg_resources.ExtractionError):
bdraco marked this conversation as resolved.
Show resolved Hide resolved
req = pkg_resources.Requirement.parse(package)
except ValueError:
# This is a zip file. We no longer use this in Home Assistant,
# leaving it in for custom components.
req = pkg_resources.Requirement.parse(urlparse(package).fragment)

try:
return version(req.project_name) in req
installed_version = version(req.project_name)
# This will happen when an install failed or
# was aborted while in progress see
# https://github.com/home-assistant/core/issues/47699
if installed_version is None:
_LOGGER.error("Installed version for %s resolved to None", req.project_name) # type: ignore
return False
return installed_version in req
except PackageNotFoundError:
return False

Expand Down
47 changes: 46 additions & 1 deletion tests/util/test_package.py
Expand Up @@ -239,10 +239,55 @@ async def test_async_get_user_site(mock_env_copy):

def test_check_package_global():
"""Test for an installed package."""
installed_package = list(pkg_resources.working_set)[0].project_name
first_package = list(pkg_resources.working_set)[0]
installed_package = first_package.project_name
installed_version = first_package.version

assert package.is_installed(installed_package)
assert package.is_installed(f"{installed_package}=={installed_version}")
assert package.is_installed(f"{installed_package}>={installed_version}")
assert package.is_installed(f"{installed_package}<={installed_version}")
assert not package.is_installed(f"{installed_package}<{installed_version}")


def test_check_package_version_does_not_match():
"""Test for version mismatch."""
installed_package = list(pkg_resources.working_set)[0].project_name
assert not package.is_installed(f"{installed_package}==999.999.999")
assert not package.is_installed(f"{installed_package}>=999.999.999")


def test_check_package_zip():
"""Test for an installed zip package."""
assert not package.is_installed(TEST_ZIP_REQ)


def test_get_distribution_falls_back_to_version():
"""Test for get_distribution failing and fallback to version."""
first_package = list(pkg_resources.working_set)[0]
installed_package = first_package.project_name
installed_version = first_package.version

with patch(
"homeassistant.util.package.pkg_resources.get_distribution",
side_effect=pkg_resources.ExtractionError,
):
assert package.is_installed(installed_package)
assert package.is_installed(f"{installed_package}=={installed_version}")
assert package.is_installed(f"{installed_package}>={installed_version}")
assert package.is_installed(f"{installed_package}<={installed_version}")
assert not package.is_installed(f"{installed_package}<{installed_version}")


def test_check_package_previous_failed_install():
"""Test for when a previously install package failed and left cruft behind."""
first_package = list(pkg_resources.working_set)[0]
installed_package = first_package.project_name
installed_version = first_package.version

with patch(
"homeassistant.util.package.pkg_resources.get_distribution",
side_effect=pkg_resources.ExtractionError,
), patch("homeassistant.util.package.version", return_value=None):
assert not package.is_installed(installed_package)
assert not package.is_installed(f"{installed_package}=={installed_version}")