python-adblock

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

test_metadata.py (2550B)


      1 import re
      2 import sys
      3 
      4 import toml
      5 import adblock
      6 
      7 
      8 def parse_version(version):
      9     parts = version.split(".")
     10     return tuple(map(int, parts))
     11 
     12 
     13 def get_version_value_cargo():
     14     with open("Cargo.toml", encoding="utf-8") as f:
     15         cargo_toml = toml.loads(f.read())
     16     return parse_version(cargo_toml["package"]["version"])
     17 
     18 
     19 def get_version_values_pyproject():
     20     with open("pyproject.toml", encoding="utf-8") as f:
     21         pyproject_toml = toml.loads(f.read())
     22     return [
     23         parse_version(pyproject_toml["project"]["version"]),
     24         parse_version(pyproject_toml["tool"]["poetry"]["version"]),
     25     ]
     26 
     27 
     28 def get_version_value_changelog():
     29     """
     30     Try to get the names of all classes that we added to the Python module
     31     from Rust. As always, we unfortunately don't have access to the Rust AST
     32     so we have to make do with regular expressions.
     33     """
     34     versions = []
     35     with open("CHANGELOG.md", "r", encoding="utf-8") as f:
     36         for line in f:
     37             match = re.match(
     38                 r"## ([0-9]+\.[0-9]+\.[0-9]+) - \(20[0-9]+-[0-1][0-9]-[0-3][0-9]\)",
     39                 line.strip(),
     40             )
     41             if match is not None:
     42                 versions.append(parse_version(match.group(1)))
     43     assert versions == sorted(versions, reverse=True)
     44     return versions[0]
     45 
     46 
     47 def test_version_numbers_all_same():
     48     """
     49     Makes sure that `pyproject.toml`, `Cargo.toml` and `CHANGELOG.md` contain
     50     the same version number as the one attached to the `adblock` module.
     51     """
     52     cargo_version = get_version_value_cargo()
     53     changelog_version = get_version_value_changelog()
     54     pyproject_versions = get_version_values_pyproject()
     55     module_version = parse_version(adblock.__version__)
     56 
     57     assert cargo_version == module_version
     58     assert module_version == changelog_version
     59     assert changelog_version == pyproject_versions[0]
     60     assert pyproject_versions[0] == pyproject_versions[1]
     61 
     62 
     63 def get_current_python_version():
     64     return (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)
     65 
     66 
     67 def test_required_python_version():
     68     """
     69     Make sure that the Python interpreter we're running this test suite on
     70     falls into the required Python range.
     71     """
     72     with open("pyproject.toml", encoding="utf-8") as f:
     73         pyproject_toml = toml.loads(f.read())
     74 
     75     required_python = pyproject_toml["project"]["requires-python"]
     76     assert required_python.startswith(">=")
     77     required_python = required_python[2:]
     78     assert get_current_python_version() >= parse_version(required_python)