tests runners: Refactor out global variables and add argparse

This commit is contained in:
Niklas Claesson
2018-10-10 21:17:24 +02:00
parent a0a0c244e2
commit 4ef4edee2f
4 changed files with 78 additions and 55 deletions

View File

@ -25,6 +25,7 @@ Eventually migrate to something fancier.'''
import sys import sys
import os import os
from pathlib import Path from pathlib import Path
import argparse
from run_project_tests import gather_tests, run_tests, StopException, setup_commands from run_project_tests import gather_tests, run_tests, StopException, setup_commands
from run_project_tests import failing_logs from run_project_tests import failing_logs
@ -40,11 +41,16 @@ def runtests(cross_file):
print('Total skipped cross tests:', skipped_tests) print('Total skipped cross tests:', skipped_tests)
if failing_tests > 0 and ('TRAVIS' in os.environ or 'APPVEYOR' in os.environ): if failing_tests > 0 and ('TRAVIS' in os.environ or 'APPVEYOR' in os.environ):
print('\nMesonlogs of failing tests\n') print('\nMesonlogs of failing tests\n')
for l in failing_logs: for log in failing_logs:
print(l, '\n') print(log, '\n')
sys.exit(failing_tests) 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__': if __name__ == '__main__':
setup_commands('ninja') sys.exit(main())
cross_file = sys.argv[1]
runtests(cross_file)

View File

@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import sys
import os import os
import tempfile import tempfile
import unittest import unittest
@ -23,11 +24,6 @@ from pathlib import Path
from mesonbuild.mesonlib import windows_proof_rmtree, python_command, is_windows 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(): def get_pypath():
import sysconfig import sysconfig
pypath = sysconfig.get_path('purelib', vars={'base': ''}) pypath = sysconfig.get_path('purelib', vars={'base': ''})
@ -176,8 +172,7 @@ class CommandTests(unittest.TestCase):
builddir = str(self.tmpdir / 'build4') builddir = str(self.tmpdir / 'build4')
(bindir / 'meson').rename(bindir / 'meson.real') (bindir / 'meson').rename(bindir / 'meson.real')
wrapper = (bindir / 'meson') wrapper = (bindir / 'meson')
with open(str(wrapper), 'w') as f: wrapper.open('w').write('#!/bin/sh\n\nmeson.real "$@"')
f.write('#!/bin/sh\n\nmeson.real "$@"')
wrapper.chmod(0o755) wrapper.chmod(0o755)
meson_setup = [str(wrapper), 'setup'] meson_setup = [str(wrapper), 'setup']
meson_command = meson_setup + self.meson_args 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) zipapp.create_archive(source=source, target=target, interpreter=python_command[0], main=None)
self._run([target.as_posix(), '--help']) self._run([target.as_posix(), '--help'])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main(buffer=True) sys.exit(unittest.main(buffer=True))

View File

@ -21,17 +21,18 @@ import shutil
import subprocess import subprocess
import tempfile import tempfile
import platform import platform
import argparse
from io import StringIO from io import StringIO
from enum import Enum from enum import Enum
from glob import glob from glob import glob
from pathlib import Path from pathlib import Path
import mesonbuild import mesonbuild
from mesonbuild import mesonlib from mesonbuild import mesonlib
from mesonbuild import mesonmain from mesonbuild import mesonmain
from mesonbuild import mtest from mesonbuild import mtest
from mesonbuild import mlog from mesonbuild import mlog
from mesonbuild.environment import Environment, detect_ninja from mesonbuild.environment import Environment, detect_ninja
from mesonbuild.coredata import backendlist
# Fake classes and objects for mocking # Fake classes and objects for mocking
@ -106,9 +107,9 @@ def find_vcxproj_with_target(builddir, target):
import re, fnmatch import re, fnmatch
t, ext = os.path.splitext(target) t, ext = os.path.splitext(target)
if ext: if ext:
p = '<TargetName>{}</TargetName>\s*<TargetExt>\{}</TargetExt>'.format(t, ext) p = r'<TargetName>{}</TargetName>\s*<TargetExt>\{}</TargetExt>'.format(t, ext)
else: else:
p = '<TargetName>{}</TargetName>'.format(t) p = r'<TargetName>{}</TargetName>'.format(t)
for root, dirs, files in os.walk(builddir): for root, dirs, files in os.walk(builddir):
for f in fnmatch.filter(files, '*.vcxproj'): for f in fnmatch.filter(files, '*.vcxproj'):
f = os.path.join(builddir, f) f = os.path.join(builddir, f)
@ -218,32 +219,40 @@ def print_system_info():
print('System:', platform.system()) print('System:', platform.system())
print('') print('')
if __name__ == '__main__': def main():
print_system_info() 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 early...
enable_coverage = '--cov' in sys.argv enable_coverage = options.cov
if enable_coverage: if enable_coverage:
os.makedirs('.coverage', exist_ok=True) os.makedirs('.coverage', exist_ok=True)
sys.argv.remove('--cov') sys.argv.remove('--cov')
import coverage import coverage
coverage.process_startup() coverage.process_startup()
returncode = 0 returncode = 0
# Iterate over list in reverse order to find the last --backend arg backend = options.backend
backend = Backend.ninja cross = options.cross
cross = False msbuild_exe = shutil.which('msbuild')
# FIXME: PLEASE convert to argparse # Auto-detect backend if unspecified
for arg in reversed(sys.argv[1:]): if backend is None:
if arg.startswith('--backend'): if msbuild_exe is not None:
if arg.startswith('--backend=vs'): backend = 'vs' # Meson will auto-detect VS version to use
backend = Backend.vs else:
elif arg == '--backend=xcode': backend = 'ninja'
backend = Backend.xcode # Set backend arguments for Meson
if arg.startswith('--cross'): if backend.startswith('vs'):
cross = True backend = Backend.vs
if arg == '--cross=mingw': elif backend == 'xcode':
cross = 'mingw' backend = Backend.xcode
elif arg == '--cross=arm': elif backend == 'ninja':
cross = 'arm' backend = Backend.ninja
else:
raise RuntimeError('Unknown backend: {!r}'.format(backend))
# Running on a developer machine? Be nice! # Running on a developer machine? Be nice!
if not mesonlib.is_windows() and not mesonlib.is_haiku() and 'TRAVIS' not in os.environ: if not mesonlib.is_windows() and not mesonlib.is_haiku() and 'TRAVIS' not in os.environ:
os.nice(20) 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 # Can't pass arguments to unit tests, so set the backend to use in the environment
env = os.environ.copy() env = os.environ.copy()
env['MESON_UNIT_TEST_BACKEND'] = backend.name env['MESON_UNIT_TEST_BACKEND'] = backend.name
with tempfile.TemporaryDirectory() as td: with tempfile.TemporaryDirectory() as temp_dir:
# Enable coverage on all subsequent processes. # Enable coverage on all subsequent processes.
if enable_coverage: if enable_coverage:
with open(os.path.join(td, 'usercustomize.py'), 'w') as f: Path(temp_dir, 'usercustomize.py').open('w').write(
f.write('import coverage\n' 'import coverage\n'
'coverage.process_startup()\n') 'coverage.process_startup()\n')
env['COVERAGE_PROCESS_START'] = '.coveragerc' 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: if not cross:
returncode += subprocess.call(mesonlib.python_command + ['run_meson_command_tests.py', '-v'], env=env) cmd = mesonlib.python_command + ['run_meson_command_tests.py', '-v']
returncode += subprocess.call(mesonlib.python_command + ['run_unittests.py', '-v'], env=env) returncode += subprocess.call(cmd, env=env)
returncode += subprocess.call(mesonlib.python_command + ['run_project_tests.py'] + sys.argv[1:], 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: else:
cross_test_args = mesonlib.python_command + ['run_cross_test.py'] 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(mlog.bold('Running armhf cross tests.').get_text(mlog.colorize_console)) print()
print() cmd = cross_test_args + ['cross/ubuntu-armhf.txt']
returncode += subprocess.call(cross_test_args + ['cross/ubuntu-armhf.txt'], env=env) returncode += subprocess.call(cmd, env=env)
if cross is True or cross == 'mingw': print(mlog.bold('Running mingw-w64 64-bit cross tests.')
print(mlog.bold('Running mingw-w64 64-bit cross tests.').get_text(mlog.colorize_console)) .get_text(mlog.colorize_console))
print() print()
returncode += subprocess.call(cross_test_args + ['cross/linux-mingw-w64-64bit.txt'], env=env) cmd = cross_test_args + ['cross/linux-mingw-w64-64bit.txt']
sys.exit(returncode) returncode += subprocess.call(cmd, env=env)
return returncode
if __name__ == '__main__':
sys.exit(main())

View File

@ -16,11 +16,13 @@
import stat import stat
import shlex import shlex
import subprocess import subprocess
import re, json import re
import json
import tempfile import tempfile
import textwrap import textwrap
import os import os
import shutil import shutil
import sys
import unittest import unittest
import platform import platform
from itertools import chain from itertools import chain
@ -4329,7 +4331,7 @@ def should_run_cross_arm_tests():
def should_run_cross_mingw_tests(): def should_run_cross_mingw_tests():
return shutil.which('x86_64-w64-mingw32-gcc') and not (is_windows() or is_cygwin()) return shutil.which('x86_64-w64-mingw32-gcc') and not (is_windows() or is_cygwin())
if __name__ == '__main__': def main():
unset_envs() unset_envs()
cases = ['InternalTests', 'DataTests', 'AllPlatformTests', 'FailureTests', 'PythonTests'] cases = ['InternalTests', 'DataTests', 'AllPlatformTests', 'FailureTests', 'PythonTests']
if not is_windows(): if not is_windows():
@ -4343,4 +4345,7 @@ if __name__ == '__main__':
if is_osx(): if is_osx():
cases += ['DarwinTests'] cases += ['DarwinTests']
unittest.main(defaultTest=cases, buffer=True) return unittest.main(defaultTest=cases, buffer=True)
if __name__ == '__main__':
sys.exit(main())