run_unittests.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env python3
  2. # Copyright 2016 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. import unittest, os, sys, shutil, time
  13. import subprocess
  14. import re, json
  15. import tempfile
  16. import mesonbuild.environment
  17. from mesonbuild.environment import detect_ninja
  18. from mesonbuild.dependencies import PkgConfigDependency
  19. def get_soname(fname):
  20. # HACK, fix to not use shell.
  21. raw_out = subprocess.check_output(['readelf', '-a', fname])
  22. pattern = re.compile(b'soname: \[(.*?)\]')
  23. for line in raw_out.split(b'\n'):
  24. m = pattern.search(line)
  25. if m is not None:
  26. return m.group(1)
  27. class FakeEnvironment(object):
  28. def __init__(self):
  29. self.cross_info = None
  30. def is_cross_build(self):
  31. return False
  32. class InternalTests(unittest.TestCase):
  33. def test_version_number(self):
  34. searchfunc = mesonbuild.environment.search_version
  35. self.assertEqual(searchfunc('foobar 1.2.3'), '1.2.3')
  36. self.assertEqual(searchfunc('1.2.3'), '1.2.3')
  37. self.assertEqual(searchfunc('foobar 2016.10.28 1.2.3'), '1.2.3')
  38. self.assertEqual(searchfunc('2016.10.28 1.2.3'), '1.2.3')
  39. self.assertEqual(searchfunc('foobar 2016.10.128'), 'unknown version')
  40. self.assertEqual(searchfunc('2016.10.128'), 'unknown version')
  41. class LinuxlikeTests(unittest.TestCase):
  42. def setUp(self):
  43. super().setUp()
  44. src_root = os.path.dirname(__file__)
  45. self.builddir = tempfile.mkdtemp()
  46. self.meson_command = [sys.executable, os.path.join(src_root, 'meson.py')]
  47. self.mconf_command = [sys.executable, os.path.join(src_root, 'mesonconf.py')]
  48. self.mintro_command = [sys.executable, os.path.join(src_root, 'mesonintrospect.py')]
  49. self.ninja_command = [detect_ninja(), '-C', self.builddir]
  50. self.common_test_dir = os.path.join(src_root, 'test cases/common')
  51. self.vala_test_dir = os.path.join(src_root, 'test cases/vala')
  52. self.output = b''
  53. self.orig_env = os.environ.copy()
  54. def tearDown(self):
  55. shutil.rmtree(self.builddir)
  56. os.environ = self.orig_env
  57. super().tearDown()
  58. def init(self, srcdir):
  59. self.output += subprocess.check_output(self.meson_command + [srcdir, self.builddir])
  60. def build(self):
  61. self.output += subprocess.check_output(self.ninja_command)
  62. def run_target(self, target):
  63. self.output += subprocess.check_output(self.ninja_command + [target])
  64. def setconf(self, arg):
  65. self.output += subprocess.check_output(self.mconf_command + [arg, self.builddir])
  66. def get_compdb(self):
  67. with open(os.path.join(self.builddir, 'compile_commands.json')) as ifile:
  68. return json.load(ifile)
  69. def introspect(self, arg):
  70. out = subprocess.check_output(self.mintro_command + [arg, self.builddir])
  71. return json.loads(out.decode('utf-8'))
  72. def test_basic_soname(self):
  73. testdir = os.path.join(self.common_test_dir, '4 shared')
  74. self.init(testdir)
  75. self.build()
  76. lib1 = os.path.join(self.builddir, 'libmylib.so')
  77. soname = get_soname(lib1)
  78. self.assertEqual(soname, b'libmylib.so')
  79. def test_custom_soname(self):
  80. testdir = os.path.join(self.common_test_dir, '27 library versions')
  81. self.init(testdir)
  82. self.build()
  83. lib1 = os.path.join(self.builddir, 'prefixsomelib.suffix')
  84. soname = get_soname(lib1)
  85. self.assertEqual(soname, b'prefixsomelib.suffix')
  86. def test_pic(self):
  87. testdir = os.path.join(self.common_test_dir, '3 static')
  88. self.init(testdir)
  89. compdb = self.get_compdb()
  90. self.assertTrue('-fPIC' in compdb[0]['command'])
  91. # This is needed to increase the difference between build.ninja's
  92. # timestamp and coredata.dat's timestamp due to a Ninja bug.
  93. # https://github.com/ninja-build/ninja/issues/371
  94. time.sleep(1)
  95. self.setconf('-Db_staticpic=false')
  96. # Regenerate build
  97. self.build()
  98. compdb = self.get_compdb()
  99. self.assertTrue('-fPIC' not in compdb[0]['command'])
  100. def test_pkgconfig_gen(self):
  101. testdir = os.path.join(self.common_test_dir, '51 pkgconfig-gen')
  102. self.init(testdir)
  103. env = FakeEnvironment()
  104. kwargs = {'required': True, 'silent': True}
  105. os.environ['PKG_CONFIG_LIBDIR'] = os.path.join(self.builddir, 'meson-private')
  106. simple_dep = PkgConfigDependency('libfoo', env, kwargs)
  107. self.assertTrue(simple_dep.found())
  108. self.assertEqual(simple_dep.get_version(), '1.0')
  109. self.assertTrue('-lfoo' in simple_dep.get_link_args())
  110. def test_vala_c_warnings(self):
  111. testdir = os.path.join(self.vala_test_dir, '5 target glib')
  112. self.init(testdir)
  113. compdb = self.get_compdb()
  114. vala_command = None
  115. c_command = None
  116. for each in compdb:
  117. if each['file'].endswith('GLib.Thread.c'):
  118. vala_command = each['command']
  119. elif each['file'].endswith('retcode.c'):
  120. c_command = each['command']
  121. else:
  122. m = 'Unknown file {!r} in vala_c_warnings test'.format(each['file'])
  123. raise AssertionError(m)
  124. self.assertIsNotNone(vala_command)
  125. self.assertIsNotNone(c_command)
  126. # -w suppresses all warnings, should be there in Vala but not in C
  127. self.assertTrue('-w' in vala_command)
  128. self.assertFalse('-w' in c_command)
  129. # -Wall enables all warnings, should be there in C but not in Vala
  130. self.assertFalse('-Wall' in vala_command)
  131. self.assertTrue('-Wall' in c_command)
  132. # -Werror converts warnings to errors, should always be there since it's
  133. # injected by an unrelated piece of code and the project has werror=true
  134. self.assertTrue('-Werror' in vala_command)
  135. self.assertTrue('-Werror' in c_command)
  136. def test_static_compile_order(self):
  137. testdir = os.path.join(self.common_test_dir, '5 linkstatic')
  138. self.init(testdir)
  139. compdb = self.get_compdb()
  140. # Rules will get written out in this order
  141. self.assertTrue(compdb[0]['file'].endswith("libfile.c"))
  142. self.assertTrue(compdb[1]['file'].endswith("libfile2.c"))
  143. self.assertTrue(compdb[2]['file'].endswith("libfile3.c"))
  144. self.assertTrue(compdb[3]['file'].endswith("libfile4.c"))
  145. # FIXME: We don't have access to the linker command
  146. def test_install_introspection(self):
  147. testdir = os.path.join(self.common_test_dir, '8 install')
  148. self.init(testdir)
  149. intro = self.introspect('--targets')
  150. if intro[0]['type'] == 'executable':
  151. intro = intro[::-1]
  152. self.assertEqual(intro[0]['install_filename'], '/usr/local/libtest/libstat.a')
  153. self.assertEqual(intro[1]['install_filename'], '/usr/local/bin/prog')
  154. def test_run_target_files_path(self):
  155. testdir = os.path.join(self.common_test_dir, '58 run target')
  156. self.init(testdir)
  157. self.run_target('check_exists')
  158. if __name__ == '__main__':
  159. unittest.main()