Spaces:
Sleeping

File size: 4,743 Bytes
6d50eaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""Unit tests for the AST code analyzer."""

from __future__ import annotations

import pytest

from intellicode.analysis import CodeAnalyzer


@pytest.fixture
def analyzer() -> CodeAnalyzer:
    return CodeAnalyzer()


# ── Individual anti-pattern detection ────────────────────────────────────────


@pytest.mark.parametrize(
    ("code", "expected_type"),
    [
        ("def f(x=[]):\n    return x", "Mutable default argument"),
        ("def f(x={}):\n    return x", "Mutable default argument"),
        ("def f(x=set()):\n    return x", "Mutable default argument"),
        ("try:\n    pass\nexcept:\n    pass", "Bare except clause"),
        ("try:\n    pass\nexcept Exception:\n    pass", "Broad exception handler"),
        ("from os import *", "Star import"),
        ("def f():\n    global x\n    x = 1", "Global statement"),
        ("def f(x):\n    assert x > 0\n    return x", "Assert in production code"),
    ],
)
def test_detects_pattern(analyzer, code, expected_type):
    """Each anti-pattern is flagged with the correct issue type."""
    result = analyzer.analyze_code(code)
    assert result.success
    assert expected_type in {issue.type for issue in result.issues}


def test_detects_unused_variable(analyzer):
    result = analyzer.analyze_code("def f():\n    unused = 5\n    return 10")
    assert "Unused variable" in {i.type for i in result.issues}


def test_underscore_variable_not_flagged_as_unused(analyzer):
    """Names prefixed with underscore are intentionally-unused and ignored."""
    result = analyzer.analyze_code("def f():\n    _ignored = 5\n    return 10")
    assert "Unused variable" not in {i.type for i in result.issues}


# ── Async support (the original bug) ─────────────────────────────────────────


def test_async_function_mutable_default(analyzer):
    """AsyncFunctionDef must be analyzed, not skipped (regression guard)."""
    result = analyzer.analyze_code("async def fetch(cache=[]):\n    return cache")
    assert "Mutable default argument" in {i.type for i in result.issues}


def test_async_function_bare_except(analyzer):
    code = "async def run():\n    try:\n        await x()\n    except:\n        pass"
    result = analyzer.analyze_code(code)
    assert "Bare except clause" in {i.type for i in result.issues}


# ── Complexity & nesting ─────────────────────────────────────────────────────


def test_deep_nesting_detected(analyzer):
    code = (
        "def f(data):\n"
        "    for a in data:\n"
        "        if a:\n"
        "            while a:\n"
        "                if a > 1:\n"
        "                    a -= 1"
    )
    assert "Deep nesting" in {i.type for i in analyzer.analyze_code(code).issues}


def test_high_complexity_detected():
    analyzer = CodeAnalyzer(complexity_threshold=3)
    code = "def f(a, b, c):\n    if a:\n        pass\n    if b:\n        pass\n    if c:\n        pass"
    assert "High cyclomatic complexity" in {i.type for i in analyzer.analyze_code(code).issues}


# ── Metrics ──────────────────────────────────────────────────────────────────


def test_metrics_counts(analyzer):
    code = "# a comment\nclass A:\n    def m(self):\n        return 1\n\ndef g():\n    return 2"
    result = analyzer.analyze_code(code)
    assert result.metrics["functions"] == 2
    assert result.metrics["classes"] == 1
    assert result.metrics["comment_lines"] == 1


# ── Edge cases ───────────────────────────────────────────────────────────────


def test_syntax_error_returns_failure(analyzer):
    result = analyzer.analyze_code("def f(:\n    pass")
    assert not result.success
    assert "Syntax Error" in result.error


def test_empty_code_succeeds_with_no_issues(analyzer):
    result = analyzer.analyze_code("")
    assert result.success
    assert result.issues == []


def test_clean_code_has_no_high_severity(analyzer, clean_code_path):
    result = analyzer.analyze_file(str(clean_code_path))
    assert result.success
    assert all(i.severity != "HIGH" for i in result.issues)


def test_missing_file_returns_failure(analyzer):
    result = analyzer.analyze_file("does_not_exist_12345.py")
    assert not result.success
    assert "not found" in result.error.lower()