|
2 | 2 | from ast import literal_eval
|
3 | 3 | import asyncio
|
4 | 4 | from datetime import datetime as dt
|
| 5 | +from logging import getLogger |
5 | 6 | import pathlib
|
6 | 7 |
|
7 | 8 | from custom_components.pyscript.const import DOMAIN
|
|
18 | 19 | from homeassistant.helpers.service import async_get_all_descriptions
|
19 | 20 | from homeassistant.setup import async_setup_component
|
20 | 21 |
|
| 22 | +_LOGGER = getLogger(__name__) |
| 23 | + |
21 | 24 |
|
22 | 25 | async def setup_script(hass, notify_q, now, source):
|
23 | 26 | """Initialize and load the given pyscript."""
|
@@ -441,7 +444,7 @@ def func5(var_name=None, value=None):
|
441 | 444 | with patch("custom_components.pyscript.os.path.isdir", return_value=True), patch(
|
442 | 445 | "custom_components.pyscript.glob.iglob", return_value=scripts
|
443 | 446 | ), 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 |
445 | 448 | ), patch(
|
446 | 449 | "custom_components.pyscript.trigger.dt_now", return_value=now
|
447 | 450 | ), patch(
|
@@ -482,3 +485,27 @@ async def test_misc_errors(hass, caplog):
|
482 | 485 | assert "State class is not meant to be instantiated" in caplog.text
|
483 | 486 | assert "Event class is not meant to be instantiated" in caplog.text
|
484 | 487 | 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