feat(bindings): improve python binding test

Previously, the test would not detect ABI incompatibilities.

(cherry picked from commit 8c61bbdb73)
This commit is contained in:
Robert Muir 2025-07-02 22:29:36 -04:00 committed by Will Lillis
parent 3c0088f037
commit d507a2defb
2 changed files with 28 additions and 10 deletions

View file

@ -608,14 +608,32 @@ pub fn generate_grammar_files(
})?;
missing_path(path.join("tests"), create_dir)?.apply(|path| {
missing_path(path.join("test_binding.py"), |path| {
generate_file(
path,
TEST_BINDING_PY_TEMPLATE,
language_name,
&generate_opts,
)
})?;
missing_path_else(
path.join("test_binding.py"),
allow_update,
|path| {
generate_file(
path,
TEST_BINDING_PY_TEMPLATE,
language_name,
&generate_opts,
)
},
|path| {
let mut contents = fs::read_to_string(path)?;
if !contents.contains("Parser(Language(") {
contents = contents
.replace("tree_sitter.Language(", "Parser(Language(")
.replace(".language())\n", ".language()))\n")
.replace(
"import tree_sitter\n",
"from tree_sitter import Language, Parser\n",
);
write_file(path, contents)?;
}
Ok(())
},
)?;
Ok(())
})?;

View file

@ -1,12 +1,12 @@
from unittest import TestCase
import tree_sitter
from tree_sitter import Language, Parser
import tree_sitter_LOWER_PARSER_NAME
class TestLanguage(TestCase):
def test_can_load_grammar(self):
try:
tree_sitter.Language(tree_sitter_LOWER_PARSER_NAME.language())
Parser(Language(tree_sitter_LOWER_PARSER_NAME.language()))
except Exception:
self.fail("Error loading TITLE_PARSER_NAME grammar")