Skip to content

Commit 30db0dc

Browse files
committed
small tweaks and add a test
1 parent 40d0d18 commit 30db0dc

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

custom_components/pyscript/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
if sys.version_info[:2] >= (3, 8):
4545
from importlib.metadata import ( # pylint: disable=no-name-in-module,import-error
4646
PackageNotFoundError,
47-
version,
47+
version as installed_version,
4848
)
4949
else:
5050
from importlib_metadata import ( # pylint: disable=import-error
5151
PackageNotFoundError,
52-
version,
52+
version as installed_version,
5353
)
5454

5555
_LOGGER = logging.getLogger(LOGGER_PATH)
@@ -280,13 +280,14 @@ async def install_requirements(hass):
280280
# https://rosettacode.org/wiki/Strip_comments_from_a_string#Python
281281
i = pkg.find("#")
282282
if i >= 0:
283-
pkg = pkg[:i].strip()
283+
pkg = pkg[:i]
284+
pkg = pkg.strip()
284285

285286
try:
286287
# Attempt to get version of package. Do nothing if it's found since
287288
# we want to use the version that's already installed to be safe
288289
requirement = pkg_resources.Requirement.parse(pkg)
289-
requirement_installed_version = version(requirement.project_name)
290+
requirement_installed_version = installed_version(requirement.project_name)
290291

291292
if requirement_installed_version in requirement:
292293
_LOGGER.debug("`%s` already found", requirement.project_name)
@@ -307,7 +308,7 @@ async def install_requirements(hass):
307308
# Not valid requirements line so it can be skipped
308309
_LOGGER.debug("Ignoring `%s` because it is not a valid package", pkg)
309310
if requirements_to_install:
310-
_LOGGER.info("Installing the following packages: %s", ",".join(requirements_to_install))
311+
_LOGGER.info("Installing the following packages: %s", ", ".join(requirements_to_install))
311312
await async_process_requirements(hass, DOMAIN, requirements_to_install)
312313
else:
313314
_LOGGER.info("All requirements are already available.")

tests/test_init.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,27 @@ async def test_misc_errors(hass, caplog):
482482
assert "State class is not meant to be instantiated" in caplog.text
483483
assert "Event class is not meant to be instantiated" in caplog.text
484484
assert "TrigTime class is not meant to be instantiated" in caplog.text
485+
486+
487+
async def test_install_requirements(hass):
488+
"""Test install_requirements function."""
489+
requirements = """
490+
pytube==9.7.0
491+
# another test comment
492+
pykakasi==2.0.1 # test comment
493+
494+
"""
495+
496+
with patch("os.path.exists", return_value=True), patch(
497+
"custom_components.pyscript.async_hass_config_yaml", return_value={}
498+
), patch("custom_components.pyscript.open", mock_open(read_data=requirements), create=True,), patch(
499+
"custom_components.pyscript.async_process_requirements"
500+
) as install_requirements:
501+
await setup_script(hass, None, dt(2020, 7, 1, 11, 59, 59, 999999), "")
502+
assert install_requirements.call_args[0][2] == ["pytube==9.7.0", "pykakasi==2.0.1"]
503+
install_requirements.reset_mock()
504+
# Because in tests, packages are not installed, we fake that they are
505+
# installed so we can test that we don't attempt to install them
506+
with patch("custom_components.pyscript.installed_version", return_value="2.0.1"):
507+
await hass.services.async_call("pyscript", "reload", {}, blocking=True)
508+
assert not install_requirements.called

0 commit comments

Comments
 (0)