Skip to content

Commit

Permalink
ENH: detect missing version information in meson.build when required
Browse files Browse the repository at this point in the history
When pyproject.toml does not contain a "project" section, or when the
"version" field is declared as dynamic, the package version is
retrieved from the one specified in the project() call in meson.build.
However, providing a version in meson.build is optional. Detect when
the version is missing and report an appropriate error.
  • Loading branch information
dnicolodi authored and rgommers committed Aug 25, 2023
1 parent 274d72a commit 92a0e8d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
12 changes: 10 additions & 2 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,18 @@ def __init__( # noqa: C901
self._metadata = Metadata.from_pyproject(pyproject, self._source_dir)
# set version from meson.build if version is declared as dynamic
if 'version' in self._metadata.dynamic:
self._metadata.version = packaging.version.Version(self._meson_version)
version = self._meson_version
if version == 'undefined':
raise pyproject_metadata.ConfigurationError(
'Field "version" declared as dynamic but version is not defined in meson.build')
self._metadata.version = packaging.version.Version(version)
else:
# if project section is missing, use minimal metdata from meson.build
self._metadata = Metadata(name=self._meson_name, version=packaging.version.Version(self._meson_version))
name, version = self._meson_name, self._meson_version
if version == 'undefined':
raise pyproject_metadata.ConfigurationError(
'Section "project" missing in pyproject.toml and version is not defined in meson.build')
self._metadata = Metadata(name=name, version=packaging.version.Version(version))

# verify that we are running on a supported interpreter
if self._metadata.requires_python:
Expand Down
5 changes: 5 additions & 0 deletions tests/packages/missing-dynamic-version/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2023 The meson-python developers
#
# SPDX-License-Identifier: MIT

project('missing-dynamic-version')
11 changes: 11 additions & 0 deletions tests/packages/missing-dynamic-version/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2023 The meson-python developers
#
# SPDX-License-Identifier: MIT

[build-system]
build-backend = 'mesonpy'
requires = ['meson-python']

[project]
name = 'missing-dynamic-version'
dynamic = ['version']
5 changes: 5 additions & 0 deletions tests/packages/missing-meson-version/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2023 The meson-python developers
#
# SPDX-License-Identifier: MIT

project('missing-meson-version')
7 changes: 7 additions & 0 deletions tests/packages/missing-meson-version/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2023 The meson-python developers
#
# SPDX-License-Identifier: MIT

[build-system]
build-backend = 'mesonpy'
requires = ['meson-python']
13 changes: 13 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ def test_missing_version(package_missing_version):
with mesonpy.Project.with_temp_working_dir():
pass


def test_missing_meson_version(package_missing_meson_version):
with pytest.raises(pyproject_metadata.ConfigurationError, match='Section "project" missing in pyproject.toml'):
with mesonpy.Project.with_temp_working_dir():
pass


def test_missing_dynamic_version(package_missing_dynamic_version):
with pytest.raises(pyproject_metadata.ConfigurationError, match='Field "version" declared as dynamic but'):
with mesonpy.Project.with_temp_working_dir():
pass


def test_user_args(package_user_args, tmp_path, monkeypatch):
project_run = mesonpy.Project._run
cmds = []
Expand Down

0 comments on commit 92a0e8d

Please sign in to comment.