python-adblock

Updated Brave adblock library for Python
Log | Files | Refs | README

test_imports.py (1019B)


      1 import re
      2 import adblock
      3 
      4 
      5 def get_added_classes():
      6     """
      7     Try to get the names of all classes that we added to the Python module
      8     from Rust. As always, we unfortunately don't have access to the Rust AST
      9     so we have to make do with regular expressions.
     10     """
     11     classes = []
     12     with open("src/lib.rs", "r", encoding="utf-8") as rs_f:
     13         for line in rs_f:
     14             match = re.match(r"m\.add_class::<(.+)>\(\)\?;", line.strip())
     15             if match is not None:
     16                 classes.append(match.group(1))
     17                 continue
     18     return classes
     19 
     20 
     21 def test_added_classes():
     22     """
     23     Make sure that there's no class that we added in Rust but didn't import in
     24     `__init__.py`.
     25     """
     26     added_classes = get_added_classes()
     27     for c in added_classes:
     28         assert c in adblock.__all__
     29 
     30 
     31 def test_dunder_all_classes_imported():
     32     """
     33     Make sure that there's no class in `__all__` that we haven't imported.
     34     """
     35     for c in adblock.__all__:
     36         assert hasattr(adblock, c)