♻️refactor(project): adapt to new naming of parsing methods

adapt to new naming of parsing methods, remove unused paths in parser module, all module import logwatcher.config instead of importing logwatcher, rename test functions
This commit is contained in:
2026-09-10 10:13:12 +02:00
parent cf83a06cfe
commit c5466a4171
10 changed files with 59 additions and 71 deletions
+26 -24
View File
@@ -6,18 +6,19 @@ import pytest
from logwatcher.classifier import N2_PATTERNS, classify_log_entries
from logwatcher.config import FIXTURE_PATH
from logwatcher.parser import parse_log_file
from logwatcher.parser import parse_file
def _pattern_name_to_filename(name: str) -> str:
"""
Returns the fixture corresponding to the error code name
Returns the fixture corresponding to the error code name
"""
return f"CR_{name.lower()}.txt"
def _read_fixture(path: Path) -> str:
"""
Read a file with fallback Windows-1252.
Read a file with fallback Windows-1252.
"""
try:
content = path.read_text(encoding="utf-8")
@@ -27,10 +28,11 @@ def _read_fixture(path: Path) -> str:
return content
return path.read_text(encoding="windows-1252") # TODO: improve coverage score
@pytest.mark.parametrize("name, pattern", N2_PATTERNS.items())
def test_pattern_matches_fixture(valid_log_dir, name, pattern):
"""
Assert each N2 pattern must match at least one line in its fixture.
Assert each N2 pattern must match at least one line in its fixture.
"""
fixture = valid_log_dir / _pattern_name_to_filename(name)
@@ -39,10 +41,7 @@ def test_pattern_matches_fixture(valid_log_dir, name, pattern):
warnings.warn(msg)
pytest.skip(msg)
lignes = [
l for l in _read_fixture(fixture).splitlines()
if l.strip()
]
lignes = [l for l in _read_fixture(fixture).splitlines() if l.strip()]
assert lignes, f"empty fixture file : {fixture}"
@@ -54,21 +53,23 @@ def test_pattern_matches_fixture(valid_log_dir, name, pattern):
match_count += 1
assert match_count > 0, (
f"{name} : no match in file {fixture.name}\n"
f"pattern: {pattern}"
f"{name} : no match in file {fixture.name}\npattern: {pattern}"
)
def test_patterns_do_not_overlap():
"""
Assert that a log line can match only one N2 pattern.
Assert that a log line can match only one N2 pattern.
"""
fixtures_dir = FIXTURE_PATH
for fixture in fixtures_dir.glob("CR_*.txt"):
line_list = [line for line in _read_fixture(fixture).splitlines() if line.strip()] # TODO: improve coverage score
line_list = [
line for line in _read_fixture(fixture).splitlines() if line.strip()
] # TODO: improve coverage score
for line in line_list: # TODO: improve coverage score
matching = [ # TODO: improve coverage score
name for name, raw in N2_PATTERNS.items()
name
for name, raw in N2_PATTERNS.items()
if re.compile(raw).search(line)
]
assert len(matching) <= 1, ( # TODO: improve coverage score
@@ -76,10 +77,11 @@ def test_patterns_do_not_overlap():
f"{matching} match line : {line[:80]}"
)
def test_no_orphan_fixtures():
"""
Assert that a fixture exist only if its associated pattern exists.
Aucun fichier de fixture ne doit exister sans pattern associé.
Assert that a fixture exist only if its associated pattern exists.
Aucun fichier de fixture ne doit exister sans pattern associé.
"""
expected = {f"CR_{name.lower()}.txt" for name in N2_PATTERNS}
actual = {f.name for f in FIXTURE_PATH.glob("CR_*.txt")}
@@ -89,10 +91,10 @@ def test_no_orphan_fixtures():
def test_classify_log_entries_all_relevant(valid_log_dir: Path):
"""
Must return an empty irrelevant log entry list
Must return an empty irrelevant log entry list
"""
log_file = valid_log_dir / "only_relevant_logs.txt"
log_entries = parse_log_file(log_file)
log_entries = parse_file(log_file)
# all entries are relevant
relevant, irrelevant = classify_log_entries(log_entries)
@@ -102,10 +104,10 @@ def test_classify_log_entries_all_relevant(valid_log_dir: Path):
def test_classify_log_entries_none_relevant(valid_log_dir: Path):
"""
Must return an empty relevant log entry list
Must return an empty relevant log entry list
"""
log_file = valid_log_dir / "no_relevant_logs.txt"
log_entries = parse_log_file(log_file)
log_entries = parse_file(log_file)
# all entries are relevant
relevant, irrelevant = classify_log_entries(log_entries)
@@ -115,10 +117,10 @@ def test_classify_log_entries_none_relevant(valid_log_dir: Path):
def test_classify_log_entries_mixed(valid_log_dir: Path):
"""
Test classification on relevant and irrelevant log entry list
Test classification on relevant and irrelevant log entry list
"""
log_file = valid_log_dir / "mixed_logs.txt"
log_entries = parse_log_file(log_file)
log_entries = parse_file(log_file)
relevant, irrelevant = classify_log_entries(log_entries)
@@ -128,10 +130,10 @@ def test_classify_log_entries_mixed(valid_log_dir: Path):
def test_classify_log_entries_empty_log_entries(invalid_log_dir: Path):
"""
Test classification on an empty log entry list
Test classification on an empty log entry list
"""
empty_file = invalid_log_dir / "empty_file.txt"
log_entries = parse_log_file(empty_file)
log_entries = parse_file(empty_file)
relevant, irrelevant = classify_log_entries(log_entries)
+2 -1
View File
@@ -15,6 +15,7 @@ from logging import DEBUG, ERROR, INFO, WARNING, FileHandler, StreamHandler, get
import pytest
from logwatcher.config import DATETIME_FORMAT
from logwatcher.logging_config import _setup_formatter, _setup_handler, setup_logging
@@ -54,7 +55,7 @@ def test_type_of_handlers():
else:
pytest.fail("handlers must be either a file or a stream handler")
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
assert handler.formatter.datefmt == "%d/%m/%Y %H:%M:%S"
assert handler.formatter.datefmt == DATETIME_FORMAT
def test_modified_format():
+1 -1
View File
@@ -2,7 +2,7 @@ from datetime import datetime
import pytest
from logwatcher import DATETIME_FORMAT, FRENCH_TIMEZONE
from logwatcher.config import DATETIME_FORMAT, FRENCH_TIMEZONE
from logwatcher.models import LogEntry
+12 -4
View File
@@ -57,7 +57,7 @@ def test_parse_line(specific_logs: list[str]):
assert log_entry.raw_line != ""
def test_parse_empty_log_line():
def test_parse_empty_line():
"""
An empty or structurally invalid line returns None.
Covered cases:
@@ -71,7 +71,15 @@ def test_parse_empty_log_line():
assert not log_entry
def test_parse_valid_log_file(original_log_dir: Path):
def test_parse_lines():
pass
def test_parse_empty_lines():
pass
def test_parse_valid_file(original_log_dir: Path):
"""
Each real log file produces at least one valid LogEntry.
Integration test: files provided by technicians contain
@@ -86,7 +94,7 @@ def test_parse_valid_log_file(original_log_dir: Path):
assert isinstance(log_entry, LogEntry)
def test_parse_empty_log_file(empty_log_file):
def test_parse_empty_file(empty_log_file):
"""
An empty log file can be parsed and must returns an
empty LogEntry list.
@@ -95,7 +103,7 @@ def test_parse_empty_log_file(empty_log_file):
assert len(log_list) == 0
def test_parse_bad_log_file(bad_log_file):
def test_parse_bad_file(bad_log_file):
"""
A file containing only invalid lines produces nothing.
Any line that does not match the LAME MDC structure
+3 -3
View File
@@ -6,7 +6,7 @@ import pytest
from logwatcher.classifier import classify_log_entries
from logwatcher.config import DATETIME_FORMAT, FIXTURE_PATH, SourceType
from logwatcher.models import LogEntry
from logwatcher.parser import parse_log_file
from logwatcher.parser import parse_file
from logwatcher.reporter import (
N2_SUPPORT_TEMPLATE,
OTHER_TEMPLATE,
@@ -25,7 +25,7 @@ def _get_classified_log_entries(
log_file: Path,
) -> tuple[list[LogEntry], list[LogEntry]]:
"""Parse a log file and split its entries into relevant and irrelevant lists."""
log_entries = parse_log_file(log_file)
log_entries = parse_file(log_file)
return classify_log_entries(log_entries)
@@ -40,7 +40,7 @@ def get_all_log_entries_fixture() -> tuple[list[LogEntry], list[LogEntry]]:
"""Parse and classify every CR_* fixture, returning (relevant, irrelevant)."""
all_entries = []
for log_file in CR_LOG_FILES:
all_entries.extend(parse_log_file(log_file))
all_entries.extend(parse_file(log_file))
relevant_list, irrelevant_list = classify_log_entries(all_entries)
return relevant_list, irrelevant_list