fix: ensure DATA_DIR and notifications dir exist before bot.py import in CI

This commit is contained in:
ZdenekSrotyr 2026-04-13 13:26:18 +02:00
parent 1a68decd4e
commit 0045f5d324
2 changed files with 10 additions and 14 deletions

View file

@ -12,6 +12,16 @@ import pytest
os.environ.setdefault("TESTING", "1") os.environ.setdefault("TESTING", "1")
os.environ.setdefault("JWT_SECRET_KEY", "test-secret-e2e") os.environ.setdefault("JWT_SECRET_KEY", "test-secret-e2e")
# Ensure directories exist for modules with module-level FileHandlers.
# bot.py creates FileHandler(config.BOT_LOG_FILE) at import time.
# config.py reads DATA_DIR at import time. We must ensure the directory
# exists for whatever DATA_DIR resolves to (default: /data in Docker).
import tempfile as _tf
if "DATA_DIR" not in os.environ:
os.environ["DATA_DIR"] = os.path.join(_tf.gettempdir(), ".agnes-test-data")
os.makedirs(os.path.join(os.environ["DATA_DIR"], "notifications"), exist_ok=True)
os.makedirs(os.path.join(os.environ["DATA_DIR"], "state"), exist_ok=True)
@pytest.fixture @pytest.fixture
def e2e_env(tmp_path, monkeypatch): def e2e_env(tmp_path, monkeypatch):

View file

@ -1,24 +1,10 @@
"""Tests for Telegram bot message handlers.""" """Tests for Telegram bot message handlers."""
import asyncio import asyncio
import os
import sys
import tempfile
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import pytest import pytest
# Patch BOT_LOG_FILE at import time (before any fixture runs) so that
# bot.py can be imported even when /data/notifications/ doesn't exist.
# This is critical for pytest-xdist where module-scoped fixtures
# may not run before the worker tries to import the module.
_tmp_log = tempfile.mktemp(suffix=".log")
import services.telegram_bot.config as _cfg
_cfg.BOT_LOG_FILE = _tmp_log
# Ensure the bot module is not cached with the old config
sys.modules.pop("services.telegram_bot.bot", None)
def _make_message(text: str, chat_id: int = 10) -> dict: def _make_message(text: str, chat_id: int = 10) -> dict:
return {"chat": {"id": chat_id}, "text": text} return {"chat": {"id": chat_id}, "text": text}