added try statement

This commit is contained in:
John Lancaster
2024-08-12 22:15:15 -05:00
parent 1358da1e60
commit e3873f5510
2 changed files with 12 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
import asyncio import asyncio
import logging
from pathlib import Path from pathlib import Path
from typing import List from typing import List
@@ -45,6 +46,7 @@ def ad(base_config: AppDaemonConfig):
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
ad = AppDaemon(Logging(), loop, base_config) ad = AppDaemon(Logging(), loop, base_config)
logging.getLogger('AppDaemon').propagate = True
yield ad yield ad
ad.stop() ad.stop()

View File

@@ -24,7 +24,7 @@ def modify_file(path: Path):
path.write_text(modified_content) path.write_text(modified_content)
def insert_importerror(path: Path): def insert_import_error(path: Path):
file_content = path.read_text().splitlines() file_content = path.read_text().splitlines()
file_content.insert(0, 'raise ImportError') file_content.insert(0, 'raise ImportError')
path.write_text('\n'.join(file_content)) path.write_text('\n'.join(file_content))
@@ -36,22 +36,24 @@ def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo
module_file = Path(food.meal.__file__) module_file = Path(food.meal.__file__)
modify_file(module_file) modify_file(module_file)
logging.getLogger('AppDaemon').propagate = True try:
with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'): with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'):
ad.loop.run_until_complete(asyncio.sleep(1.0)) ad.loop.run_until_complete(asyncio.sleep(1.0))
module_load_order = get_load_order(caplog) module_load_order = get_load_order(caplog)
assert module_load_order == ['food.meal', 'restaurant'] assert module_load_order == ['food.meal', 'restaurant']
reset_file(config_repo, module_file)
ad.loop.run_until_complete(asyncio.sleep(1.0))
finally:
reset_file(config_repo, module_file) 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): def test_file_with_error(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo):
assert isinstance(ad, AppDaemon) assert isinstance(ad, AppDaemon)
module_file = Path(food.meal.__file__) module_file = Path(food.meal.__file__)
insert_importerror(module_file) insert_import_error(module_file)
try: try:
with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'): with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'):