diff options
author | Niklas Claesson <nicke.claesson@gmail.com> | 2018-10-10 21:17:24 +0200 |
---|---|---|
committer | Niklas Claesson <nicke.claesson@gmail.com> | 2018-10-10 21:19:06 +0200 |
commit | 4ef4edee2f460a354cca0c381d08604f6837c496 (patch) | |
tree | 1eb5308b3beb94e1e2d4e7416fe9ce9a6f1acc8d | |
parent | a0a0c244e2a062ceba357f05d4852cd063f0b0cd (diff) | |
download | meson-4ef4edee2f460a354cca0c381d08604f6837c496.zip meson-4ef4edee2f460a354cca0c381d08604f6837c496.tar.gz meson-4ef4edee2f460a354cca0c381d08604f6837c496.tar.bz2 |
tests runners: Refactor out global variables and add argparse
-rwxr-xr-x | run_cross_test.py | 18 | ||||
-rwxr-xr-x | run_meson_command_tests.py | 12 | ||||
-rwxr-xr-x | run_tests.py | 92 | ||||
-rwxr-xr-x | run_unittests.py | 11 |
4 files changed, 78 insertions, 55 deletions
diff --git a/run_cross_test.py b/run_cross_test.py index 7191402..60be8a2 100755 --- a/run_cross_test.py +++ b/run_cross_test.py @@ -25,6 +25,7 @@ Eventually migrate to something fancier.''' import sys import os from pathlib import Path +import argparse from run_project_tests import gather_tests, run_tests, StopException, setup_commands from run_project_tests import failing_logs @@ -40,11 +41,16 @@ def runtests(cross_file): print('Total skipped cross tests:', skipped_tests) if failing_tests > 0 and ('TRAVIS' in os.environ or 'APPVEYOR' in os.environ): print('\nMesonlogs of failing tests\n') - for l in failing_logs: - print(l, '\n') - sys.exit(failing_tests) + for log in failing_logs: + print(log, '\n') + return failing_tests + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('cross_file') + options = parser.parse_args() + setup_commands('ninja') + return runtests(options.cross_file) if __name__ == '__main__': - setup_commands('ninja') - cross_file = sys.argv[1] - runtests(cross_file) + sys.exit(main()) diff --git a/run_meson_command_tests.py b/run_meson_command_tests.py index fd33856..390868a 100755 --- a/run_meson_command_tests.py +++ b/run_meson_command_tests.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys import os import tempfile import unittest @@ -23,11 +24,6 @@ from pathlib import Path from mesonbuild.mesonlib import windows_proof_rmtree, python_command, is_windows -# Find the meson.py adjacent to us -meson_py = Path(__file__).resolve().parent / 'meson.py' -if not meson_py.is_file(): - raise RuntimeError("meson.py not found: test must only run from git") - def get_pypath(): import sysconfig pypath = sysconfig.get_path('purelib', vars={'base': ''}) @@ -176,8 +172,7 @@ class CommandTests(unittest.TestCase): builddir = str(self.tmpdir / 'build4') (bindir / 'meson').rename(bindir / 'meson.real') wrapper = (bindir / 'meson') - with open(str(wrapper), 'w') as f: - f.write('#!/bin/sh\n\nmeson.real "$@"') + wrapper.open('w').write('#!/bin/sh\n\nmeson.real "$@"') wrapper.chmod(0o755) meson_setup = [str(wrapper), 'setup'] meson_command = meson_setup + self.meson_args @@ -195,5 +190,6 @@ class CommandTests(unittest.TestCase): zipapp.create_archive(source=source, target=target, interpreter=python_command[0], main=None) self._run([target.as_posix(), '--help']) + if __name__ == '__main__': - unittest.main(buffer=True) + sys.exit(unittest.main(buffer=True)) diff --git a/run_tests.py b/run_tests.py index af926ea..103416d 100755 --- a/run_tests.py +++ b/run_tests.py @@ -21,17 +21,18 @@ import shutil import subprocess import tempfile import platform +import argparse from io import StringIO from enum import Enum from glob import glob from pathlib import Path - import mesonbuild from mesonbuild import mesonlib from mesonbuild import mesonmain from mesonbuild import mtest from mesonbuild import mlog from mesonbuild.environment import Environment, detect_ninja +from mesonbuild.coredata import backendlist # Fake classes and objects for mocking @@ -106,9 +107,9 @@ def find_vcxproj_with_target(builddir, target): import re, fnmatch t, ext = os.path.splitext(target) if ext: - p = '<TargetName>{}</TargetName>\s*<TargetExt>\{}</TargetExt>'.format(t, ext) + p = r'<TargetName>{}</TargetName>\s*<TargetExt>\{}</TargetExt>'.format(t, ext) else: - p = '<TargetName>{}</TargetName>'.format(t) + p = r'<TargetName>{}</TargetName>'.format(t) for root, dirs, files in os.walk(builddir): for f in fnmatch.filter(files, '*.vcxproj'): f = os.path.join(builddir, f) @@ -218,32 +219,40 @@ def print_system_info(): print('System:', platform.system()) print('') -if __name__ == '__main__': +def main(): print_system_info() + parser = argparse.ArgumentParser() + parser.add_argument('--cov', action='store_true') + parser.add_argument('--backend', default=None, dest='backend', + choices=backendlist) + parser.add_argument('--cross', default=False, dest='cross', action='store_true') + (options, _) = parser.parse_known_args() # Enable coverage early... - enable_coverage = '--cov' in sys.argv + enable_coverage = options.cov if enable_coverage: os.makedirs('.coverage', exist_ok=True) sys.argv.remove('--cov') import coverage coverage.process_startup() returncode = 0 - # Iterate over list in reverse order to find the last --backend arg - backend = Backend.ninja - cross = False - # FIXME: PLEASE convert to argparse - for arg in reversed(sys.argv[1:]): - if arg.startswith('--backend'): - if arg.startswith('--backend=vs'): - backend = Backend.vs - elif arg == '--backend=xcode': - backend = Backend.xcode - if arg.startswith('--cross'): - cross = True - if arg == '--cross=mingw': - cross = 'mingw' - elif arg == '--cross=arm': - cross = 'arm' + backend = options.backend + cross = options.cross + msbuild_exe = shutil.which('msbuild') + # Auto-detect backend if unspecified + if backend is None: + if msbuild_exe is not None: + backend = 'vs' # Meson will auto-detect VS version to use + else: + backend = 'ninja' + # Set backend arguments for Meson + if backend.startswith('vs'): + backend = Backend.vs + elif backend == 'xcode': + backend = Backend.xcode + elif backend == 'ninja': + backend = Backend.ninja + else: + raise RuntimeError('Unknown backend: {!r}'.format(backend)) # Running on a developer machine? Be nice! if not mesonlib.is_windows() and not mesonlib.is_haiku() and 'TRAVIS' not in os.environ: os.nice(20) @@ -267,26 +276,33 @@ if __name__ == '__main__': # Can't pass arguments to unit tests, so set the backend to use in the environment env = os.environ.copy() env['MESON_UNIT_TEST_BACKEND'] = backend.name - with tempfile.TemporaryDirectory() as td: + with tempfile.TemporaryDirectory() as temp_dir: # Enable coverage on all subsequent processes. if enable_coverage: - with open(os.path.join(td, 'usercustomize.py'), 'w') as f: - f.write('import coverage\n' - 'coverage.process_startup()\n') + Path(temp_dir, 'usercustomize.py').open('w').write( + 'import coverage\n' + 'coverage.process_startup()\n') env['COVERAGE_PROCESS_START'] = '.coveragerc' - env['PYTHONPATH'] = os.pathsep.join([td] + env.get('PYTHONPATH', [])) + env['PYTHONPATH'] = os.pathsep.join([temp_dir] + env.get('PYTHONPATH', [])) if not cross: - returncode += subprocess.call(mesonlib.python_command + ['run_meson_command_tests.py', '-v'], env=env) - returncode += subprocess.call(mesonlib.python_command + ['run_unittests.py', '-v'], env=env) - returncode += subprocess.call(mesonlib.python_command + ['run_project_tests.py'] + sys.argv[1:], env=env) + cmd = mesonlib.python_command + ['run_meson_command_tests.py', '-v'] + returncode += subprocess.call(cmd, env=env) + cmd = mesonlib.python_command + ['run_unittests.py', '-v'] + returncode += subprocess.call(cmd, env=env) + cmd = mesonlib.python_command + ['run_project_tests.py'] + sys.argv[1:] + returncode += subprocess.call(cmd, env=env) else: cross_test_args = mesonlib.python_command + ['run_cross_test.py'] - if cross is True or cross == 'arm': - print(mlog.bold('Running armhf cross tests.').get_text(mlog.colorize_console)) - print() - returncode += subprocess.call(cross_test_args + ['cross/ubuntu-armhf.txt'], env=env) - if cross is True or cross == 'mingw': - print(mlog.bold('Running mingw-w64 64-bit cross tests.').get_text(mlog.colorize_console)) - print() - returncode += subprocess.call(cross_test_args + ['cross/linux-mingw-w64-64bit.txt'], env=env) - sys.exit(returncode) + print(mlog.bold('Running armhf cross tests.').get_text(mlog.colorize_console)) + print() + cmd = cross_test_args + ['cross/ubuntu-armhf.txt'] + returncode += subprocess.call(cmd, env=env) + print(mlog.bold('Running mingw-w64 64-bit cross tests.') + .get_text(mlog.colorize_console)) + print() + cmd = cross_test_args + ['cross/linux-mingw-w64-64bit.txt'] + returncode += subprocess.call(cmd, env=env) + return returncode + +if __name__ == '__main__': + sys.exit(main()) diff --git a/run_unittests.py b/run_unittests.py index 9ffce52..9b7e45a 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -16,11 +16,13 @@ import stat import shlex import subprocess -import re, json +import re +import json import tempfile import textwrap import os import shutil +import sys import unittest import platform from itertools import chain @@ -4329,7 +4331,7 @@ def should_run_cross_arm_tests(): def should_run_cross_mingw_tests(): return shutil.which('x86_64-w64-mingw32-gcc') and not (is_windows() or is_cygwin()) -if __name__ == '__main__': +def main(): unset_envs() cases = ['InternalTests', 'DataTests', 'AllPlatformTests', 'FailureTests', 'PythonTests'] if not is_windows(): @@ -4343,4 +4345,7 @@ if __name__ == '__main__': if is_osx(): cases += ['DarwinTests'] - unittest.main(defaultTest=cases, buffer=True) + return unittest.main(defaultTest=cases, buffer=True) + +if __name__ == '__main__': + sys.exit(main()) |