Skip to content

Commit 0921689

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

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from ast import literal_eval
33
import asyncio
44
from datetime import datetime as dt
5+
from logging import getLogger
56
import pathlib
67

78
from custom_components.pyscript.const import DOMAIN
@@ -18,6 +19,8 @@
1819
from homeassistant.helpers.service import async_get_all_descriptions
1920
from homeassistant.setup import async_setup_component
2021

22+
_LOGGER = getLogger(__name__)
23+
2124

2225
async def setup_script(hass, notify_q, now, source):
2326
"""Initialize and load the given pyscript."""
@@ -441,7 +444,7 @@ def func5(var_name=None, value=None):
441444
with patch("custom_components.pyscript.os.path.isdir", return_value=True), patch(
442445
"custom_components.pyscript.glob.iglob", return_value=scripts
443446
), patch(
444-
"custom_components.pyscript.global_ctx.open", mock_open(read_data=next_source), create=True,
447+
"custom_components.pyscript.global_ctx.open", mock_open(read_data=next_source), create=True
445448
), patch(
446449
"custom_components.pyscript.trigger.dt_now", return_value=now
447450
), patch(
@@ -482,3 +485,27 @@ async def test_misc_errors(hass, caplog):
482485
assert "State class is not meant to be instantiated" in caplog.text
483486
assert "Event class is not meant to be instantiated" in caplog.text
484487
assert "TrigTime class is not meant to be instantiated" in caplog.text
488+
489+
490+
async def test_install_requirements(hass):
491+
"""Test install_requirements function."""
492+
requirements = """
493+
pytube==9.7.0
494+
# another test comment
495+
pykakasi==2.0.1 # test comment
496+
497+
"""
498+
499+
with patch("os.path.exists", return_value=True), patch(
500+
"custom_components.pyscript.async_hass_config_yaml", return_value={}
501+
), patch("custom_components.pyscript.open", mock_open(read_data=requirements), create=True,), patch(
502+
"custom_components.pyscript.async_process_requirements"
503+
) as install_requirements:
504+
await setup_script(hass, None, dt(2020, 7, 1, 11, 59, 59, 999999), "")
505+
assert install_requirements.call_args[0][2] == ["pytube==9.7.0", "pykakasi==2.0.1"]
506+
install_requirements.reset_mock()
507+
# Because in tests, packages are not installed, we fake that they are
508+
# installed so we can test that we don't attempt to install them
509+
with patch("custom_components.pyscript.installed_version", return_value="2.0.1"):
510+
await hass.services.async_call("pyscript", "reload", {}, blocking=True)
511+
assert not install_requirements.called

0 commit comments

Comments
 (0)