Files
ad-test/src/ad_test/test_file_change.py
2024-08-12 20:46:13 -05:00

48 lines
1.3 KiB
Python

import asyncio
import logging
import re
from pathlib import Path
import food.meal
import pytest
from appdaemon.appdaemon import AppDaemon
from git import Repo
from .utils import get_load_order
def reset_file(changed: Path):
for p in Path(__file__).parents:
if p.with_name('.git').exists():
root = p.parent
break
repo = Repo(root)
if not changed.is_absolute():
changed = root / changed
repo.git.checkout('HEAD', '--', changed)
def modify_file(path: Path):
file_content = path.read_text()
modified_content = re.sub(r'Ham', r'Spam', file_content, flags=re.MULTILINE)
modified_content = re.sub(r'ham', r'spam', modified_content, flags=re.MULTILINE)
path.write_text(modified_content)
def test_file(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo):
assert isinstance(ad, AppDaemon)
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))
module_load_order = get_load_order(caplog)
assert module_load_order == ['food.meal', 'restaurant']
reset_file(module_file)
ad.loop.run_until_complete(asyncio.sleep(1.0))