68 lines
1.6 KiB
Python
68 lines
1.6 KiB
Python
import asyncio
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
import pytest
|
|
from appdaemon.appdaemon import AppDaemon
|
|
from appdaemon.logging import Logging
|
|
from appdaemon.models.config import AppDaemonConfig
|
|
from git import Repo
|
|
from pytest import Function
|
|
|
|
|
|
def pytest_collection_modifyitems(session, config, items: List[Function]):
|
|
for i, item in enumerate(items):
|
|
if isinstance(item, Function) and 'startup' in item.name:
|
|
items.insert(0, items.pop(i))
|
|
break
|
|
|
|
return items
|
|
|
|
|
|
CONFIG_DIR = Path(__file__).parents[2] / 'conf'
|
|
|
|
|
|
@pytest.fixture
|
|
def config_repo() -> Repo:
|
|
repo = Repo(Path(__file__).parents[2])
|
|
return repo
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def base_config() -> AppDaemonConfig:
|
|
return AppDaemonConfig(
|
|
latitude=0.0,
|
|
longitude=0.0,
|
|
elevation=0,
|
|
time_zone='America/Chicago',
|
|
config_dir=CONFIG_DIR,
|
|
config_file='appdaemon.yaml',
|
|
module_debug={'_app_management': 'DEBUG'}
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def ad(base_config: AppDaemonConfig):
|
|
loop = asyncio.new_event_loop()
|
|
|
|
ad = AppDaemon(Logging(), loop, base_config)
|
|
|
|
for cfg in ad.logging.config.values():
|
|
logger = logging.getLogger(cfg['name'])
|
|
logger.propagate = True
|
|
logger.setLevel('DEBUG')
|
|
|
|
yield ad
|
|
ad.stop()
|
|
|
|
tasks = asyncio.all_tasks(loop)
|
|
for t in tasks:
|
|
t.cancel()
|
|
|
|
with pytest.raises(asyncio.exceptions.CancelledError):
|
|
loop.run_until_complete(asyncio.gather(*tasks))
|
|
|
|
loop.close()
|
|
print('Cleaned up running AD')
|