51adcfd8ad
remove the test_unicode.txt used for unicode reading tests. Todo reminder is for coverage score to improve in the future
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
import re
|
|
import warnings
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from logwatcher.classifier import N2_PATTERNS
|
|
from logwatcher.config import FIXTURE_PATH
|
|
|
|
ORIGINAL_LOGS_DIR = FIXTURE_PATH / "original_logs"
|
|
|
|
|
|
def _pattern_name_to_filename(name: str) -> str:
|
|
"""
|
|
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.
|
|
"""
|
|
try:
|
|
contenu = path.read_text(encoding="utf-8")
|
|
except UnicodeDecodeError:
|
|
return path.read_text(encoding="windows-1252")
|
|
if "\ufffd" not in contenu:
|
|
return contenu
|
|
return path.read_text(encoding="windows-1252") # TODO: improve coverage score
|
|
|
|
@pytest.mark.parametrize("name, pattern", N2_PATTERNS.items())
|
|
def test_pattern_matches_fixture(name, pattern):
|
|
"""
|
|
Assert each N2 pattern must match at least one line in its fixture.
|
|
"""
|
|
|
|
fixture = ORIGINAL_LOGS_DIR / _pattern_name_to_filename(name)
|
|
if not fixture.exists():
|
|
msg = f"No fixture named '{fixture}'"
|
|
warnings.warn(msg)
|
|
pytest.skip(msg)
|
|
|
|
lignes = [
|
|
l for l in _read_fixture(fixture).splitlines()
|
|
if l.strip()
|
|
]
|
|
|
|
assert lignes, f"empty fixture file : {fixture}"
|
|
|
|
match_count = 0
|
|
|
|
for ligne in lignes:
|
|
match = re.search(pattern, ligne)
|
|
if match:
|
|
match_count += 1
|
|
|
|
assert match_count > 0, (
|
|
f"{name} : no match in file {fixture.name}\n"
|
|
f"pattern: {pattern}"
|
|
)
|
|
|
|
|
|
def test_patterns_do_not_overlap():
|
|
"""
|
|
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
|
|
for line in line_list: # TODO: improve coverage score
|
|
matching = [ # TODO: improve coverage score
|
|
name for name, raw in N2_PATTERNS.items()
|
|
if re.compile(raw).search(line)
|
|
]
|
|
assert len(matching) <= 1, ( # TODO: improve coverage score
|
|
f"Chevauchement détecté sur {fixture.name} : "
|
|
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é.
|
|
"""
|
|
expected = {f"CR_{name.lower()}.txt" for name in N2_PATTERNS}
|
|
actual = {f.name for f in FIXTURE_PATH.glob("CR_*.txt")}
|
|
orphan = actual - expected
|
|
assert not orphan, f"Fixtures orphelines (sans pattern) : {sorted(orphan)}" |