test(cli): ✅ add tests for from_mails command

close task #85
This commit is contained in:
2026-09-17 13:01:38 +02:00
parent 5e43a0cbb8
commit 22e54da876
+83
View File
@@ -2,7 +2,10 @@ import re
from pathlib import Path from pathlib import Path
import pytest import pytest
from exchangelib import Message
from typer.testing import CliRunner from typer.testing import CliRunner
from unittest.mock import MagicMock
from logwatcher import __version__ from logwatcher import __version__
from logwatcher.cli import _get_files_from_dir, _is_log_file, app from logwatcher.cli import _get_files_from_dir, _is_log_file, app
@@ -398,3 +401,83 @@ def test_cli_from_files_valid_logs(output_path: Path, valid_log_dir: Path):
assert re.search(f"Période\t : {date_pattern} -> {date_pattern}\n", content) assert re.search(f"Période\t : {date_pattern} -> {date_pattern}\n", content)
assert f"Fichier(s) lu(s)\t : {nb_files}" in content assert f"Fichier(s) lu(s)\t : {nb_files}" in content
assert "Nombre total d'erreur(s)\t: 0" not in content assert "Nombre total d'erreur(s)\t: 0" not in content
def test_cli_from_mails_no_output_dir(monkeypatch, make_mock_account):
"""Assert everything works when no output_path is provided.
Must use "output/" directory, located in project root.
"""
messages = [
Message(subject="subject test 1", text_body="body test 1"),
Message(subject="subject test 2", text_body="body test 2"),
Message(subject="subject test 3", text_body="body test 3")
]
with monkeypatch.context() as m:
m.setattr("logwatcher.cli.connect_to_mailbox", lambda: make_mock_account(messages=messages, analyzed_folder="Analyzed"))
m.setattr("logwatcher.notifier.Message.send", MagicMock(return_value=None))
arguments = ["from-mails"]
results = runner.invoke(app, arguments)
assert results.exit_code == 0
def test_cli_from_mails_invalid_output_dir(tmp_path, monkeypatch):
"""Assert that if output_dir is a file, raise an error.
Args:
tmp_path: path where to store temporarily output files
monkeypatch: used to fake a context and method calls
make_mock_account: initialize a fake account object
"""
bad_output_dir = tmp_path.touch("test_file.txt")
messages = [
Message(subject="subject test 1", text_body="body test 1"),
]
with monkeypatch.context() as m:
m.setattr("logwatcher.cli.connect_to_mailbox", lambda: make_mock_account(messages=messages, analyzed_folder="Analyzed"))
m.setattr("logwatcher.notifier.Message.send", MagicMock(return_value=None))
arguments = ["from-mails", "--output-dir", bad_output_dir]
results = runner.invoke(app, arguments)
assert results.exit_code != 0
def test_cli_from_mails_valid_output_dir(tmp_path, monkeypatch, make_mock_account):
"""Assert everything works when a valid output_path is provided.
Args:
tmp_path: path where to store temporarily output files
monkeypatch: used to fake a context and method calls
make_mock_account: initialize a fake account object
"""
output_path = tmp_path / "output_test"
messages = [
Message(subject="subject test 1", text_body="body test 1"),
Message(subject="subject test 2", text_body="body test 2"),
Message(subject="subject test 3", text_body="body test 3")
]
with monkeypatch.context() as m:
m.setattr("logwatcher.cli.connect_to_mailbox", lambda: make_mock_account(messages=messages, analyzed_folder="Analyzed"))
m.setattr("logwatcher.notifier.Message.send", MagicMock(return_value=None))
arguments = ["from-mails", "--output-dir", output_path]
results = runner.invoke(app, arguments)
assert results.exit_code == 0
output_children = list(output_path.iterdir())
expected = ["n2.log", "all.log", "other.log"]
file_names = [file.name for file in output_children]
assert len(output_children) == len(expected)
for name in expected:
assert name in file_names