added basic failed import

This commit is contained in:
John Lancaster
2024-08-12 22:09:34 -05:00
parent 0fcc8ac652
commit 4e26d1b4ef

View File

@@ -1,5 +1,6 @@
import asyncio import asyncio
import logging import logging
import random
import re import re
from pathlib import Path from pathlib import Path
@@ -24,6 +25,13 @@ def modify_file(path: Path):
path.write_text(modified_content) 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): def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo):
assert isinstance(ad, AppDaemon) 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) reset_file(config_repo, module_file)
ad.loop.run_until_complete(asyncio.sleep(1.0)) 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)