diff --git a/src/ad_test/test_file_change.py b/src/ad_test/test_file_change.py index b7139d5..7dde753 100644 --- a/src/ad_test/test_file_change.py +++ b/src/ad_test/test_file_change.py @@ -1,5 +1,6 @@ import asyncio import logging +import random import re from pathlib import Path @@ -24,6 +25,13 @@ def modify_file(path: Path): path.write_text(modified_content) +def insert_importerror(path: Path): + file_content = path.read_text().splitlines() + # i = random.randint(0, len(file_content)) + file_content.insert(0, 'raise ImportError') + path.write_text('\n'.join(file_content)) + + def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo): assert isinstance(ad, AppDaemon) @@ -39,3 +47,23 @@ def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo reset_file(config_repo, module_file) ad.loop.run_until_complete(asyncio.sleep(1.0)) + + +def test_file_with_error(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo): + assert isinstance(ad, AppDaemon) + + module_file = Path(food.meal.__file__) + insert_importerror(module_file) + + try: + with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'): + ad.loop.run_until_complete(asyncio.sleep(1.0)) + + module_load_order = get_load_order(caplog) + assert module_load_order == ['food.meal', 'restaurant'] + + reset_file(config_repo, module_file) + ad.loop.run_until_complete(asyncio.sleep(1.0)) + assert "Started 'my_restaurant'" in caplog.text + finally: + reset_file(config_repo, module_file)