Files
maurane e50797bba6 test(cli): ✅ add tests for prune command
add tests for prune command and some code optimization and formatting.
2026-09-25 17:59:27 +02:00

98 lines
2.9 KiB
Python

# tests/conftest.py
import os
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from exchangelib import Account, Message
from logwatcher.config import FIXTURE_PATH, OUTPUT_PATH
from logwatcher.logging_config import setup_logging
# original log directory
ORIGINAL_LOG_DIR = FIXTURE_PATH / "original_logs"
# valid log directory
VALID_LOG_DIR = FIXTURE_PATH / "valid_logs"
# invalid_log_directory
INVALID_LOG_DIR = FIXTURE_PATH / "invalid_logs"
# define temp directory only for testing
os.environ["PYTEST_DEBUG_TEMPROOT"] = str(OUTPUT_PATH)
@pytest.fixture
def tmp_log_file(tmp_path):
"""Return path of logs logwatcher related."""
return tmp_path / "logwatcher_test.log"
@pytest.fixture(autouse=True)
def tmp_setup_logging(tmp_log_file):
"""Redirige les logs vers un dossier temporaire pendant les tests."""
setup_logging(file_path=tmp_log_file)
@pytest.fixture(autouse=True)
def tmp_cli_logging(monkeypatch, tmp_path):
"""Keep the CLI callback from writing run logs into the project `output/logs`.
The callback builds `LOGGING_PATH / <timestamp>.log` on every command call,
so `runner.invoke()` would otherwise create a file in the working tree and
add two handlers to the root logger for each test.
"""
monkeypatch.setattr("logwatcher.cli.LOGGING_PATH", tmp_path / "logs")
@pytest.fixture(name="original_log_dir")
def original_log_dir_fixture() -> Path:
"""Return log directory containing real logs.
All real log files provided by N2 technicians.
Used for integration tests: these files contain both valid lines
and lines to be ignored (system logs, empty lines, directory headers).
"""
return FIXTURE_PATH / "original_logs"
@pytest.fixture(name="valid_log_dir")
def valid_log_dir_fixture() -> Path:
"""Return log directory containing only valid logs.
All valid log files provided by N2 technicians.
Used for integration tests: these files contain both relevant and irrelevant logs
for N2 technicians.
"""
return FIXTURE_PATH / "valid_logs"
@pytest.fixture(name="invalid_log_dir")
def invalid_log_dir_fixture() -> Path:
"""Return log directory containing invalid logs.
All invalid log files created for testing only.
Used for integration tests of parsing.
"""
return FIXTURE_PATH / "invalid_logs"
@pytest.fixture(scope="package")
def make_mock_account():
"""Create a fake Account.
A fake Account with a msg_folder_root returning a fake Logs folder.
"""
def _make_mock_account(
messages: list[Message] | None = None, analyzed_folder: MagicMock | None = None
):
account = MagicMock(spec=Account)
account.fullname = "fake"
logs_folder = account.msg_folder_root.__truediv__.return_value
logs_folder.all.return_value = messages or []
logs_folder.glob.return_value = [analyzed_folder] if analyzed_folder else []
return account
return _make_mock_account