File size: 4,771 Bytes
339ddd8
 
 
 
e1de134
339ddd8
 
48f2b75
 
e1de134
 
 
 
48f2b75
e1de134
 
339ddd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48f2b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339ddd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1de134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for Python AST symbol, inheritance, import, and call graph extraction."""

from __future__ import annotations

import codecs
from pathlib import Path

import pytest

from lingologos_python.ast_graph import (
    discover_python_files,
    extract_python_graph,
    importers_of,
    is_linked_path,
    module_names_for_file,
)


def test_discover_python_files_ignores_caches_and_venvs(sample_pkg: Path) -> None:
    (sample_pkg / "__pycache__").mkdir()
    (sample_pkg / "__pycache__" / "junk.py").write_text("x = 1\n", encoding="utf-8")
    (sample_pkg / ".venv" / "lib").mkdir(parents=True)
    (sample_pkg / ".venv" / "lib" / "site.py").write_text("x = 2\n", encoding="utf-8")

    files = discover_python_files(sample_pkg)
    assert files == [
        "src/mathops.py",
        "src/textops.py",
        "tests/test_mathops.py",
        "tests/test_textops.py",
    ]


def test_discover_python_files_prunes_links_instead_of_following_them(
    sample_pkg: Path, tmp_path: Path, dir_link
) -> None:
    """A junction must not drag files that live outside the workspace into the graph."""
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "faraway.py").write_text("x = 1\n", encoding="utf-8", newline="")
    if not dir_link(sample_pkg / "vendor", outside):
        pytest.skip("this host offers no way to create a directory link")
    files = discover_python_files(sample_pkg)
    assert not any(rel.startswith("vendor/") for rel in files)
    assert "vendor/faraway.py" not in files
    assert is_linked_path(sample_pkg / "vendor")
    assert not is_linked_path(sample_pkg / "src" / "mathops.py")


def test_module_names_for_file_strips_src_and_init() -> None:
    assert "mathops" in module_names_for_file("src/mathops.py")
    assert "src.mathops" in module_names_for_file("src/mathops.py")
    assert "pkg" in module_names_for_file("src/pkg/__init__.py")


def test_extract_python_graph_captures_classes_methods_inheritance_and_calls(
    sample_pkg: Path,
) -> None:
    files = discover_python_files(sample_pkg)
    symbols, edges = extract_python_graph(sample_pkg, files)

    assert "src/mathops.py::Accumulator" in symbols
    assert "src/mathops.py::BoundedAccumulator" in symbols
    assert "src/mathops.py::BoundedAccumulator.add" in symbols
    assert "src/mathops.py::double_sum" in symbols
    assert ("src/mathops.py::BoundedAccumulator", "src/mathops.py::Accumulator") in edges
    assert ("src/mathops.py::double_sum", "src/mathops.py::add_numbers") in edges
    assert ("src/textops.py", "src/mathops.py") in edges
    assert ("src/textops.py::format_sum", "src/mathops.py::add_numbers") in edges


def test_extract_python_graph_handles_relative_imports_and_syntax_errors(tmp_path: Path) -> None:
    pkg = tmp_path / "pkg"
    pkg.mkdir()
    (pkg / "__init__.py").write_text("", encoding="utf-8")
    (pkg / "core.py").write_text("def compute():\n    return 42\n", encoding="utf-8")
    (pkg / "caller.py").write_text(
        "from .core import compute\n\ndef run():\n    return compute()\n",
        encoding="utf-8",
    )
    (pkg / "broken.py").write_text("def oops(:\n", encoding="utf-8")

    files = discover_python_files(tmp_path)
    symbols, edges = extract_python_graph(tmp_path, files)
    assert "pkg/broken.py" in symbols
    assert ("pkg/caller.py", "pkg/core.py") in edges
    assert ("pkg/caller.py::run", "pkg/core.py::compute") in edges


def test_importers_of_names_the_survivors_that_import_the_target(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    (tmp_path / "src" / "tokens.py").write_text(
        "def count_words(text: str) -> int:\n    return len(text.split())\n",
        encoding="utf-8",
    )
    (tmp_path / "src" / "report.py").write_text(
        "from src.tokens import count_words\n\n\ndef render(text: str) -> int:\n    return count_words(text)\n",
        encoding="utf-8",
    )
    (tmp_path / "src" / "unused.py").write_text("VALUE = 1\n", encoding="utf-8")

    survivors = ["src/report.py", "src/unused.py"]
    assert importers_of(tmp_path, survivors, "src/tokens.py") == ["src/report.py"]


def test_the_graph_reads_a_byte_order_mark_as_text(tmp_path: Path) -> None:
    (tmp_path / "bom.py").write_bytes(codecs.BOM_UTF8 + b"def go() -> int:\n    return 1\n")
    files = discover_python_files(tmp_path)
    symbols, _ = extract_python_graph(tmp_path, files)
    assert "bom.py::go" in symbols


def test_the_graph_invents_no_symbols_from_undecodable_bytes(tmp_path: Path) -> None:
    (tmp_path / "bytes.py").write_bytes(b"def \xffbad():\n    pass\n")
    files = discover_python_files(tmp_path)
    symbols, _ = extract_python_graph(tmp_path, files)
    assert "bytes.py" in symbols
    assert not any(name.startswith("bytes.py::") for name in symbols)