# SPDX-FileCopyrightText: 2021 The meson-python developers
#
# SPDX-License-Identifier: MIT

import pathlib
import sys

from packaging import requirements, specifiers


if sys.version_info >= (3, 11):
    import tomllib
else:
    import tomli as tomllib

import mesonpy


def test_version():
    pyproject = pathlib.Path(__file__).parent.parent.joinpath('pyproject.toml')
    with open(pyproject, 'rb') as f:
        project_version = tomllib.load(f)['project']['version']
    assert mesonpy.__version__ == project_version


def test_pyproject_dependencies():
    pyproject = pathlib.Path(__file__).parent.parent.joinpath('pyproject.toml')
    with open(pyproject, 'rb') as f:
        data = tomllib.load(f)
    build_dependencies = data['build-system']['requires']
    project_dependencies = data['project']['dependencies']
    # verify that all build dependencies are project dependencies
    assert not set(build_dependencies) - set(project_dependencies), \
        'pyproject.toml is inconsistent: not all "build-system.requires" are in "project.dependencies"'


def test_meson_version_requirement():
    pyproject = pathlib.Path(__file__).parent.parent.joinpath('pyproject.toml')
    with open(pyproject, 'rb') as f:
        data = tomllib.load(f)
    for dep in data['project']['dependencies']:
        req = requirements.Requirement(dep)
        if req.name == 'meson' and req.marker.evaluate():
            assert req.specifier == specifiers.SpecifierSet(f'>= {mesonpy._MESON_REQUIRED_VERSION}')
