test: Run ctest.

Run ctest if an emulator is available and make sure it uses it.

Add more status information to the test output and make sure that the order is
correct with flush() calls.
This commit is contained in:
Matt McCormick 2016-03-06 14:16:49 -05:00
parent 25e9333bf9
commit 0e8478d1db

View File

@ -1,6 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
"""Run the tests.""" """Test that the toolchain can build executables.
Multiple build tools and languages are supported. If an emulator is available,
its ability to run the generated executables is also tested.
"""
import argparse import argparse
import glob import glob
@ -10,7 +14,7 @@ import subprocess
import sys import sys
import tempfile import tempfile
def test_no_build_system(build_dir, language, source): def test_none_build_system(build_dir, language, source):
if language == 'C': if language == 'C':
compiler = os.getenv('CC', 'cc') compiler = os.getenv('CC', 'cc')
elif language == 'C++': elif language == 'C++':
@ -19,22 +23,30 @@ def test_no_build_system(build_dir, language, source):
print('Unknown language: ' + language) print('Unknown language: ' + language)
return 1 return 1
print('Building ' + source + ' by calling ' + compiler + '...') print('Building ' + source + ' by calling ' + compiler + '...')
sys.stdout.flush()
return subprocess.call([compiler, source]) return subprocess.call([compiler, source])
def test_cmake_build_system(build_dir, source): def test_cmake_build_system(build_dir, source, emulator):
shutil.copy(source, build_dir) shutil.copy(source, build_dir)
with open('CMakeLists.txt', 'w') as fp: with open('CMakeLists.txt', 'w') as fp:
fp.write('cmake_minimum_required(VERSION 3.0)\n') fp.write('cmake_minimum_required(VERSION 3.0)\n')
fp.write('project(test-compiler)\n') fp.write('project(test-compiler)\n')
fp.write('add_executable(a.out ' + os.path.basename(source) + ')\n') fp.write('add_executable(a.out ' + os.path.basename(source) + ')\n')
if emulator:
fp.write('enable_testing()\n')
fp.write('add_test(emulator-in-cmake a.out)\n')
os.mkdir('build') os.mkdir('build')
os.chdir('build') os.chdir('build')
print('Building ' + source + ' with CMake...') print('Building ' + source + ' with CMake...')
sys.stdout.flush()
if subprocess.call(['cmake', '..']): if subprocess.call(['cmake', '..']):
return 1 return 1
if subprocess.call(['make']): if subprocess.call(['make']):
return 1 return 1
if emulator:
if subprocess.call(['ctest']):
return 1
shutil.copy('a.out', build_dir) shutil.copy('a.out', build_dir)
return 0 return 0
@ -46,9 +58,9 @@ def test_source(source, language, build_system, emulator):
os.chdir(build_dir) os.chdir(build_dir)
if build_system == 'None': if build_system == 'None':
result += test_no_build_system(build_dir, language, source) result += test_none_build_system(build_dir, language, source)
elif build_system == 'CMake': elif build_system == 'CMake':
result += test_cmake_build_system(build_dir, source) result += test_cmake_build_system(build_dir, source, emulator)
else: else:
print('Unknown build system: ' + build_system) print('Unknown build system: ' + build_system)
result += 1 result += 1
@ -57,14 +69,20 @@ def test_source(source, language, build_system, emulator):
cmd = emulator cmd = emulator
cmd += ' ' + os.path.join(build_dir, 'a.out') cmd += ' ' + os.path.join(build_dir, 'a.out')
print('Running ' + cmd + '...') print('Running ' + cmd + '...')
sys.stdout.flush()
result += subprocess.call(cmd, shell=True) result += subprocess.call(cmd, shell=True)
os.chdir(cwd) os.chdir(cwd)
shutil.rmtree(build_dir) shutil.rmtree(build_dir)
sys.stdout.flush()
return result return result
def test_build_system(test_dir, language, build_system, emulator): def test_build_system(test_dir, language, build_system, emulator):
print('\n\n--------------------------------------------------------')
print('Testing ' + build_system + ' build system with the ' +
language + ' language\n')
sys.stdout.flush()
result = 0 result = 0
for source in glob.glob(os.path.join(test_dir, language, '*')): for source in glob.glob(os.path.join(test_dir, language, '*')):
result += test_source(source, language, build_system, emulator) result += test_source(source, language, build_system, emulator)