the-stack-go / stackgo /tests /test_repo_extract.py
s0duku's picture
Add generic native parser fallback and sample recovery
804ecef verified
Raw History Blame Contribute Delete
2.75 kB
import sys
from types import SimpleNamespace
import pytest
from stackbuild import repo_extract
def _panic_type():
return type("PanicException", (BaseException,), {"__module__": "pyo3_runtime"})
def test_pyo3_parser_panic_falls_back_to_regex(monkeypatch):
panic = _panic_type()
def process(*args, **kwargs):
raise panic("native parser panic")
monkeypatch.setattr(repo_extract, "_downloaded_languages", lambda: frozenset({"rescript"}))
monkeypatch.setitem(sys.modules, "tree_sitter_language_pack", SimpleNamespace(
downloaded_languages=lambda: ["rescript"],
ProcessConfig=lambda **kwargs: kwargs,
process=process,
))
refs, backend = repo_extract.extract_references(
'import "./dep";\n', "rescript", "src/main.res",
)
assert backend == "regex"
assert refs == ()
def test_keyboard_interrupt_is_not_swallowed(monkeypatch):
monkeypatch.setattr(repo_extract, "_downloaded_languages", lambda: frozenset({"rescript"}))
monkeypatch.setitem(sys.modules, "tree_sitter_language_pack", SimpleNamespace(
downloaded_languages=lambda: ["rescript"],
ProcessConfig=lambda **kwargs: kwargs,
process=lambda *args, **kwargs: (_ for _ in ()).throw(KeyboardInterrupt()),
))
with pytest.raises(KeyboardInterrupt):
repo_extract.treesitter_references("", "rescript", "src/main.res")
def test_crystal_uses_regex_without_calling_native_parser(monkeypatch):
monkeypatch.setattr(repo_extract, "_downloaded_languages", lambda: frozenset({"crystal"}))
monkeypatch.setitem(sys.modules, "tree_sitter_language_pack", SimpleNamespace(
downloaded_languages=lambda: ["crystal"],
ProcessConfig=lambda **kwargs: kwargs,
process=lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("must not run")),
))
refs, backend = repo_extract.extract_references(
'require "json"\n', "Crystal", "src/main.cr",
)
assert backend == "regex"
assert refs == (repo_extract.Reference("json", "require", 'require "json"'),)
def test_lua_uses_regex_without_calling_native_parser(monkeypatch):
monkeypatch.setattr(repo_extract, "_downloaded_languages", lambda: frozenset({"lua"}))
monkeypatch.setitem(sys.modules, "tree_sitter_language_pack", SimpleNamespace(
downloaded_languages=lambda: ["lua"],
ProcessConfig=lambda **kwargs: kwargs,
process=lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("must not run")),
))
refs, backend = repo_extract.extract_references(
'local json = require("json")\n', "Lua", "src/main.lua",
)
assert backend == "regex"
assert refs == (repo_extract.Reference("json", "require", 'require("json")'),)