tests: Factor out common code to run_tests

And use generic build/clean/test/install commands in the unit tests,
just like project tests. This sets the groundwork for running the unit
tests with all backends.
pull/1575/head
Nirbheek Chauhan 8 years ago
parent 4646958917
commit d755228afe
  1. 37
      run_project_tests.py
  2. 54
      run_tests.py
  3. 51
      run_unittests.py

@ -26,6 +26,7 @@ from mesonbuild import mesonlib
from mesonbuild import mlog
from mesonbuild import mesonmain
from mesonbuild.mesonlib import stringlistify, Popen_safe
from mesonbuild.coredata import backendlist
import argparse
import xml.etree.ElementTree as ET
import time
@ -33,7 +34,7 @@ import multiprocessing
import concurrent.futures as conc
import re
from mesonbuild.coredata import backendlist
from run_tests import get_backend_commands
class BuildStep(Enum):
@ -153,42 +154,30 @@ def stop_handler(signal, frame):
signal.signal(signal.SIGINT, stop_handler)
signal.signal(signal.SIGTERM, stop_handler)
# unity_flags = ['--unity']
unity_flags = []
# Must define these here for compatibility with Python 3.4
backend_flags = None
compile_commands = None
clean_commands = []
test_commands = None
install_commands = []
clean_commands = []
uninstall_commands = None
def setup_commands(backend):
global backend_flags, compile_commands, test_commands, install_commands, clean_commands
global do_debug, backend_flags
global compile_commands, clean_commands, test_commands, install_commands, uninstall_commands
msbuild_exe = shutil.which('msbuild')
if (backend and backend.startswith('vs')) or (backend is None and msbuild_exe is not None):
if backend is None:
backend = 'vs2010'
# Autodetect if unspecified
backend = 'vs'
backend_flags = ['--backend=' + backend]
compile_commands = ['msbuild']
test_commands = ['msbuild', 'RUN_TESTS.vcxproj']
elif backend == 'xcode' or (backend is None and mesonlib.is_osx()):
backend_flags = ['--backend=xcode']
compile_commands = ['xcodebuild']
test_commands = ['xcodebuild', '-target', 'RUN_TESTS']
else:
backend = 'ninja'
backend_flags = []
# We need at least 1.6 because of -w dupbuild=err
ninja_command = environment.detect_ninja(version='1.6')
if ninja_command is None:
raise RuntimeError('Could not find Ninja v1.6 or newer')
if do_debug:
compile_commands = [ninja_command, '-v']
else:
compile_commands = [ninja_command]
compile_commands += ['-w', 'dupbuild=err']
test_commands = [ninja_command, 'test', 'benchmark']
install_commands = [ninja_command, 'install']
clean_commands = [ninja_command, 'clean']
compile_commands, clean_commands, test_commands, install_commands, \
uninstall_commands = get_backend_commands(backend, do_debug)
def get_compile_commands_for_dir(compile_commands, test_build_dir):
if 'msbuild' in compile_commands[0]:
@ -502,7 +491,7 @@ def run_tests(all_tests, log_name_base, extra_args):
should_fail = False
if name.startswith('failing'):
should_fail = name.split('failing-')[1]
result = executor.submit(run_test, skipped, t, extra_args, unity_flags + backend_flags, compile_commands, should_fail)
result = executor.submit(run_test, skipped, t, extra_args, backend_flags, compile_commands, should_fail)
futures.append((testname, t, result))
for (testname, t, result) in futures:
sys.stdout.flush()

@ -20,10 +20,64 @@ import shutil
import subprocess
import platform
from mesonbuild import mesonlib
from mesonbuild.environment import detect_ninja
from enum import Enum
Backend = Enum('Backend', 'ninja vs xcode')
if mesonlib.is_windows():
exe_suffix = '.exe'
else:
exe_suffix = ''
def get_build_target_args(backend, target):
if target is None:
return []
if backend.startswith('vs'):
return ['/target:' + target]
if backend == 'xcode':
return ['-target', target]
return [target]
def get_backend_commands(backend, debug=False):
install_cmd = []
uninstall_cmd = []
if backend.startswith('vs'):
cmd = ['msbuild']
clean_cmd = []
test_cmd = cmd + ['RUN_TESTS.vcxproj']
elif backend == 'xcode':
cmd = ['xcodebuild']
clean_cmd = cmd + ['-alltargets', 'clean']
test_cmd = cmd + ['-target', 'RUN_TESTS']
else:
# We need at least 1.6 because of -w dupbuild=err
cmd = [detect_ninja('1.6'), '-w', 'dupbuild=err']
if cmd[0] is None:
raise RuntimeError('Could not find Ninja v1.6 or newer')
if debug:
cmd += ['-v']
clean_cmd = cmd + ['clean']
test_cmd = cmd + ['test', 'benchmark']
install_cmd = cmd + ['install']
uninstall_cmd = cmd + ['uninstall']
return cmd, clean_cmd, test_cmd, install_cmd, uninstall_cmd
def get_fake_options(prefix):
import argparse
opts = argparse.Namespace()
opts.cross_file = None
opts.wrap_mode = None
opts.prefix = prefix
return opts
class FakeEnvironment(object):
def __init__(self):
self.cross_info = None
def is_cross_build(self):
return False
if __name__ == '__main__':
returncode = 0
# Iterate over list in reverse order to find the last --backend arg

@ -25,13 +25,12 @@ import mesonbuild.compilers
import mesonbuild.environment
import mesonbuild.mesonlib
from mesonbuild.mesonlib import is_windows, is_osx, is_cygwin
from mesonbuild.environment import detect_ninja, Environment
from mesonbuild.environment import Environment
from mesonbuild.dependencies import PkgConfigDependency, ExternalProgram
if is_windows() or is_cygwin():
exe_suffix = '.exe'
else:
exe_suffix = ''
from run_tests import exe_suffix, get_fake_options, FakeEnvironment
from run_tests import get_build_target_args, get_backend_commands
def get_soname(fname):
# HACK, fix to not use shell.
@ -44,21 +43,6 @@ def get_soname(fname):
return m.group(1)
raise RuntimeError('Could not determine soname:\n\n' + raw_out)
def get_fake_options(prefix):
import argparse
opts = argparse.Namespace()
opts.cross_file = None
opts.wrap_mode = None
opts.prefix = prefix
return opts
class FakeEnvironment(object):
def __init__(self):
self.cross_info = None
def is_cross_build(self):
return False
class InternalTests(unittest.TestCase):
def test_version_number(self):
@ -350,7 +334,11 @@ class BasePlatformTests(unittest.TestCase):
self.mconf_command = [sys.executable, os.path.join(src_root, 'mesonconf.py')]
self.mintro_command = [sys.executable, os.path.join(src_root, 'mesonintrospect.py')]
self.mtest_command = [sys.executable, os.path.join(src_root, 'mesontest.py'), '-C', self.builddir]
self.ninja_command = [detect_ninja(), '-C', self.builddir]
# Backend-specific commands
self.backend = os.environ.get('MESON_UNIT_TEST_BACKEND', 'ninja')
self.build_command, self.clean_command, self.test_command, self.install_command, \
self.uninstall_command = get_backend_commands(self.backend)
# Test directories
self.common_test_dir = os.path.join(src_root, 'test cases/common')
self.vala_test_dir = os.path.join(src_root, 'test cases/vala')
self.framework_test_dir = os.path.join(src_root, 'test cases/frameworks')
@ -370,14 +358,14 @@ class BasePlatformTests(unittest.TestCase):
os.environ = self.orig_env
super().tearDown()
def _run(self, command):
def _run(self, command, workdir=None):
'''
Run a command while printing the stdout and stderr to stdout,
and also return a copy of it
'''
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=os.environ.copy(),
universal_newlines=True)
universal_newlines=True, cwd=workdir)
output = p.communicate()[0]
print(output)
if p.returncode != 0:
@ -398,27 +386,28 @@ class BasePlatformTests(unittest.TestCase):
raise
self.privatedir = os.path.join(self.builddir, 'meson-private')
def build(self, extra_args=None):
def build(self, target=None, extra_args=None):
if extra_args is None:
extra_args = []
self._run(self.ninja_command + extra_args)
target = get_build_target_args(self.backend, target)
self._run(self.build_command + target + extra_args, workdir=self.builddir)
def run_tests(self):
self._run(self.ninja_command + ['test'])
self._run(self.test_command, workdir=self.builddir)
def install(self):
os.environ['DESTDIR'] = self.installdir
self._run(self.ninja_command + ['install'])
self._run(self.install_command, workdir=self.builddir)
def uninstall(self):
self._run(self.ninja_command + ['uninstall'])
self._run(self.uninstall_command, workdir=self.builddir)
def run_target(self, target):
'''
Run a Ninja target while printing the stdout and stderr to stdout,
and also return a copy of it
'''
return self._run(self.ninja_command + [target])
return self.build(target=target)
def setconf(self, arg, will_build=True):
# This is needed to increase the difference between build.ninja's
@ -438,7 +427,7 @@ class BasePlatformTests(unittest.TestCase):
# replace it as the command for all compile commands in the parsed json.
if len(contents) > 0 and contents[0]['command'].endswith('.rsp'):
# Pretend to build so that the rsp files are generated
self.build(['-d', 'keeprsp', '-n'])
self.build(extra_args=['-d', 'keeprsp', '-n'])
for each in contents:
# Extract the actual command from the rsp file
compiler, rsp = each['command'].split(' @')
@ -722,7 +711,7 @@ class AllPlatformTests(BasePlatformTests):
exe = os.path.join(self.builddir, 'fooprog' + exe_suffix)
self.assertTrue(os.path.exists(genfile))
self.assertFalse(os.path.exists(exe))
self._run(self.ninja_command + ['fooprog' + exe_suffix])
self.build(target=('fooprog' + exe_suffix))
self.assertTrue(os.path.exists(exe))
def test_internal_include_order(self):

Loading…
Cancel
Save