Files
templates/c/gas-logger/tests/run_tests.py

47 lines
2.4 KiB
Python

"""Build in a temporary directory; run core + Python/C end-to-end tests."""
import argparse
import ctypes
import importlib.util
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
parser=argparse.ArgumentParser()
parser.add_argument('--cc',default=shutil.which('gcc'))
args=parser.parse_args()
if not args.cc:raise SystemExit('Specify --cc /path/to/gcc')
root=Path(__file__).resolve().parents[1]
templates=root.parents[1]
sys.path.insert(0,str(templates/'python'))
env=dict(os.environ)
env['PATH']=str(Path(args.cc).resolve().parent)+os.pathsep+env.get('PATH','')
flags=[args.cc,'-std=c99','-Wall','-Wextra','-Werror','-pedantic','-I'+str(root/'include')]
with tempfile.TemporaryDirectory(prefix='gas-logger-') as directory:
binary=Path(directory)/'core.exe'
subprocess.run(flags+[str(root/'src/gas_logger.c'),str(root/'tests/test_gas_logger.c'),'-o',str(binary)],check=True,env=env)
subprocess.run([str(binary)],check=True,env=env)
lib=Path(directory)/('device.dll' if os.name=='nt' else 'device.so')
subprocess.run(flags+([] if os.name=='nt' else ['-fPIC'])+['-shared','-I'+str(root/'ports/pm35'),
'-I'+str(templates/'c/set-protocol/include'),
str(root/'src/gas_logger.c'),str(root/'src/gas_logger_pcan.c'),
str(root/'ports/pm35/gas_logger_pm35.c'),str(root/'tests/host_device.c'),
'-o',str(lib)],check=True,env=env)
# Child releases the loaded DLL before TemporaryDirectory cleanup on Windows.
runner='''import ctypes,importlib.util,sys,unittest
spec=importlib.util.spec_from_file_location('test_client',sys.argv[1])
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
m.DEVICE=ctypes.CDLL(sys.argv[2])
m.DEVICE.host_capture.argtypes=[ctypes.c_uint32,ctypes.c_uint16]
m.DEVICE.host_read.argtypes=[ctypes.c_uint16,ctypes.POINTER(ctypes.c_uint16),ctypes.c_uint16]
m.DEVICE.host_write.argtypes=[ctypes.c_uint16,ctypes.c_uint16]
m.DEVICE.host_pcan_read.argtypes=[ctypes.c_uint16,ctypes.POINTER(ctypes.c_uint16)]
r=unittest.TextTestRunner(verbosity=2).run(unittest.defaultTestLoader.loadTestsFromModule(m))
sys.exit(not r.wasSuccessful())'''
env['PYTHONPATH']=str(templates/'python')
subprocess.run([sys.executable,'-c',runner,str(root/'tests/test_client.py'),str(lib)],check=True,env=env)
subprocess.run([sys.executable,str(root/'tools/generate_map.py'),'--check'],check=True,env=env)