run_meson_command_tests.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. # Copyright 2018 The Meson development team
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import tempfile
  17. import unittest
  18. import subprocess
  19. from pathlib import Path
  20. from mesonbuild.mesonlib import windows_proof_rmtree, python_command, is_windows
  21. # Find the meson.py adjacent to us
  22. meson_py = Path(__file__).resolve().parent / 'meson.py'
  23. if not meson_py.is_file():
  24. raise RuntimeError("meson.py not found: test must only run from git")
  25. def get_pypath():
  26. import sysconfig
  27. pypath = sysconfig.get_path('purelib', vars={'base': ''})
  28. # Ensure that / is the path separator and not \, then strip /
  29. return Path(pypath).as_posix().strip('/')
  30. def get_pybindir():
  31. import sysconfig
  32. # 'Scripts' on Windows and 'bin' on other platforms including MSYS
  33. return sysconfig.get_path('scripts', vars={'base': ''}).strip('\\/')
  34. class CommandTests(unittest.TestCase):
  35. '''
  36. Test that running meson in various ways works as expected by checking the
  37. value of mesonlib.meson_command that was set during configuration.
  38. '''
  39. def setUp(self):
  40. super().setUp()
  41. self.orig_env = os.environ.copy()
  42. self.orig_dir = os.getcwd()
  43. os.environ['MESON_COMMAND_TESTS'] = '1'
  44. self.tmpdir = Path(tempfile.mkdtemp()).resolve()
  45. self.src_root = Path(__file__).resolve().parent
  46. self.testdir = str(self.src_root / 'test cases/common/1 trivial')
  47. self.meson_args = ['--backend=ninja']
  48. def tearDown(self):
  49. try:
  50. windows_proof_rmtree(str(self.tmpdir))
  51. except FileNotFoundError:
  52. pass
  53. os.environ.clear()
  54. os.environ.update(self.orig_env)
  55. os.chdir(str(self.orig_dir))
  56. super().tearDown()
  57. def _run(self, command, workdir=None):
  58. '''
  59. Run a command while printing the stdout and stderr to stdout,
  60. and also return a copy of it
  61. '''
  62. # If this call hangs CI will just abort. It is very hard to distinguish
  63. # between CI issue and test bug in that case. Set timeout and fail loud
  64. # instead.
  65. p = subprocess.run(command, stdout=subprocess.PIPE,
  66. stderr=subprocess.STDOUT, env=os.environ.copy(),
  67. universal_newlines=True, cwd=workdir, timeout=60 * 5)
  68. print(p.stdout)
  69. if p.returncode != 0:
  70. raise subprocess.CalledProcessError(p.returncode, command)
  71. return p.stdout
  72. def assertMesonCommandIs(self, line, cmd):
  73. self.assertTrue(line.startswith('meson_command '), msg=line)
  74. self.assertEqual(line, 'meson_command is {!r}'.format(cmd))
  75. def test_meson_uninstalled(self):
  76. # This is what the meson command must be for all these cases
  77. resolved_meson_command = python_command + [str(self.src_root / 'meson.py')]
  78. # Absolute path to meson.py
  79. os.chdir('/')
  80. builddir = str(self.tmpdir / 'build1')
  81. meson_py = str(self.src_root / 'meson.py')
  82. meson_setup = [meson_py, 'setup']
  83. meson_command = python_command + meson_setup + self.meson_args
  84. stdo = self._run(meson_command + [self.testdir, builddir])
  85. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  86. # ./meson.py
  87. os.chdir(str(self.src_root))
  88. builddir = str(self.tmpdir / 'build2')
  89. meson_py = './meson.py'
  90. meson_setup = [meson_py, 'setup']
  91. meson_command = python_command + meson_setup + self.meson_args
  92. stdo = self._run(meson_command + [self.testdir, builddir])
  93. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  94. # Symlink to meson.py
  95. if is_windows():
  96. # Symlinks require admin perms
  97. return
  98. os.chdir(str(self.src_root))
  99. builddir = str(self.tmpdir / 'build3')
  100. # Create a symlink to meson.py in bindir, and add it to PATH
  101. bindir = (self.tmpdir / 'bin')
  102. bindir.mkdir()
  103. (bindir / 'meson').symlink_to(self.src_root / 'meson.py')
  104. os.environ['PATH'] = str(bindir) + os.pathsep + os.environ['PATH']
  105. # See if it works!
  106. meson_py = 'meson'
  107. meson_setup = [meson_py, 'setup']
  108. meson_command = meson_setup + self.meson_args
  109. stdo = self._run(meson_command + [self.testdir, builddir])
  110. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  111. def test_meson_installed(self):
  112. # Install meson
  113. prefix = self.tmpdir / 'prefix'
  114. pylibdir = prefix / get_pypath()
  115. bindir = prefix / get_pybindir()
  116. pylibdir.mkdir(parents=True)
  117. os.environ['PYTHONPATH'] = str(pylibdir)
  118. os.environ['PATH'] = str(bindir) + os.pathsep + os.environ['PATH']
  119. self._run(python_command + ['setup.py', 'install', '--prefix', str(prefix)])
  120. self.assertTrue(pylibdir.is_dir())
  121. self.assertTrue(bindir.is_dir())
  122. # Run `meson`
  123. os.chdir('/')
  124. if is_windows():
  125. resolved_meson_command = python_command + [str(bindir / 'meson.py')]
  126. else:
  127. resolved_meson_command = python_command + [str(bindir / 'meson')]
  128. # The python configuration on appveyor does not register .py as
  129. # a valid extension, so we cannot run `meson` on Windows.
  130. builddir = str(self.tmpdir / 'build1')
  131. meson_setup = ['meson', 'setup']
  132. meson_command = meson_setup + self.meson_args
  133. stdo = self._run(meson_command + [self.testdir, builddir])
  134. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  135. # Run `/path/to/meson`
  136. builddir = str(self.tmpdir / 'build2')
  137. if is_windows():
  138. # Cannot run .py directly because of the appveyor configuration,
  139. # and the script is named meson.py, not meson
  140. meson_setup = python_command + [str(bindir / 'meson.py'), 'setup']
  141. else:
  142. meson_setup = [str(bindir / 'meson'), 'setup']
  143. meson_command = meson_setup + self.meson_args
  144. stdo = self._run(meson_command + [self.testdir, builddir])
  145. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  146. # Run `python3 -m mesonbuild.mesonmain`
  147. resolved_meson_command = python_command + ['-m', 'mesonbuild.mesonmain']
  148. builddir = str(self.tmpdir / 'build3')
  149. meson_setup = ['-m', 'mesonbuild.mesonmain', 'setup']
  150. meson_command = python_command + meson_setup + self.meson_args
  151. stdo = self._run(meson_command + [self.testdir, builddir])
  152. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  153. if is_windows():
  154. # Next part requires a shell
  155. return
  156. # `meson` is a wrapper to `meson.real`
  157. resolved_meson_command = python_command + [str(bindir / 'meson.real')]
  158. builddir = str(self.tmpdir / 'build4')
  159. (bindir / 'meson').rename(bindir / 'meson.real')
  160. wrapper = (bindir / 'meson')
  161. with open(wrapper, 'w') as f:
  162. f.write('#!/bin/sh\n\nmeson.real "$@"')
  163. wrapper.chmod(0o755)
  164. meson_setup = [str(wrapper), 'setup']
  165. meson_command = meson_setup + self.meson_args
  166. stdo = self._run(meson_command + [self.testdir, builddir])
  167. self.assertMesonCommandIs(stdo.split('\n')[0], resolved_meson_command)
  168. def test_meson_exe_windows(self):
  169. raise unittest.SkipTest('NOT IMPLEMENTED')
  170. if __name__ == '__main__':
  171. unittest.main(buffer=True)