summaryrefslogtreecommitdiff
path: root/tests/test_metadata.py
blob: c719b4d5b4f2e88a57979c85da486e876d33cefc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import re
import sys

import toml
import adblock


def parse_version(version):
    parts = version.split(".")
    return tuple(map(int, parts))


def get_version_value_cargo():
    with open("Cargo.toml", encoding="utf-8") as f:
        cargo_toml = toml.loads(f.read())
    return parse_version(cargo_toml["package"]["version"])


def get_version_values_pyproject():
    with open("pyproject.toml", encoding="utf-8") as f:
        pyproject_toml = toml.loads(f.read())
    return [
        parse_version(pyproject_toml["project"]["version"]),
        parse_version(pyproject_toml["tool"]["poetry"]["version"]),
    ]


def get_version_value_changelog():
    """
    Try to get the names of all classes that we added to the Python module
    from Rust. As always, we unfortunately don't have access to the Rust AST
    so we have to make do with regular expressions.
    """
    versions = []
    with open("CHANGELOG.md", "r", encoding="utf-8") as f:
        for line in f:
            match = re.match(
                r"## ([0-9]+\.[0-9]+\.[0-9]+) - \(20[0-9]+-[0-1][0-9]-[0-3][0-9]\)",
                line.strip(),
            )
            if match is not None:
                versions.append(parse_version(match.group(1)))
    assert versions == sorted(versions, reverse=True)
    return versions[0]


def test_version_numbers_all_same():
    """
    Makes sure that `pyproject.toml`, `Cargo.toml` and `CHANGELOG.md` contain
    the same version number as the one attached to the `adblock` module.
    """
    cargo_version = get_version_value_cargo()
    changelog_version = get_version_value_changelog()
    pyproject_versions = get_version_values_pyproject()
    module_version = parse_version(adblock.__version__)

    assert cargo_version == module_version
    assert module_version == changelog_version
    assert changelog_version == pyproject_versions[0]
    assert pyproject_versions[0] == pyproject_versions[1]


def get_current_python_version():
    return (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)


def test_required_python_version():
    """
    Make sure that the Python interpreter we're running this test suite on
    falls into the required Python range.
    """
    with open("pyproject.toml", encoding="utf-8") as f:
        pyproject_toml = toml.loads(f.read())

    required_python = pyproject_toml["project"]["requires-python"]
    assert required_python.startswith(">=")
    required_python = required_python[2:]
    assert get_current_python_version() >= parse_version(required_python)