| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
# Copyright 2018 The Meson development team |
| 4 |
|
| 5 |
import os |
| 6 |
import tempfile |
| 7 |
import unittest |
| 8 |
import subprocess |
| 9 |
import zipapp |
| 10 |
import sysconfig |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
from mesonbuild.mesonlib import windows_proof_rmtree, python_command, is_windows |
| 14 |
from mesonbuild.coredata import version as meson_version |
| 15 |
|
| 16 |
scheme = None |
| 17 |
|
| 18 |
def needs_debian_path_hack(): |
| 19 |
try: |
| 20 |
import setuptools |
| 21 |
return int(setuptools.__version__.split('.')[0]) < 65 |
| 22 |
except ModuleNotFoundError: |
| 23 |
return False |
| 24 |
|
| 25 |
if needs_debian_path_hack(): |
| 26 |
# Handle the scheme that Debian patches in the as default |
| 27 |
# This function was renamed and made public in Python 3.10 |
| 28 |
if hasattr(sysconfig, 'get_default_scheme'): |
| 29 |
scheme = sysconfig.get_default_scheme() |
| 30 |
else: |
| 31 |
scheme = sysconfig._get_default_scheme() |
| 32 |
if scheme == 'posix_local': |
| 33 |
scheme = 'posix_prefix' |
| 34 |
|
| 35 |
def get_pypath(): |
| 36 |
if scheme: |
| 37 |
pypath = sysconfig.get_path('purelib', scheme=scheme, vars={'base': ''}) |
| 38 |
else: |
| 39 |
pypath = sysconfig.get_path('purelib', vars={'base': ''}) |
| 40 |
# Ensure that / is the path separator and not \, then strip / |
| 41 |
return Path(pypath).as_posix().strip('/') |
| 42 |
|
| 43 |
def get_pybindir(): |
| 44 |
# 'Scripts' on Windows and 'bin' on other platforms including MSYS |
| 45 |
if scheme: |
| 46 |
return sysconfig.get_path('scripts', scheme=scheme, vars={'base': ''}).strip('\\/') |
| 47 |
return sysconfig.get_path('scripts', vars={'base': ''}).strip('\\/') |
| 48 |
|
| 49 |
def has_python_module(module: str) -> bool: |
| 50 |
result = subprocess.run(python_command + ['-c', f'import {module}']) |
| 51 |
return result.returncode == 0 |
| 52 |
|
| 53 |
|
| 54 |
class CommandTests(unittest.TestCase): |
| 55 |
''' |
| 56 |
Test that running meson in various ways works as expected by checking the |
| 57 |
value of mesonlib.meson_command that was set during configuration. |
| 58 |
''' |
| 59 |
|
| 60 |
def setUp(self): |
| 61 |
super().setUp() |
| 62 |
self.orig_env = os.environ.copy() |
| 63 |
self.orig_dir = os.getcwd() |
| 64 |
os.environ['MESON_COMMAND_TESTS'] = '1' |
| 65 |
self.tmpdir = Path(tempfile.mkdtemp()).resolve() |
| 66 |
self.src_root = Path(__file__).resolve().parent |
| 67 |
self.testdir = str(self.src_root / 'test cases/common/1 trivial') |
| 68 |
self.meson_args = ['--backend=ninja'] |
| 69 |
|
| 70 |
def tearDown(self): |
| 71 |
try: |
| 72 |
windows_proof_rmtree(str(self.tmpdir)) |
| 73 |
except FileNotFoundError: |
| 74 |
pass |
| 75 |
os.environ.clear() |
| 76 |
os.environ.update(self.orig_env) |
| 77 |
os.chdir(str(self.orig_dir)) |
| 78 |
super().tearDown() |
| 79 |
|
| 80 |
def _run(self, command, workdir=None, env=None): |
| 81 |
''' |
| 82 |
Run a command while printing the stdout, and also return a copy of it |
| 83 |
''' |
| 84 |
# If this call hangs CI will just abort. It is very hard to distinguish |
| 85 |
# between CI issue and test bug in that case. Set timeout and fail loud |
| 86 |
# instead. |
| 87 |
p = subprocess.run(command, |
| 88 |
stdout=subprocess.PIPE, |
| 89 |
env=env, |
| 90 |
encoding='utf-8', |
| 91 |
text=True, |
| 92 |
cwd=workdir, |
| 93 |
timeout=60 * 5) |
| 94 |
print(p.stdout) |
| 95 |
if p.returncode != 0: |
| 96 |
raise subprocess.CalledProcessError(p.returncode, command) |
| 97 |
return p.stdout |
| 98 |
|
| 99 |
def assertMesonCommandIs(self, line, cmd): |
| 100 |
self.assertTrue(line.startswith('meson_command '), msg=line) |
| 101 |
self.assertEqual(line, f'meson_command is {cmd!r}') |
| 102 |
|
| 103 |
def test_meson_uninstalled(self): |
| 104 |
# This is what the meson command must be for all these cases |
| 105 |
resolved_meson_command = python_command + [str(self.src_root / 'meson.py')] |
| 106 |
# Absolute path to meson.py |
| 107 |
os.chdir('/') |
| 108 |
builddir = str(self.tmpdir / 'build1') |
| 109 |
meson_py = str(self.src_root / 'meson.py') |
| 110 |
meson_setup = [meson_py, 'setup'] |
| 111 |
meson_command = python_command + meson_setup + self.meson_args |
| 112 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 113 |
self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command) |
| 114 |
# ./meson.py |
| 115 |
os.chdir(str(self.src_root)) |
| 116 |
builddir = str(self.tmpdir / 'build2') |
| 117 |
meson_py = './meson.py' |
| 118 |
meson_setup = [meson_py, 'setup'] |
| 119 |
meson_command = python_command + meson_setup + self.meson_args |
| 120 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 121 |
self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command) |
| 122 |
# Symlink to meson.py |
| 123 |
if is_windows(): |
| 124 |
# Symlinks require admin perms |
| 125 |
return |
| 126 |
os.chdir(str(self.src_root)) |
| 127 |
builddir = str(self.tmpdir / 'build3') |
| 128 |
# Create a symlink to meson.py in bindir, and add it to PATH |
| 129 |
bindir = (self.tmpdir / 'bin') |
| 130 |
bindir.mkdir() |
| 131 |
(bindir / 'meson').symlink_to(self.src_root / 'meson.py') |
| 132 |
(bindir / 'python3').symlink_to(python_command[0]) |
| 133 |
os.environ['PATH'] = str(bindir) + os.pathsep + os.environ['PATH'] |
| 134 |
# use our overridden PATH-compatible python |
| 135 |
path_resolved_meson_command = [str(bindir / 'meson')] |
| 136 |
# See if it works! |
| 137 |
meson_py = 'meson' |
| 138 |
meson_setup = [meson_py, 'setup'] |
| 139 |
meson_command = meson_setup + self.meson_args |
| 140 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 141 |
self.assertMesonCommandIs(stdo.split('\n')[0], path_resolved_meson_command) |
| 142 |
|
| 143 |
def test_meson_installed(self): |
| 144 |
# Install meson |
| 145 |
prefix = self.tmpdir / 'prefix' |
| 146 |
pylibdir = prefix / get_pypath() |
| 147 |
bindir = prefix / get_pybindir() |
| 148 |
pylibdir.mkdir(parents=True) |
| 149 |
# XXX: join with empty name so it always ends with os.sep otherwise |
| 150 |
# distutils complains that prefix isn't contained in PYTHONPATH |
| 151 |
os.environ['PYTHONPATH'] = os.path.join(str(pylibdir), '') |
| 152 |
os.environ['PATH'] = str(bindir) + os.pathsep + os.environ['PATH'] |
| 153 |
if has_python_module('gpep517'): |
| 154 |
self._run(python_command + ['-m', 'gpep517', 'install-from-source', '--destdir', '/', '--prefix', str(prefix)]) |
| 155 |
elif has_python_module('pip'): |
| 156 |
self._run(python_command + ['-m', 'pip', 'install', '--prefix', str(prefix), '.']) |
| 157 |
else: |
| 158 |
# Legacy deprecated setuptools command used as fallback |
| 159 |
self._run(python_command + ['setup.py', 'install', '--prefix', str(prefix)]) |
| 160 |
# Fix importlib-metadata by appending all dirs in pylibdir |
| 161 |
PYTHONPATHS = [pylibdir] + [x for x in pylibdir.iterdir() if x.name.endswith('.egg')] |
| 162 |
PYTHONPATHS = [os.path.join(str(x), '') for x in PYTHONPATHS] |
| 163 |
os.environ['PYTHONPATH'] = os.pathsep.join(PYTHONPATHS) |
| 164 |
# Check that all the files were installed correctly |
| 165 |
self.assertTrue(bindir.is_dir()) |
| 166 |
self.assertTrue(pylibdir.is_dir()) |
| 167 |
# Run `meson` |
| 168 |
os.chdir('/') |
| 169 |
resolved_meson_command = [str(bindir / 'meson')] |
| 170 |
builddir = str(self.tmpdir / 'build1') |
| 171 |
meson_setup = ['meson', 'setup'] |
| 172 |
meson_command = meson_setup + self.meson_args |
| 173 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 174 |
self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command) |
| 175 |
# Run `/path/to/meson` |
| 176 |
builddir = str(self.tmpdir / 'build2') |
| 177 |
meson_setup = [str(bindir / 'meson'), 'setup'] |
| 178 |
meson_command = meson_setup + self.meson_args |
| 179 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 180 |
self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command) |
| 181 |
# Run `python3 -m mesonbuild.mesonmain` |
| 182 |
resolved_meson_command = python_command + ['-m', 'mesonbuild.mesonmain'] |
| 183 |
builddir = str(self.tmpdir / 'build3') |
| 184 |
meson_setup = ['-m', 'mesonbuild.mesonmain', 'setup'] |
| 185 |
meson_command = python_command + meson_setup + self.meson_args |
| 186 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 187 |
self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command) |
| 188 |
if is_windows(): |
| 189 |
# Next part requires a shell |
| 190 |
return |
| 191 |
# `meson` is a wrapper to `meson.real` |
| 192 |
resolved_meson_command = [str(bindir / 'meson.real')] |
| 193 |
builddir = str(self.tmpdir / 'build4') |
| 194 |
(bindir / 'meson').rename(bindir / 'meson.real') |
| 195 |
wrapper = (bindir / 'meson') |
| 196 |
wrapper.write_text('#!/bin/sh\n\nmeson.real "$@"', encoding='utf-8') |
| 197 |
wrapper.chmod(0o755) |
| 198 |
meson_setup = [str(wrapper), 'setup'] |
| 199 |
meson_command = meson_setup + self.meson_args |
| 200 |
stdo = self._run(meson_command + [self.testdir, builddir]) |
| 201 |
self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command) |
| 202 |
|
| 203 |
def test_meson_exe_windows(self): |
| 204 |
raise unittest.SkipTest('NOT IMPLEMENTED') |
| 205 |
|
| 206 |
def test_meson_zipapp(self): |
| 207 |
if is_windows(): |
| 208 |
raise unittest.SkipTest('NOT IMPLEMENTED') |
| 209 |
source = Path(__file__).resolve().parent |
| 210 |
target = self.tmpdir / 'meson.pyz' |
| 211 |
script = source / 'packaging' / 'create_zipapp.py' |
| 212 |
self._run([script.as_posix(), source, '--outfile', target, '--interpreter', python_command[0]]) |
| 213 |
self._run([target.as_posix(), '--help']) |
| 214 |
|
| 215 |
def test_meson_runpython(self): |
| 216 |
meson_command = str(self.src_root / 'meson.py') |
| 217 |
script_file = str(self.src_root / 'foo.py') |
| 218 |
test_command = 'import sys; print(sys.argv[1])' |
| 219 |
env = os.environ.copy() |
| 220 |
del env['MESON_COMMAND_TESTS'] |
| 221 |
with open(script_file, 'w', encoding='utf-8') as f: |
| 222 |
f.write('#!/usr/bin/env python3\n\n') |
| 223 |
f.write(f'{test_command}\n') |
| 224 |
self.addCleanup(os.remove, script_file) |
| 225 |
|
| 226 |
for cmd in [['-c', test_command, 'fake argument'], [script_file, 'fake argument']]: |
| 227 |
pyout = self._run(python_command + cmd) |
| 228 |
mesonout = self._run(python_command + [meson_command, 'runpython'] + cmd, env=env) |
| 229 |
self.assertEqual(pyout, mesonout) |
| 230 |
|
| 231 |
if __name__ == '__main__': |
| 232 |
print('Meson build system', meson_version, 'Command Tests') |
| 233 |
raise SystemExit(unittest.main(buffer=True)) |