Download tests/test_ast_graph.py from tailot/codex-python: direct link, hf CLI and curl.
- Browser
- Download file 4.77 kB
-
https://huggingface.co/datasets/tailot/codex-python/resolve/main/tests/test_ast_graph.py
- Command line
-
hf download hf://datasets/tailot/codex-python/tests/test_ast_graph.py
-
curl -L -o test_ast_graph.py https://huggingface.co/datasets/tailot/codex-python/resolve/main/tests/test_ast_graph.py
4.77 kB
| """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) | |