From 25684535e85a8bbb05b65b0da2304273d1d90ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Mon, 27 Jul 2020 20:35:50 +0000 Subject: Update upstream library to 0.3.0, respond to API changes (#10) * Update upstream library to 0.3.0, respond to API changes * Change pypi publishing criteria in CI * Create test to make sure the version numbers are the same everywhere --- tests/test_typestubs.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/test_typestubs.py (limited to 'tests/test_typestubs.py') diff --git a/tests/test_typestubs.py b/tests/test_typestubs.py new file mode 100644 index 0000000..6a8d75f --- /dev/null +++ b/tests/test_typestubs.py @@ -0,0 +1,53 @@ +import ast +import re + + +def read_stubfile(): + with open("adblock/adblock.pyi", encoding="utf-8") as file: + node = ast.parse(file.read()) + return node + + +def get_functions_and_methods(node): + functions = [n for n in node.body if isinstance(n, ast.FunctionDef)] + classes = [n for n in node.body if isinstance(n, ast.ClassDef)] + + methods = {} + for c in classes: + methods[c.name] = [n for n in c.body if isinstance(n, ast.FunctionDef)] + + return functions, methods + + +def pattern_exists_in_file(filename, regex): + """ + Checks if the given regex is present in the given file + """ + with open(filename, "r", encoding="utf-8") as f: + for line in f: + if re.search(regex, line): + return True + return False + + +def test_functions_and_methods_exist_in_rust(): + """ + Check that for each of the functions and methods present in the Python + typestub file, there is a line in `src/lib.rs` containing a matching + definition. Since we're doing a naive grep search, without access to the + Rust AST, there may be false negatives. + """ + stubfile_node = read_stubfile() + functions, methods = get_functions_and_methods(stubfile_node) + + methods_flattened = [] + for class_methods in methods.values(): + methods_flattened += class_methods + + for f in functions + methods_flattened: + if f.name.startswith("__"): + # Skip dunder methods since their names are the same for every + # class, making the test not particularly useful. They are also not + # marked `pub` in Rust. + continue + assert pattern_exists_in_file("src/lib.rs", r"pub fn {}".format(f.name)) -- cgit v1.2.3