Skip to content

Commit 5922e6f

Browse files
committed
TST: enable fatal meson warnings for all tests
Unless explicitly disabled in ``pyproject.toml``. This uses a pytest ``autouse`` fixture to monkeypatch the function that validates the ``pyproject.toml`` meson-python configuration to add ``--fatal-meson-warnings`` to the ``meson setup`` arguments, unless ``--no-fatal-meson-warnings`` is specified by the package.
1 parent 0444e63 commit 5922e6f

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

mesonpy/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,14 @@ def _string_or_strings(value: Any, name: str) -> List[str]:
659659
return config
660660

661661

662+
def _get_meson_args(args: Dict[str, List[str]]) -> MesonArgs:
663+
# This mostly exists so that it can be monkeypatched in tests
664+
meson_args: MesonArgs = collections.defaultdict(list)
665+
for key, value in args.items():
666+
meson_args[key].extend(value)
667+
return meson_args
668+
669+
662670
class Project():
663671
"""Meson project wrapper to generate Python artifacts."""
664672

@@ -674,16 +682,14 @@ def __init__(
674682
self._editable_verbose = editable_verbose
675683
self._meson_native_file = self._build_dir / 'meson-python-native-file.ini'
676684
self._meson_cross_file = self._build_dir / 'meson-python-cross-file.ini'
677-
self._meson_args: MesonArgs = collections.defaultdict(list)
678685
self._limited_api = False
679686

680687
# load pyproject.toml
681688
pyproject = tomllib.loads(self._source_dir.joinpath('pyproject.toml').read_text(encoding='utf-8'))
682-
683-
# load meson args from pyproject.toml
684689
pyproject_config = _validate_pyproject_config(pyproject)
685-
for key, value in pyproject_config.get('args', {}).items():
686-
self._meson_args[key].extend(value)
690+
691+
# get meson args from pyproject.toml
692+
self._meson_args = _get_meson_args(pyproject_config.get('args', {}))
687693

688694
# meson arguments from the command line take precedence over
689695
# arguments from the configuration file thus are added later

tests/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
import argparse
56
import contextlib
67
import importlib.metadata
78
import os
@@ -192,3 +193,28 @@ def cleanenv():
192193
# $MACOSX_DEPLOYMENT_TARGET affects the computation of the platform tag on macOS.
193194
yield mpatch.delenv('MACOSX_DEPLOYMENT_TARGET', raising=False)
194195
mpatch.undo()
196+
197+
198+
@pytest.fixture(autouse=True, scope='session')
199+
def meson_fatal_warnings():
200+
# Cannot use the 'monkeypatch' fixture because of scope mismatch.
201+
mpatch = pytest.MonkeyPatch()
202+
mesonpy_get_meson_args = mesonpy._get_meson_args
203+
204+
def _get_meson_args(args):
205+
meson_args = mesonpy_get_meson_args(args)
206+
meson_setup_args = meson_args['setup']
207+
208+
# Add ``--fatal-meson-warnings`` to the ``meson setup`` arguments
209+
# unless the project specifies ``--no-fatal-meson-warnings`` in
210+
# ``tool.meson-build.args.setup``.
211+
parser = argparse.ArgumentParser(add_help=False)
212+
parser.add_argument('--no-fatal-meson-warnings', action='store_true')
213+
args, meson_setup_args = parser.parse_known_args(meson_setup_args)
214+
if not args.no_fatal_meson_warnings:
215+
meson_setup_args.append('--fatal-meson-warnings')
216+
217+
meson_args['setup'] = meson_setup_args
218+
return meson_args
219+
220+
mpatch.setattr(mesonpy, '_get_meson_args', _get_meson_args)

tests/test_project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def test_ios_project(package_simple, monkeypatch, multiarch, tmp_path):
399399
project = mesonpy.Project(source_dir=package_simple, build_dir=tmp_path)
400400

401401
# Meson configuration points at the cross file
402-
assert project._meson_args['setup'] == ['--cross-file', os.fspath(tmp_path / 'meson-python-cross-file.ini')]
402+
assert project._meson_args['setup'][-2:] == ['--cross-file', os.fspath(tmp_path / 'meson-python-cross-file.ini')]
403403

404404
# Meson config files exist, and have some relevant keys
405405
assert (tmp_path / 'meson-python-native-file.ini').exists()

0 commit comments

Comments
 (0)