From e3873f551091c1e5888fc129e19b685b57edae2d Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:15:15 -0500 Subject: [PATCH] added try statement --- src/ad_test/conftest.py | 2 ++ src/ad_test/test_file_change.py | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ad_test/conftest.py b/src/ad_test/conftest.py index cce7b67..3c6b444 100644 --- a/src/ad_test/conftest.py +++ b/src/ad_test/conftest.py @@ -1,4 +1,5 @@ import asyncio +import logging from pathlib import Path from typing import List @@ -45,6 +46,7 @@ def ad(base_config: AppDaemonConfig): loop = asyncio.new_event_loop() ad = AppDaemon(Logging(), loop, base_config) + logging.getLogger('AppDaemon').propagate = True yield ad ad.stop() diff --git a/src/ad_test/test_file_change.py b/src/ad_test/test_file_change.py index 293b8d8..90d6e43 100644 --- a/src/ad_test/test_file_change.py +++ b/src/ad_test/test_file_change.py @@ -24,7 +24,7 @@ def modify_file(path: Path): path.write_text(modified_content) -def insert_importerror(path: Path): +def insert_import_error(path: Path): file_content = path.read_text().splitlines() file_content.insert(0, 'raise ImportError') 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__) modify_file(module_file) - logging.getLogger('AppDaemon').propagate = True - with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'): - ad.loop.run_until_complete(asyncio.sleep(1.0)) + 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'] + 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)) + finally: 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) + insert_import_error(module_file) try: with caplog.at_level(logging.DEBUG, logger='AppDaemon._app_management'):