feat(F071,F068,F070): Welle 2 — Refactor-Audit, Stabilitaets-Monitor, Contract-Test-Framework @implements: [F-071] [F-068] [F-070]
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
"""F071 — Refactor-Disziplin: Audit-Script-Tests.
|
||||
|
||||
Testet scripts/audit_refactor.py:
|
||||
1. Script existiert & ist ausführbar
|
||||
2. @implements: [F-071] Marker gesetzt
|
||||
3. Audit findet ein synthetisches Duplikat (2 gleiche Funktionen)
|
||||
4. Audit findet wiederkehrende Naming-Verletzung
|
||||
5. Audit findet unreferenzierte Funktion (toter Pfad)
|
||||
6. Exit-Codes: 0 = sauber, 1 = Duplikat, 2 = Naming/Dead-Verdacht
|
||||
7. --json Ausgabe liefert gültiges JSON
|
||||
|
||||
@implements: [F-071]
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
_REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
SCRIPT = _REPO_ROOT / "scripts" / "audit_refactor.py"
|
||||
|
||||
|
||||
def _run_script(args, cwd=_REPO_ROOT):
|
||||
return subprocess.run(
|
||||
[sys.executable, str(SCRIPT), *args],
|
||||
capture_output=True, text=True, cwd=cwd,
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# 1. Script existiert & ausführbar
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
def test_script_exists():
|
||||
assert SCRIPT.exists(), "scripts/audit_refactor.py fehlt"
|
||||
|
||||
|
||||
def test_script_runs_on_repo():
|
||||
# Nur sinnvoll, wenn das echtes Repo (plugin/archivar) vorliegt.
|
||||
if not (_REPO_ROOT / "plugin" / "archivar").is_dir():
|
||||
pytest.skip("Repo-Verzeichnis plugin/archivar nicht vorhanden (Nicht-Repo-Kontext)")
|
||||
r = _run_script(["--dir", "plugin/archivar"])
|
||||
assert r.returncode in (0, 1, 2), \
|
||||
f"Audit muss auf Repo laufen, rc={r.returncode}: {r.stderr}"
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# 2. @implements-Marker
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
def test_implements_marker():
|
||||
content = SCRIPT.read_text()
|
||||
assert "@implements: [F-071]" in content, \
|
||||
"audit_refactor.py fehlt @implements: [F-071] (Bindestrich Pflicht)"
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# 3. Duplikat-Erkennung
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
DUPLICATE_FIXTURE = '''\
|
||||
def compute_totals_a(items):
|
||||
totals = []
|
||||
running = 0
|
||||
for it in items:
|
||||
running = running + it
|
||||
if running > 100:
|
||||
totals.append(running)
|
||||
else:
|
||||
totals.append(0)
|
||||
return totals
|
||||
|
||||
|
||||
def compute_totals_b(values):
|
||||
result = []
|
||||
accum = 0
|
||||
for v in values:
|
||||
accum = accum + v
|
||||
if accum > 100:
|
||||
result.append(accum)
|
||||
else:
|
||||
result.append(0)
|
||||
return result
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dup_dir():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
base = Path(tmp) / "pkg"
|
||||
base.mkdir()
|
||||
(base / "dup.py").write_text(DUPLICATE_FIXTURE, encoding="utf-8")
|
||||
yield str(base)
|
||||
|
||||
|
||||
def test_detects_duplicate(dup_dir):
|
||||
r = _run_script(["--dir", dup_dir, "--json"])
|
||||
assert r.returncode == 0, f"JSON-Modus muss 0 liefern, stderr={r.stderr}"
|
||||
data = json.loads(r.stdout)
|
||||
assert len(data["duplicates"]) >= 1, \
|
||||
f"Audit muss das Duplikat finden: {data['duplicates']}"
|
||||
names = {o["name"] for d in data["duplicates"] for o in d["occurrences"]}
|
||||
assert "compute_totals_a" in names and "compute_totals_b" in names
|
||||
|
||||
|
||||
def test_duplicate_exit_code(dup_dir):
|
||||
r = _run_script(["--dir", dup_dir]) # human-Modus
|
||||
assert r.returncode == 1, "Duplikat muss Exit-Code 1 liefern"
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# 4. Naming-Verletzung (wiederkehrend)
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
NAMING_FIXTURE = {
|
||||
"a.py": (
|
||||
"def BadNameOne(x):\n"
|
||||
" return x + 1\n\n\n"
|
||||
"def BadNameTwo(y):\n"
|
||||
" return y * 2\n"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def test_detects_naming_violation():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
base = Path(tmp) / "pkg"
|
||||
base.mkdir()
|
||||
for name, content in NAMING_FIXTURE.items():
|
||||
(base / name).write_text(content, encoding="utf-8")
|
||||
r = _run_script(["--dir", str(base), "--json"])
|
||||
data = json.loads(r.stdout)
|
||||
fn_rows = [(row[0], row[1]) for row in data["naming_findings"]]
|
||||
assert any(n == "BadNameOne" and k == "Funktion" for n, k in fn_rows), \
|
||||
f"naming_findings fehlt BadNameOne: {data['naming_findings']}"
|
||||
assert any(n == "BadNameTwo" and k == "Funktion" for n, k in fn_rows)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# 5. Toter Pfad (unreferenzierte Funktion)
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
DEAD_FIXTURE = {
|
||||
"a.py": (
|
||||
"def used_function():\n"
|
||||
" return 'x'\n\n\n"
|
||||
"def dead_function():\n"
|
||||
" return 'unused'\n\n\n"
|
||||
"_caller = used_function()\n"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def test_detects_dead_code():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
base = Path(tmp) / "pkg"
|
||||
base.mkdir()
|
||||
for name, content in DEAD_FIXTURE.items():
|
||||
(base / name).write_text(content, encoding="utf-8")
|
||||
r = _run_script(["--dir", str(base), "--json"])
|
||||
data = json.loads(r.stdout)
|
||||
dead_names = [n for n, _f in data["dead_code_suspects"]]
|
||||
assert "dead_function" in dead_names
|
||||
assert "used_function" not in dead_names
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# 6. Sauberes Paket → Exit 0
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
CLEAN_FIXTURE = {
|
||||
"x.py": (
|
||||
"def used():\n"
|
||||
" return 1\n\n\n"
|
||||
"global_out = used()\n"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def test_clean_package_exit_zero():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
base = Path(tmp) / "pkg"
|
||||
base.mkdir()
|
||||
for name, content in CLEAN_FIXTURE.items():
|
||||
(base / name).write_text(content, encoding="utf-8")
|
||||
r = _run_script(["--dir", str(base)])
|
||||
assert r.returncode == 0, \
|
||||
f"sauberes Paket soll 0 liefern: {r.stdout} {r.stderr}"
|
||||
Reference in New Issue
Block a user