|
4 | 4 | from datetime import datetime as dt
|
5 | 5 | import pathlib
|
6 | 6 |
|
7 |
| -from custom_components.pyscript.const import DOMAIN |
| 7 | +from custom_components.pyscript import process_all_requirements |
| 8 | +from custom_components.pyscript.const import DOMAIN, REQUIREMENTS_FILE, REQUIREMENTS_PATHS |
8 | 9 | from custom_components.pyscript.event import Event
|
9 | 10 | from custom_components.pyscript.function import Function
|
10 | 11 | from custom_components.pyscript.global_ctx import GlobalContextMgr
|
@@ -33,15 +34,8 @@ async def setup_script(hass, notify_q, now, source):
|
33 | 34 | ), patch(
|
34 | 35 | "homeassistant.config.load_yaml_config_file", return_value={}
|
35 | 36 | ), patch(
|
36 |
| - "custom_components.pyscript.load_all_requirement_lines", |
37 |
| - return_value={ |
38 |
| - "/some/config/dir/pyscript/requirements.txt": [ |
39 |
| - "pytube==9.7.0\n", |
40 |
| - "# another test comment\n", |
41 |
| - "pykakasi==2.0.1 # test comment\n", |
42 |
| - "\n", |
43 |
| - ] |
44 |
| - }, |
| 37 | + "custom_components.pyscript.process_all_requirements", |
| 38 | + return_value={"/some/config/dir/pyscript/requirements.txt": ["pytube==2.0.1", "pykakasi==2.0.1"]}, |
45 | 39 | ):
|
46 | 40 | assert await async_setup_component(hass, "pyscript", {DOMAIN: {}})
|
47 | 41 |
|
@@ -457,14 +451,9 @@ def func5(var_name=None, value=None):
|
457 | 451 | ), patch(
|
458 | 452 | "homeassistant.config.load_yaml_config_file", return_value={}
|
459 | 453 | ), patch(
|
460 |
| - "custom_components.pyscript.load_all_requirement_lines", |
| 454 | + "custom_components.pyscript.process_all_requirements", |
461 | 455 | return_value={
|
462 |
| - "/some/config/dir/pyscript/requirements.txt": [ |
463 |
| - "pytube==9.7.0\n", |
464 |
| - "# another test comment\n", |
465 |
| - "pykakasi==2.0.1 # test comment\n", |
466 |
| - "\n", |
467 |
| - ] |
| 456 | + "/some/config/dir/pyscript/requirements.txt": ["pytube==2.0.1", "pykakasi==2.0.1"] |
468 | 457 | },
|
469 | 458 | ):
|
470 | 459 | reload_param = {}
|
@@ -511,10 +500,52 @@ async def test_install_requirements(hass):
|
511 | 500 | ) as install_requirements:
|
512 | 501 | await setup_script(hass, None, dt(2020, 7, 1, 11, 59, 59, 999999), "")
|
513 | 502 | assert install_requirements.called
|
514 |
| - assert install_requirements.call_args[0][2] == ["pytube==9.7.0", "pykakasi==2.0.1"] |
| 503 | + assert install_requirements.call_args[0][2] == ["pytube==2.0.1", "pykakasi==2.0.1"] |
515 | 504 | install_requirements.reset_mock()
|
516 | 505 | # Because in tests, packages are not installed, we fake that they are
|
517 | 506 | # installed so we can test that we don't attempt to install them
|
518 |
| - with patch("custom_components.pyscript.installed_version", return_value="2.0.1"): |
| 507 | + with patch("custom_components.pyscript.installed_version", return_value="2.0.1"), patch( |
| 508 | + "custom_components.pyscript.process_all_requirements", |
| 509 | + return_value={"/some/config/dir/pyscript/requirements.txt": []}, |
| 510 | + ): |
519 | 511 | await hass.services.async_call("pyscript", "reload", {}, blocking=True)
|
520 | 512 | assert not install_requirements.called
|
| 513 | + |
| 514 | + |
| 515 | +async def test_process_requirements(hass, caplog): |
| 516 | + """Test process requirements function.""" |
| 517 | + requirements = [ |
| 518 | + "/some/config/dir/pyscript/requirements.txt", |
| 519 | + ] |
| 520 | + |
| 521 | + source = """ |
| 522 | +# Comment |
| 523 | +pytube==2.0.1 |
| 524 | +> |
| 525 | +pkakasi==2.0.1 # Comment |
| 526 | +
|
| 527 | +
|
| 528 | +""" |
| 529 | + |
| 530 | + with patch("custom_components.pyscript.glob.iglob", return_value=requirements), patch( |
| 531 | + "custom_components.pyscript.open", mock_open(read_data=source), create=True |
| 532 | + ): |
| 533 | + all_requirements = await hass.async_add_executor_job( |
| 534 | + process_all_requirements, hass, REQUIREMENTS_PATHS, REQUIREMENTS_FILE |
| 535 | + ) |
| 536 | + assert requirements[0] in all_requirements |
| 537 | + assert all_requirements[requirements[0]] == ["pytube==2.0.1", "pkakasi==2.0.1"] |
| 538 | + |
| 539 | + with patch("custom_components.pyscript.installed_version") as installed_version: |
| 540 | + installed_version.side_effect = ["2.0.1", "1.0.0"] |
| 541 | + all_requirements = await hass.async_add_executor_job( |
| 542 | + process_all_requirements, hass, REQUIREMENTS_PATHS, REQUIREMENTS_FILE |
| 543 | + ) |
| 544 | + assert requirements[0] in all_requirements |
| 545 | + assert all_requirements[requirements[0]] == [] |
| 546 | + |
| 547 | + assert caplog.record_tuples[0] == ( |
| 548 | + "custom_components.pyscript", |
| 549 | + 30, |
| 550 | + "`pkakasi` already found but found version `1.0.0` does not match requirement. Keeping found version.", |
| 551 | + ) |
0 commit comments