| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2016-2021 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
import subprocess |
| 6 |
import tempfile |
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import unittest |
| 10 |
import typing as T |
| 11 |
from contextlib import contextmanager |
| 12 |
|
| 13 |
from mesonbuild.mesonlib import ( |
| 14 |
MachineChoice, is_windows, is_osx, windows_proof_rmtree, windows_proof_rm |
| 15 |
) |
| 16 |
from mesonbuild.compilers import ( |
| 17 |
detect_objc_compiler, detect_objcpp_compiler |
| 18 |
) |
| 19 |
from mesonbuild.mesonlib import EnvironmentException, MesonException |
| 20 |
from mesonbuild.programs import ExternalProgram |
| 21 |
|
| 22 |
|
| 23 |
from run_tests import ( |
| 24 |
get_fake_env |
| 25 |
) |
| 26 |
|
| 27 |
from .baseplatformtests import BasePlatformTests |
| 28 |
from .helpers import * |
| 29 |
|
| 30 |
@contextmanager |
| 31 |
def no_pkgconfig(): |
| 32 |
''' |
| 33 |
A context manager that overrides shutil.which and ExternalProgram to force |
| 34 |
them to return None for pkg-config to simulate it not existing. |
| 35 |
''' |
| 36 |
old_which = shutil.which |
| 37 |
old_search = ExternalProgram._search |
| 38 |
|
| 39 |
def new_search(self, name, search_dirs, exclude_paths): |
| 40 |
if name == 'pkg-config': |
| 41 |
return [] |
| 42 |
return old_search(self, name, search_dirs, exclude_paths) |
| 43 |
|
| 44 |
def new_which(cmd, **kwargs): |
| 45 |
if cmd == 'pkg-config': |
| 46 |
return None |
| 47 |
return old_which(cmd, **kwargs) |
| 48 |
|
| 49 |
shutil.which = new_which |
| 50 |
ExternalProgram._search = new_search |
| 51 |
try: |
| 52 |
yield |
| 53 |
finally: |
| 54 |
shutil.which = old_which |
| 55 |
ExternalProgram._search = old_search |
| 56 |
|
| 57 |
class FailureTests(BasePlatformTests): |
| 58 |
''' |
| 59 |
Tests that test failure conditions. Build files here should be dynamically |
| 60 |
generated and static tests should go into `test cases/failing*`. |
| 61 |
This is useful because there can be many ways in which a particular |
| 62 |
function can fail, and creating failing tests for all of them is tedious |
| 63 |
and slows down testing. |
| 64 |
''' |
| 65 |
dnf = "[Dd]ependency.*not found(:.*)?" |
| 66 |
nopkg = '[Pp]kg-config.*not found' |
| 67 |
|
| 68 |
def setUp(self): |
| 69 |
super().setUp() |
| 70 |
self.srcdir = os.path.realpath(tempfile.mkdtemp()) |
| 71 |
self.mbuild = os.path.join(self.srcdir, 'meson.build') |
| 72 |
self.moptions = os.path.join(self.srcdir, 'meson.options') |
| 73 |
if not os.path.exists(self.moptions): |
| 74 |
self.moptions = os.path.join(self.srcdir, 'meson_options.txt') |
| 75 |
|
| 76 |
def tearDown(self): |
| 77 |
super().tearDown() |
| 78 |
windows_proof_rmtree(self.srcdir) |
| 79 |
|
| 80 |
def assertMesonRaises(self, contents: str, |
| 81 |
match: T.Union[str, T.Pattern[str]], *, |
| 82 |
extra_args: T.Optional[T.List[str]] = None, |
| 83 |
langs: T.Optional[T.List[str]] = None, |
| 84 |
meson_version: T.Optional[str] = None, |
| 85 |
options: T.Optional[str] = None, |
| 86 |
override_envvars: T.Optional[T.MutableMapping[str, str]] = None) -> None: |
| 87 |
''' |
| 88 |
Assert that running meson configure on the specified @contents raises |
| 89 |
a error message matching regex @match. |
| 90 |
''' |
| 91 |
if langs is None: |
| 92 |
langs = [] |
| 93 |
with open(self.mbuild, 'w', encoding='utf-8') as f: |
| 94 |
f.write("project('failure test', 'c', 'cpp'") |
| 95 |
if meson_version: |
| 96 |
f.write(f", meson_version: '{meson_version}'") |
| 97 |
f.write(")\n") |
| 98 |
for lang in langs: |
| 99 |
f.write(f"add_languages('{lang}', required : false)\n") |
| 100 |
f.write(contents) |
| 101 |
if options is not None: |
| 102 |
with open(self.moptions, 'w', encoding='utf-8') as f: |
| 103 |
f.write(options) |
| 104 |
o = {'MESON_FORCE_BACKTRACE': '1'} |
| 105 |
if override_envvars is None: |
| 106 |
override_envvars = o |
| 107 |
else: |
| 108 |
override_envvars.update(o) |
| 109 |
# Force tracebacks so we can detect them properly |
| 110 |
with self.assertRaisesRegex(MesonException, match, msg=contents): |
| 111 |
# Must run in-process or we'll get a generic CalledProcessError |
| 112 |
self.init(self.srcdir, extra_args=extra_args, |
| 113 |
inprocess=True, |
| 114 |
override_envvars = override_envvars) |
| 115 |
|
| 116 |
def obtainMesonOutput(self, contents, match, extra_args, langs, meson_version=None): |
| 117 |
if langs is None: |
| 118 |
langs = [] |
| 119 |
with open(self.mbuild, 'w', encoding='utf-8') as f: |
| 120 |
f.write("project('output test', 'c', 'cpp'") |
| 121 |
if meson_version: |
| 122 |
f.write(f", meson_version: '{meson_version}'") |
| 123 |
f.write(")\n") |
| 124 |
for lang in langs: |
| 125 |
f.write(f"add_languages('{lang}', required : false)\n") |
| 126 |
f.write(contents) |
| 127 |
# Run in-process for speed and consistency with assertMesonRaises |
| 128 |
return self.init(self.srcdir, extra_args=extra_args, inprocess=True) |
| 129 |
|
| 130 |
def assertMesonOutputs(self, contents, match, extra_args=None, langs=None, meson_version=None): |
| 131 |
''' |
| 132 |
Assert that running meson configure on the specified @contents outputs |
| 133 |
something that matches regex @match. |
| 134 |
''' |
| 135 |
out = self.obtainMesonOutput(contents, match, extra_args, langs, meson_version) |
| 136 |
self.assertRegex(out, match) |
| 137 |
|
| 138 |
def assertMesonDoesNotOutput(self, contents, match, extra_args=None, langs=None, meson_version=None): |
| 139 |
''' |
| 140 |
Assert that running meson configure on the specified @contents does not output |
| 141 |
something that matches regex @match. |
| 142 |
''' |
| 143 |
out = self.obtainMesonOutput(contents, match, extra_args, langs, meson_version) |
| 144 |
self.assertNotRegex(out, match) |
| 145 |
|
| 146 |
@skipIfNoPkgconfigDep('zlib') |
| 147 |
def test_dependency(self): |
| 148 |
a = (("dependency('zlib', method : 'fail')", 'dependency keyword argument "method" must be one of auto, builtin, cmake, config-tool, cups-config, dub, extraframework, libwmf-config, pcap-config, pkg-config, qmake, sdlconfig, sysconfig, system, not fail'), |
| 149 |
("dependency('zlib', static : '1')", "dependency keyword argument 'static' was of type str but should have been one of: bool, NoneType"), |
| 150 |
("dependency('zlib', version : 1)", r"dependency keyword argument 'version' was of type array\[int\] but should have been array\[str\]"), |
| 151 |
("dependency('zlib', required : 1)", "dependency keyword argument 'required' was of type int but should have been one of: bool, Feature"), |
| 152 |
("dependency('zlib', method : 1)", "dependency keyword argument 'method' was of type int but should have been str"), |
| 153 |
("dependency('zlibfail')", self.dnf),) |
| 154 |
for contents, match in a: |
| 155 |
self.assertMesonRaises(contents, match) |
| 156 |
|
| 157 |
def test_apple_frameworks_dependency(self): |
| 158 |
if not is_osx(): |
| 159 |
raise unittest.SkipTest('only run on macOS') |
| 160 |
self.assertMesonRaises("dependency('appleframeworks')", |
| 161 |
"requires at least one module") |
| 162 |
|
| 163 |
def test_extraframework_dependency_method(self): |
| 164 |
code = "dependency('metal', method : 'extraframework')" |
| 165 |
if not is_osx(): |
| 166 |
self.assertMesonRaises(code, self.dnf) |
| 167 |
else: |
| 168 |
# metal framework is always available on macOS |
| 169 |
self.assertMesonOutputs(code, '[Dd]ependency.*metal.*found.*YES') |
| 170 |
|
| 171 |
def test_sdl2_notfound_dependency(self): |
| 172 |
# Want to test failure, so skip if available |
| 173 |
if shutil.which('sdl2-config'): |
| 174 |
raise unittest.SkipTest('sdl2-config found') |
| 175 |
self.assertMesonRaises("dependency('sdl2', method : 'sdlconfig')", self.dnf) |
| 176 |
if shutil.which('pkg-config'): |
| 177 |
self.assertMesonRaises("dependency('sdl2', method : 'pkg-config')", self.dnf) |
| 178 |
with no_pkgconfig(): |
| 179 |
# Look for pkg-config, cache it, then |
| 180 |
# Use cached pkg-config without erroring out, then |
| 181 |
# Use cached pkg-config to error out |
| 182 |
code = "dependency('foobarrr', method : 'pkg-config', required : false)\n" \ |
| 183 |
"dependency('foobarrr2', method : 'pkg-config', required : false)\n" \ |
| 184 |
"dependency('sdl2', method : 'pkg-config')" |
| 185 |
self.assertMesonRaises(code, self.nopkg) |
| 186 |
|
| 187 |
def test_gnustep_notfound_dependency(self): |
| 188 |
# Want to test failure, so skip if available |
| 189 |
if shutil.which('gnustep-config'): |
| 190 |
raise unittest.SkipTest('gnustep-config found') |
| 191 |
self.assertMesonRaises("dependency('gnustep')", |
| 192 |
f"(requires a Objc compiler|{self.dnf})", |
| 193 |
langs = ['objc']) |
| 194 |
|
| 195 |
def test_wx_notfound_dependency(self): |
| 196 |
# Want to test failure, so skip if available |
| 197 |
if shutil.which('wx-config-3.0') or shutil.which('wx-config') or shutil.which('wx-config-gtk3'): |
| 198 |
raise unittest.SkipTest('wx-config, wx-config-3.0 or wx-config-gtk3 found') |
| 199 |
self.assertMesonRaises("dependency('wxwidgets')", self.dnf) |
| 200 |
self.assertMesonOutputs("dependency('wxwidgets', required : false)", |
| 201 |
"Run-time dependency .*WxWidgets.* found: .*NO.*") |
| 202 |
|
| 203 |
def test_wx_dependency(self): |
| 204 |
if not shutil.which('wx-config-3.0') and not shutil.which('wx-config') and not shutil.which('wx-config-gtk3'): |
| 205 |
raise unittest.SkipTest('Neither wx-config, wx-config-3.0 nor wx-config-gtk3 found') |
| 206 |
self.assertMesonRaises("dependency('wxwidgets', modules : 1)", |
| 207 |
r"dependency keyword argument 'modules' was of type array\[int\] but should have been array\[str\]") |
| 208 |
|
| 209 |
def test_llvm_dependency(self): |
| 210 |
self.assertMesonRaises("dependency('llvm', modules : 'fail')", |
| 211 |
f"(required.*fail|{self.dnf})") |
| 212 |
|
| 213 |
def test_boost_notfound_dependency(self): |
| 214 |
# Can be run even if Boost is found or not |
| 215 |
self.assertMesonRaises("dependency('boost', modules : 1)", |
| 216 |
r"dependency keyword argument 'modules' was of type array\[int\] but should have been array\[str\]") |
| 217 |
self.assertMesonRaises("dependency('boost', modules : 'fail')", |
| 218 |
f"(fail.*not found|{self.dnf})") |
| 219 |
|
| 220 |
def test_boost_BOOST_ROOT_dependency(self): |
| 221 |
# Test BOOST_ROOT; can be run even if Boost is found or not |
| 222 |
self.assertMesonRaises("dependency('boost')", |
| 223 |
f"(boost_root.*absolute|{self.dnf})", |
| 224 |
override_envvars = {'BOOST_ROOT': 'relative/path'}) |
| 225 |
|
| 226 |
def test_dependency_invalid_method(self): |
| 227 |
code = '''zlib_dep = dependency('zlib', required : false) |
| 228 |
zlib_dep.get_configtool_variable('foo') |
| 229 |
''' |
| 230 |
self.assertMesonRaises(code, ".* is not a config-tool dependency") |
| 231 |
code = '''zlib_dep = dependency('zlib', required : false) |
| 232 |
dep = declare_dependency(dependencies : zlib_dep) |
| 233 |
dep.get_pkgconfig_variable('foo') |
| 234 |
''' |
| 235 |
self.assertMesonRaises(code, ".*is not a pkgconfig dependency") |
| 236 |
code = '''zlib_dep = dependency('zlib', required : false) |
| 237 |
dep = declare_dependency(dependencies : zlib_dep) |
| 238 |
dep.get_configtool_variable('foo') |
| 239 |
''' |
| 240 |
self.assertMesonRaises(code, ".* is not a config-tool dependency") |
| 241 |
|
| 242 |
def test_objc_detection(self) -> None: |
| 243 |
''' |
| 244 |
Test that when we can't detect objc or objcpp, we fail gracefully. |
| 245 |
''' |
| 246 |
env = get_fake_env() |
| 247 |
try: |
| 248 |
detect_objc_compiler(env, MachineChoice.HOST) |
| 249 |
except EnvironmentException as e: |
| 250 |
self.assertRegex(str(e), r"(Unknown compiler|GCC was not built with support)") |
| 251 |
else: |
| 252 |
raise unittest.SkipTest('Working objective-c Compiler found, cannot test error.') |
| 253 |
|
| 254 |
def test_objcpp_detection(self) -> None: |
| 255 |
env = get_fake_env() |
| 256 |
try: |
| 257 |
detect_objcpp_compiler(env, MachineChoice.HOST) |
| 258 |
except EnvironmentException as e: |
| 259 |
self.assertRegex(str(e), r"(Unknown compiler|GCC was not built with support)") |
| 260 |
else: |
| 261 |
raise unittest.SkipTest('Working objective-c++ Compiler found, cannot test error.') |
| 262 |
|
| 263 |
def test_subproject_variables(self): |
| 264 |
''' |
| 265 |
Test that: |
| 266 |
1. The correct message is outputted when a not-required dep is not |
| 267 |
found and the fallback subproject is also not found. |
| 268 |
2. A not-required fallback dependency is not found because the |
| 269 |
subproject failed to parse. |
| 270 |
3. A not-found not-required dep with a fallback subproject outputs the |
| 271 |
correct message when the fallback subproject is found but the |
| 272 |
variable inside it is not. |
| 273 |
4. A fallback dependency is found from the subproject parsed in (3) |
| 274 |
5. A wrap file from a subproject is used but fails because it does not |
| 275 |
contain required keys. |
| 276 |
''' |
| 277 |
tdir = os.path.join(self.unit_test_dir, '20 subproj dep variables') |
| 278 |
stray_file = os.path.join(tdir, 'subprojects/subsubproject.wrap') |
| 279 |
if os.path.exists(stray_file): |
| 280 |
windows_proof_rm(stray_file) |
| 281 |
out = self.init(tdir, inprocess=True) |
| 282 |
self.assertRegex(out, r"Neither a subproject directory nor a .*nosubproj.wrap.* file was found") |
| 283 |
self.assertRegex(out, r'Function does not take positional arguments.') |
| 284 |
self.assertRegex(out, r'Dependency .*somenotfounddep.* from subproject .*subprojects/somesubproj.* found: .*NO.*') |
| 285 |
self.assertRegex(out, r'Dependency .*zlibproxy.* from subproject .*subprojects.*somesubproj.* found: .*YES.*') |
| 286 |
self.assertRegex(out, r'Missing key .*source_filename.* in subsubproject.wrap') |
| 287 |
windows_proof_rm(stray_file) |
| 288 |
|
| 289 |
def test_exception_exit_status(self): |
| 290 |
''' |
| 291 |
Test exit status on python exception |
| 292 |
''' |
| 293 |
tdir = os.path.join(self.unit_test_dir, '21 exit status') |
| 294 |
with self.assertRaises(subprocess.CalledProcessError) as cm: |
| 295 |
self.init(tdir, inprocess=False, override_envvars = {'MESON_UNIT_TEST': '1', 'MESON_FORCE_BACKTRACE': ''}) |
| 296 |
self.assertEqual(cm.exception.returncode, 2) |
| 297 |
self.wipe() |
| 298 |
|
| 299 |
def test_dict_requires_key_value_pairs(self): |
| 300 |
self.assertMesonRaises("dict = {3, 'foo': 'bar'}", |
| 301 |
'Only key:value pairs are valid in dict construction.') |
| 302 |
self.assertMesonRaises("{'foo': 'bar', 3}", |
| 303 |
'Only key:value pairs are valid in dict construction.') |
| 304 |
|
| 305 |
def test_dict_forbids_duplicate_keys(self): |
| 306 |
self.assertMesonRaises("dict = {'a': 41, 'a': 42}", |
| 307 |
'Duplicate dictionary key: a.*') |
| 308 |
|
| 309 |
def test_dict_forbids_integer_key(self): |
| 310 |
self.assertMesonRaises("dict = {3: 'foo'}", |
| 311 |
'Key must be a string.*') |
| 312 |
|
| 313 |
def test_using_too_recent_feature(self): |
| 314 |
# Here we use a dict, which was introduced in 0.47.0 |
| 315 |
self.assertMesonOutputs("dict = {}", |
| 316 |
".*WARNING.*Project targets.*but.*", |
| 317 |
meson_version='>= 0.46.0') |
| 318 |
|
| 319 |
def test_using_recent_feature(self): |
| 320 |
# Same as above, except the meson version is now appropriate |
| 321 |
self.assertMesonDoesNotOutput("dict = {}", |
| 322 |
".*WARNING.*Project targets.*but.*", |
| 323 |
meson_version='>= 0.47') |
| 324 |
|
| 325 |
def test_using_too_recent_feature_dependency(self): |
| 326 |
self.assertMesonOutputs("dependency('pcap', required: false)", |
| 327 |
".*WARNING.*Project targets.*but.*", |
| 328 |
meson_version='>= 0.41.0') |
| 329 |
|
| 330 |
def test_vcs_tag_featurenew_build_always_stale(self): |
| 331 |
'https://github.com/mesonbuild/meson/issues/3904' |
| 332 |
vcs_tag = '''version_data = configuration_data() |
| 333 |
version_data.set('PROJVER', '@VCS_TAG@') |
| 334 |
vf = configure_file(output : 'version.h.in', configuration: version_data) |
| 335 |
f = vcs_tag(input : vf, output : 'version.h') |
| 336 |
''' |
| 337 |
msg = '.*WARNING:.*feature.*build_always_stale.*custom_target.*' |
| 338 |
self.assertMesonDoesNotOutput(vcs_tag, msg, meson_version='>=0.43') |
| 339 |
|
| 340 |
def test_missing_subproject_not_required_and_required(self): |
| 341 |
self.assertMesonRaises("sub1 = subproject('not-found-subproject', required: false)\n" + |
| 342 |
"sub2 = subproject('not-found-subproject', required: true)", |
| 343 |
""".*Subproject "subprojects/not-found-subproject" required but not found.*""") |
| 344 |
|
| 345 |
def test_get_variable_on_not_found_project(self): |
| 346 |
self.assertMesonRaises("sub1 = subproject('not-found-subproject', required: false)\n" + |
| 347 |
"sub1.get_variable('naaa')", |
| 348 |
"""Subproject "subprojects/not-found-subproject" disabled can't get_variable on it.""") |
| 349 |
|
| 350 |
def test_version_checked_before_parsing_options(self): |
| 351 |
''' |
| 352 |
https://github.com/mesonbuild/meson/issues/5281 |
| 353 |
''' |
| 354 |
options = "option('some-option', type: 'foo', value: '')" |
| 355 |
match = 'Meson version is.*but project requires >=2000' |
| 356 |
self.assertMesonRaises("", match, meson_version='>=2000', options=options) |
| 357 |
|
| 358 |
def test_assert_default_message(self): |
| 359 |
self.assertMesonRaises("k1 = 'a'\n" + |
| 360 |
"assert({\n" + |
| 361 |
" k1: 1,\n" + |
| 362 |
"}['a'] == 2)\n", |
| 363 |
r"Assert failed: {k1 : 1}\['a'\] == 2") |
| 364 |
|
| 365 |
def test_wrap_nofallback(self): |
| 366 |
self.assertMesonRaises("dependency('notfound', fallback : ['foo', 'foo_dep'])", |
| 367 |
r"Dependency 'notfound' is required but not found.", |
| 368 |
extra_args=['--wrap-mode=nofallback']) |
| 369 |
|
| 370 |
def test_message(self): |
| 371 |
self.assertMesonOutputs("message('Array:', ['a', 'b'])", |
| 372 |
r"Message:.* Array: \['a', 'b'\]") |
| 373 |
|
| 374 |
def test_warning(self): |
| 375 |
self.assertMesonOutputs("warning('Array:', ['a', 'b'])", |
| 376 |
r"WARNING:.* Array: \['a', 'b'\]") |
| 377 |
|
| 378 |
def test_override_dependency_twice(self): |
| 379 |
self.assertMesonRaises("meson.override_dependency('foo', declare_dependency())\n" + |
| 380 |
"meson.override_dependency('foo', declare_dependency())", |
| 381 |
"""Tried to override dependency 'foo' which has already been resolved or overridden""") |
| 382 |
|
| 383 |
@unittest.skipIf(is_windows(), 'zlib is not available on Windows') |
| 384 |
def test_override_resolved_dependency(self): |
| 385 |
self.assertMesonRaises("dependency('zlib')\n" + |
| 386 |
"meson.override_dependency('zlib', declare_dependency())", |
| 387 |
"""Tried to override dependency 'zlib' which has already been resolved or overridden""") |
| 388 |
|
| 389 |
def test_error_func(self): |
| 390 |
self.assertMesonRaises("error('a', 'b', ['c', ['d', {'e': 'f'}]], 'g')", |
| 391 |
r"Problem encountered: a b \['c', \['d', {'e' : 'f'}\]\] g") |
| 392 |
|
| 393 |
def test_compiler_cache_without_compiler(self): |
| 394 |
self.assertMesonRaises('', |
| 395 |
'Compiler cache specified without compiler: ccache', |
| 396 |
override_envvars={'CC': 'ccache'}) |