From 22e54da876d963db7da38c0150513013ec3108c2 Mon Sep 17 00:00:00 2001 From: maurane Date: Thu, 17 Sep 2026 13:01:38 +0200 Subject: [PATCH] =?UTF-8?q?test(cli):=20=E2=9C=85=20add=20tests=20for=20`f?= =?UTF-8?q?rom=5Fmails`=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close task #85 --- tests/test_cli.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 7d613ed..c09bc2e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,7 +2,10 @@ import re from pathlib import Path import pytest +from exchangelib import Message from typer.testing import CliRunner +from unittest.mock import MagicMock + from logwatcher import __version__ from logwatcher.cli import _get_files_from_dir, _is_log_file, app @@ -397,4 +400,84 @@ def test_cli_from_files_valid_logs(output_path: Path, valid_log_dir: Path): content = file.read_text(encoding="windows-1252") 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 "Nombre total d'erreur(s)\t: 0" not in content \ No newline at end of file + 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