test updates

This commit is contained in:
John Lancaster
2024-09-04 23:50:57 -05:00
parent b673bda1f2
commit 7a8e6f383a
6 changed files with 89 additions and 29 deletions

View File

@@ -1,7 +1,7 @@
import asyncio
import re
from pathlib import Path
from typing import List
from typing import Generator, List, Literal
import pytest
from appdaemon.appdaemon import AppDaemon
@@ -13,23 +13,47 @@ async def delayed_stop(ad: AppDaemon, delay: float):
ad.stop()
def get_load_order(caplog: pytest.LogCaptureFixture) -> List[str]:
for record in caplog.records:
if record.name == 'AppDaemon._app_management':
if 'Determined module load order' in record.msg:
return record.args[0]
def get_load_order(
caplog: pytest.LogCaptureFixture, ordered: bool = True
) -> Generator[str, None, None]:
records = (
record.args[0]
for record in get_logger_records(caplog, 'AppDaemon._app_management')
if 'Determined module load order' in record.msg
)
try:
result = list(records)[-1]
if not ordered:
result = set(result)
return result
except Exception:
return
def get_logger_records(caplog: pytest.LogCaptureFixture, name: str):
def get_logger_records(caplog: pytest.LogCaptureFixture, logger_name: str):
for record in caplog.records:
if record.name == name:
if record.name == logger_name:
yield record
def get_app_orders(caplog: pytest.LogCaptureFixture, phase: str) -> List[str]:
for record in get_logger_records(caplog, 'AppDaemon._app_management'):
if re.search(f'App {phase} order', record.msg):
return record.args[0]
def get_app_orders(
caplog: pytest.LogCaptureFixture,
phase: Literal['start', 'stop'],
ordered: bool = True,
) -> list[str]:
"""Extracts the app start/stop order from the captured log lines"""
records = (
record.args[0]
for record in get_logger_records(caplog, 'AppDaemon._app_management')
if re.search(f'App {phase} order', record.msg)
)
try:
result = list(records)[-1]
if not ordered:
result = set(result)
return result
except Exception:
return
def get_loaded_apps(caplog: pytest.LogCaptureFixture):
@@ -39,7 +63,9 @@ def get_loaded_apps(caplog: pytest.LogCaptureFixture):
def count_error_lines(caplog: pytest.LogCaptureFixture) -> int:
error_log_lines = [msg for name, lvl, msg in caplog.record_tuples if name.startswith('Error')]
error_log_lines = [
msg for name, lvl, msg in caplog.record_tuples if name.startswith('Error')
]
return len(error_log_lines)
@@ -47,3 +73,11 @@ def reset_file(repo: Repo, changed: Path):
if not changed.is_absolute():
changed = Path(repo.working_tree_dir) / changed
repo.git.checkout('HEAD', '--', changed)
def inject_into_file(file: Path, pos: int, line: str):
content = file.read_text()
lines = content.splitlines()
lines.insert(pos, line)
new_content = '\n'.join(lines)
file.write_text(new_content)