Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

18.8 KB
Raw Blame
"""Unit tests for the error-handling anti-pattern detection.

The 24 fixtures below are ported verbatim from the bench-validated
detector (11 languages, precision-first). Each asserts the per-kind hit
counts the walker's whole-tree pass must produce. Like the other walker
tests, language-pack availability is best-effort: a missing tree-sitter
grammar skips rather than fails.
"""

from __future__ import annotations

import pytest

from repowise.core.analysis.health.biomarkers.base import FileContext
from repowise.core.analysis.health.biomarkers.error_handling import ErrorHandlingDetector
from repowise.core.analysis.health.complexity import ErrorHandlingHit, walk_file
from repowise.core.analysis.health.models import Severity

# (language, source, expected per-kind counts, note)
_FIXTURES = [
    (
        "python",
        b"try:\n    x()\nexcept Exception:\n    pass\n",
        {"swallowed_catch": 1, "broad_except": 1},
        "empty Exception catch -> swallowed + broad (cannot catch KeyboardInterrupt)",
    ),
    (
        "python",
        b"try:\n    x()\nexcept ValueError:\n    pass\n",
        {"swallowed_catch": 1},
        "empty specific catch -> swallowed, not bare",
    ),
    (
        "python",
        b"try:\n    x()\nexcept:\n    ...\n",
        {"swallowed_catch": 1, "bare_except": 1},
        "bare except + ellipsis body",
    ),
    (
        "python",
        b"try:\n    x()\nexcept ValueError as e:\n    logger.error(e)\n    raise\n",
        {},
        "handled + re-raised -> clean",
    ),
    (
        "python",
        b"try:\n    x()\nexcept ValueError:\n    return None\n",
        {},
        "specific catch with real handling -> clean",
    ),
    ("python", b"def f():\n    return 1\n", {}, "no try -> clean"),
    ("javascript", b"try { go(); } catch (e) {}\n", {"swallowed_catch": 1}, "empty JS catch"),
    (
        "javascript",
        b"try { go(); } catch (e) { console.error(e); }\n",
        {},
        "handled JS catch",
    ),
    (
        "typescript",
        b"try { go(); } catch (e) { /* ignore */ }\n",
        {"swallowed_catch": 1},
        "comment-only TS catch",
    ),
    (
        "java",
        b"class A { void m(){ try { go(); } catch (Exception e) {} } }\n",
        {"swallowed_catch": 1},
        "empty Java catch",
    ),
    (
        "java",
        b"class A { void m(){ try { go(); fail(\"x\"); } catch (Exception e) {} } }\n",
        {},
        "Java try ending in fail() -> expected exception idiom, not swallowed",
    ),
    (
        "java",
        b"class A { void m(){ try { go(); Assert.fail(\"x\"); } catch (Exception e) { // expected\n } } }\n",
        {},
        "Java try ending in Assert.fail() -> clean",
    ),
    (
        "java",
        b"class A { void m(){ try (var r = open()) { go(); Assertions.fail(\"x\"); } catch (Exception e) {} } }\n",
        {},
        "Java try-with-resources ending in Assertions.fail() -> clean",
    ),
    (
        "java",
        b"class A { void m(){ try { fail(\"x\"); go(); } catch (Exception e) {} } }\n",
        {"swallowed_catch": 1},
        "Java fail() not last in try -> swallowed",
    ),
    (
        "java",
        b"class A { void m(){ try { go(); } catch (Exception e) { log(e); } } }\n",
        {},
        "handled Java catch",
    ),
    (
        "apex",
        b"class A { void m(){ try { go(); } catch (Exception e) { // ignore\n } } }\n",
        {"swallowed_catch": 1},
        "comment-only Apex catch",
    ),
    (
        "apex",
        b"class A { void m(){ try { go(); System.assert(false, 'x'); } catch (Exception e) {} } }\n",
        {},
        "Apex try ending in System.assert(false) -> expected exception idiom",
    ),
    (
        "apex",
        b"class A { void m(){ try { go(); Assert.fail(); } catch (Exception e) {} } }\n",
        {},
        "Apex try ending in Assert.fail() -> clean",
    ),
    (
        "apex",
        b"class A { void m(){ try { go(); System.assert(ok); } catch (Exception e) {} } }\n",
        {"swallowed_catch": 1},
        "Apex System.assert on a value is no expected-throw idiom",
    ),
    (
        "kotlin",
        b"fun f(){ try { go() } catch (e: Exception) {} }\n",
        {"swallowed_catch": 1},
        "empty Kotlin catch",
    ),
    (
        "kotlin",
        b"fun f(){ try { go() } catch (e: Exception) { log(e) } }\n",
        {},
        "handled Kotlin catch",
    ),
    (
        "csharp",
        b"class A{ void M(){ try { Go(); } catch (Exception e) {} } }\n",
        {"swallowed_catch": 1},
        "empty C# catch",
    ),
    (
        "csharp",
        b"class A{ void M(){ try { Go(); } catch (Exception e) { Log(e); } } }\n",
        {},
        "handled C# catch",
    ),
    (
        "cpp",
        b"void f(){ try { go(); } catch (std::exception& e) {} }\n",
        {"swallowed_catch": 1},
        "empty C++ catch",
    ),
    (
        "cpp",
        b"void f(){ try { go(); } catch (std::exception& e) { log(e); } }\n",
        {},
        "handled C++ catch",
    ),
    ("rust", b"fn f() { let x = g().unwrap(); }\n", {"unsafe_unwrap": 1}, "unwrap"),
    (
        "rust",
        b'fn f() { let x = g().expect("boom"); panic!("x"); }\n',
        {"unsafe_unwrap": 1, "panic_macro": 1},
        "expect -> unsafe_unwrap, panic! -> panic_macro",
    ),
    (
        "rust",
        b"fn f() -> Result<i32,E> { let x = g()?; Ok(x) }\n",
        {},
        "? operator -> clean",
    ),
    (
        "go",
        b"func f() { v, _ := g(); _ = v }\n",
        {"go_swallow": 1},
        "blank discard of call return",
    ),
    ("go", b"func f() { if err != nil {} }\n", {"go_swallow": 1}, "empty if-err block"),
    ("go", b"func f() { if err != nil { return err } }\n", {}, "handled if-err"),
    (
        "go",
        b"func f() { x := g(); _ = x }\n",
        {},
        "single assign, no multi-return discard",
    ),
]


def _grammar_available(language: str) -> bool:
    try:
        from repowise.core.ingestion.parser import _get_language
    except Exception:
        return False
    try:
        return _get_language(language) is not None
    except Exception:
        return False


def _hits(language: str, source: bytes) -> list[ErrorHandlingHit]:
    if not _grammar_available(language):
        pytest.skip(f"tree-sitter language pack missing for {language}")
    return walk_file("<fixture>", language, source).error_handling_hits


@pytest.mark.parametrize(
    ("language", "source", "expected", "note"),
    _FIXTURES,
    ids=[f"{lang}-{note}" for lang, _, _, note in _FIXTURES],
)
def test_detector_fixture(language: str, source: bytes, expected: dict, note: str):
    hits = _hits(language, source)
    by_kind: dict[str, int] = {}
    for h in hits:
        by_kind[h.kind] = by_kind.get(h.kind, 0) + 1
    assert by_kind == expected, f"{note}: expected {expected}, got {by_kind}"


def test_hits_carry_line_anchors():
    hits = _hits("python", b"x = 1\ntry:\n    x()\nexcept Exception:\n    pass\n")
    assert hits, "expected hits on the swallowed bare-Exception catch"
    assert all(h.line == 4 for h in hits)


def test_biomarker_emits_one_low_finding_per_hit():
    ctx = FileContext(
        file_path="a.py",
        language="python",
        nloc=20,
        has_test_file=False,
        module=None,
        error_handling_hits=[
            ErrorHandlingHit("swallowed_catch", 4),
            ErrorHandlingHit("bare_except", 4),
        ],
    )
    findings = ErrorHandlingDetector().detect(ctx)
    assert len(findings) == 2
    assert all(f.biomarker_type == "error_handling" for f in findings)
    assert all(f.severity == Severity.LOW for f in findings)
    assert findings[0].line_start == findings[0].line_end == 4
    assert {f.details["kind"] for f in findings} == {"swallowed_catch", "bare_except"}


def test_biomarker_no_hits_no_findings():
    ctx = FileContext(
        file_path="a.py",
        language="python",
        nloc=20,
        has_test_file=False,
        module=None,
    )
    assert ErrorHandlingDetector().detect(ctx) == []


# Rust unwraps that provably cannot panic are no finding; the controls beside
# each stay findings.
_RUST_CANNOT_PANIC = [
    (b"fn f(x: Option<u8>) { if x.is_some() { g(x.unwrap()); } }\n", 0, "is_some guard"),
    (b"fn f(x: Option<u8>) { if x.is_some() && h() { g(x.unwrap()); } }\n", 0, "guard conjunct"),
    (b"fn f(x: Option<u8>) { if x.is_some() || h() { g(x.unwrap()); } }\n", 1, "|| is no guard"),
    (b"fn f(x: Option<u8>) { if !x.is_some() { g(x.unwrap()); } }\n", 1, "negated guard"),
    (b"fn f(x: Option<u8>) { if x.is_some() { } else { g(x.unwrap()); } }\n", 1, "else branch"),
    (b"fn f(x: Option<u8>) { if y.is_some() { g(x.unwrap()); } }\n", 1, "other receiver"),
    (b"fn f(v: Vec<u8>) { if !v.is_empty() && v.last().unwrap() == &0 { } }\n", 0, "non-empty"),
    (b"fn f(v: Vec<u8>) { if v.is_empty() && v.last().unwrap() == &0 { } }\n", 1, "empty check"),
    (
        b"fn f(s: &S) { if s.r.is_some() { let r = (*s.r).as_ref().map(|r| r).unwrap(); } }\n",
        0,
        "guard through as_ref/map/deref",
    ),
    (
        b'fn f() -> String { let mut s = String::new(); writeln!(s, "a").unwrap(); s }\n',
        0,
        "write into a String",
    ),
    (
        b'fn f(s: &mut String) { write!(&mut s, "a").unwrap(); }\n',
        0,
        "write into a &mut String parameter",
    ),
    (
        b'fn f() { let mut o = io::stdout(); writeln!(o, "a").unwrap(); }\n',
        1,
        "write into stdout can fail",
    ),
    (
        b"fn f() { let mut w = String::new(); { let mut w = io::stdout(); "
        b'writeln!(w, "a").unwrap(); } }\n',
        1,
        "a shadowing rebind of the String",
    ),
    (
        b'fn f(o: &mut File) { let c = |o: String| 1; writeln!(o, "a").unwrap(); }\n',
        1,
        "a closure parameter of the same name",
    ),
    (b"fn f(x: Option<u8>) { if x.is_some() { let x = h(); g(x.unwrap()); } }\n", 1, "rebind"),
    (b"fn f(mut x: Option<u8>) { if x.is_some() { x = h(); g(x.unwrap()); } }\n", 1, "assign"),
    (
        b"fn f(s: &mut S) { if s.v.is_some() { s.v = None; g(s.v.unwrap()); } }\n",
        1,
        "field assigned after the guard",
    ),
]


@pytest.mark.parametrize(
    ("source", "expected", "note"),
    _RUST_CANNOT_PANIC,
    ids=[note for _, _, note in _RUST_CANNOT_PANIC],
)
def test_rust_unwrap_that_cannot_panic_is_no_hit(source: bytes, expected: int, note: str):
    hits = [h for h in _hits("rust", source) if h.kind == "unsafe_unwrap"]
    assert len(hits) == expected, note


_RUST_IDIOMS = [
    (b"fn f(m: &Mutex<u8>) { let g = m.lock().unwrap(); }\n", "lock_poison"),
    (b"fn f(m: &RwLock<u8>) { let g = m.write().unwrap(); }\n", "lock_poison"),
    (b"fn f(h: JoinHandle<()>) { h.join().unwrap(); }\n", "thread_join"),
    (b'fn f() { g().expect("config is validated at load"); }\n', "invariant_expect"),
    (b"fn f() { g().expect(MSG); }\n", "invariant_expect"),
    (b'fn f(x: u8) { match x { 0 => {}, _ => unreachable!("x is 0") } }\n', "unreachable"),
    (b"fn f() { g().unwrap(); }\n", None),
    (b"fn f(r: &mut File) { r.read(&mut buf).unwrap(); }\n", None),
    (b'fn f() { g().expect(&format!("bad {}", 1)); }\n', None),
    (b'fn f() { panic!("boom"); }\n', None),
]


@pytest.mark.parametrize(("source", "idiom"), _RUST_IDIOMS)
def test_rust_idiomatic_assertions_are_labelled(source: bytes, idiom: str | None):
    hits = _hits("rust", source)
    assert len(hits) == 1
    assert hits[0].idiom == idiom


def test_rust_unwrap_anchors_on_the_unwrap_line_of_a_chain():
    hits = _hits("rust", b"fn f() {\n    let x = g()\n        .h()\n        .unwrap();\n}\n")
    assert [h.line for h in hits] == [4]


def test_idiomatic_hit_is_labelled_and_not_scored():
    ctx = FileContext(
        file_path="a.rs",
        language="rust",
        nloc=20,
        has_test_file=False,
        module=None,
        error_handling_hits=[
            ErrorHandlingHit("unsafe_unwrap", 3, "lock_poison"),
            ErrorHandlingHit("unsafe_unwrap", 5),
        ],
    )
    idiomatic, plain = ErrorHandlingDetector().detect(ctx)
    assert idiomatic.details == {"kind": "unsafe_unwrap", "idiom": "lock_poison", "deduction": 0.0}
    assert idiomatic.deduction == 0.0
    assert idiomatic.reason == (
        "unwrapping a lock panics only if another thread panicked while holding it"
    )
    assert plain.details == {"kind": "unsafe_unwrap"}
    assert plain.deduction is None
    assert plain.reason == "unwrap/expect turns a recoverable error into a crash"


@pytest.mark.parametrize(
    ("source", "expected", "note"),
    [
        (
            b"try:\n    x()\nexcept Exception:  # noqa: BLE001\n    pass\n",
            {"swallowed_catch": 1},
            "BLE001 suppresses broad_except; swallowed_catch remains",
        ),
        (
            b"try:\n    x()\nexcept Exception:  # noqa: BLE001, E722\n    pass\n",
            {"swallowed_catch": 1},
            "multi-code noqa suppresses broad_except",
        ),
        (
            b"try:\n    x()\nexcept:  # noqa: E722\n    pass\n",
            {"swallowed_catch": 1},
            "E722 suppresses bare_except",
        ),
        (
            b"try:\n    x()\nexcept:  # noqa: BLE001\n    pass\n",
            {"swallowed_catch": 1},
            "BLE001 suppresses bare_except",
        ),
        (
            b"try:\n    x()\nexcept Exception:  # noqa\n    pass\n",
            {},
            "blanket noqa suppresses broad_except and swallowed_catch",
        ),
        (
            b"try:\n    x()\nexcept:  # noqa\n    pass\n",
            {},
            "blanket noqa suppresses bare_except and swallowed_catch",
        ),
        (
            b"try:\n    x()\nexcept ValueError:  # noqa: S110\n    pass\n",
            {},
            "S110 suppresses swallowed_catch",
        ),
        (
            b"try:\n    x()\nexcept Exception:  # noqa: BLE001, S110\n    pass\n",
            {},
            "BLE001 + S110 suppresses both broad_except and swallowed_catch",
        ),
        (
            b"try:\n    x()\nexcept Exception:  # noqa: F401\n    pass\n",
            {"swallowed_catch": 1, "broad_except": 1},
            "unrelated noqa code does not suppress broad_except",
        ),
        (
            b"try:\n    x()\nexcept:  # noqa: F401\n    pass\n",
            {"swallowed_catch": 1, "bare_except": 1},
            "unrelated noqa code does not suppress bare_except",
        ),
        (
            b"try:\n    x()\nexcept Exception:\n    # noqa: BLE001\n    pass\n",
            {"swallowed_catch": 1, "broad_except": 1},
            "noqa on the next line inside block does not suppress header",
        ),
    ],
)
def test_python_noqa_suppression(source: bytes, expected: dict, note: str):
    hits = _hits("python", source)
    by_kind: dict[str, int] = {}
    for h in hits:
        by_kind[h.kind] = by_kind.get(h.kind, 0) + 1
    assert by_kind == expected, f"{note}: expected {expected}, got {by_kind}"


@pytest.mark.parametrize(
    ("language", "source", "expected", "note"),
    [
        ("python", b"try:\n    x()\nexcept asyncio.CancelledError:\n    pass\n", set(), "asyncio"),
        ("python", b"try:\n    x()\nexcept CancelledError:\n    pass\n", set(), "bare name"),
        (
            "python",
            b"try:\n    x()\n"
            b"except (asyncio.CancelledError, concurrent.futures.CancelledError) as e:\n"
            b"    pass\n",
            set(),
            "tuple of cancellations",
        ),
        (
            "python",
            b"try:\n    x()\nexcept (asyncio.CancelledError, ValueError):\n    pass\n",
            {"swallowed_catch"},
            "cancellation mixed with an error is still swallowed",
        ),
        (
            "python",
            b"try:\n    x()\nexcept CancelledError, ValueError:\n    pass\n",
            {"swallowed_catch"},
            "parenthesis-free list keeps every type",
        ),
        (
            "csharp",
            b"class A{ void M(){ try { Go(); } catch (OperationCanceledException) {} } }\n",
            set(),
            "C# OperationCanceledException",
        ),
        (
            "csharp",
            b"class A{ void M(){ try { Go(); } "
            b"catch (System.Threading.Tasks.TaskCanceledException e) {} } }\n",
            set(),
            "C# qualified TaskCanceledException",
        ),
    ],
)
def test_cancellation_catch_is_not_swallowed(
    language: str, source: bytes, expected: set, note: str
):
    assert {h.kind for h in _hits(language, source)} == expected, note


_FALLBACK_BODY = b"    try:\n        return load()\n    except Exception:\n        return None\n"


@pytest.mark.parametrize(
    ("source", "flagged", "note"),
    [
        (b'def f():\n    """Return None on failure."""\n' + _FALLBACK_BODY, False, "docstring"),
        (b'def f():\n    """Best-effort cache read."""\n' + _FALLBACK_BODY, False, "best-effort"),
        (b'def f():\n    """Load it. Never raises."""\n' + _FALLBACK_BODY, False, "never raises"),
        (
            b'def f():\n    """Falls back to an empty dict if the file cannot be read."""\n'
            + _FALLBACK_BODY,
            False,
            "falls back",
        ),
        (
            b"def f():\n    # best-effort: a missing cache is fine\n" + _FALLBACK_BODY,
            False,
            "comment above try",
        ),
        (
            b"def f():\n    try:\n        return load()\n    except Exception:  # best-effort\n"
            b"        return None\n",
            False,
            "comment on the except line",
        ),
        (b'def f():\n    """Return the loaded config."""\n' + _FALLBACK_BODY, True, "undocumented"),
        (
            b'def f():\n    """Returns the value if the key exists; raises on error."""\n'
            + _FALLBACK_BODY,
            True,
            "failure word in another clause",
        ),
        (
            b'def f():\n    """Does not raise ValueError for blanks."""\n' + _FALLBACK_BODY,
            True,
            "a named exception it does not raise is no fallback",
        ),
        (
            b'def f():\n    """Best-effort read."""\n    try:\n        load()\n'
            b"    except Exception:\n        log()\n",
            True,
            "documented but the handler does not return",
        ),
        (
            b'def f():\n    """Best-effort read."""\n    try:\n        return load()\n'
            b"    except:\n        return None\n",
            True,
            "bare except still catches KeyboardInterrupt",
        ),
        (
            b'def f():\n    """Return None on failure."""\n    def g():\n        """Return it."""\n'
            b"        try:\n            return load()\n        except Exception:\n"
            b"            return None\n",
            True,
            "only the nearest function's docstring counts",
        ),
    ],
)
def test_documented_fallback_is_not_broad_except(source: bytes, flagged: bool, note: str):
    kinds = {h.kind for h in _hits("python", source)}
    assert bool(kinds & {"broad_except", "bare_except"}) is flagged, note