wscript (3239B)
1 #! /usr/bin/env python 2 # encoding: utf-8 3 # Calle Rosenquist, 2016 (xbreak) 4 5 """ 6 Execute Python tests during build 7 8 To force all tests, run with "waf build --alltests" 9 """ 10 11 top = '.' 12 out = 'build' 13 14 def test_results(bld): 15 """ 16 Custom post- function that prints out test results. 17 """ 18 lst = getattr(bld, 'utest_results', []) 19 if not lst: 20 return 21 for (f, code, out, err) in lst: 22 print(out.decode('utf-8')) 23 print(err.decode('utf-8')) 24 25 26 def options(opt): 27 opt.load('python compiler_c') 28 opt.load('waf_unit_test pytest') 29 30 def configure(cnf): 31 cnf.load('python compiler_c waf_unit_test pytest buildcopy') 32 # The foo_ext module is using Python 3: 33 cnf.check_python_version(minver=(3, 0, 0)) 34 cnf.check_python_headers() 35 36 def build(bld): 37 # foo_ext and baz_ext are Python C extensions that demonstrates unit test 38 # environment population of PYTHONPATH and LD_LIBRARY_PATH/PATH/DYLD_LIBRARY_PATH. 39 40 # foo_ext is installed as part of the foo Python package and thus does not need 41 # to specify a PYTHONPATH via pytest_path. 42 bld(name = 'foo_ext', 43 features = 'c cshlib pyext', 44 source = 'src/foo_ext.c', 45 target = 'src/foo/foo_ext', 46 install_path = '${PYTHONDIR}/foo') 47 48 # baz_ext is a stand-alone Python module so we need to specify pytest_path to where baz is built: 49 bld(name = 'baz_ext', 50 features = 'c cshlib pyext', 51 source = 'src/baz/baz_ext.c', 52 target = 'src/baz/baz_ext', 53 install_path = '${PYTHONDIR}', 54 pytest_path = [bld.path.find_dir('src/baz').get_bld()]) 55 56 # Foo is a Python package that together with foo_ext is complete. 57 # Since the package is incomplete in the source directory and cannot be tested there 58 # we use the `buildcopy' feature to copy sources to build. 59 # 60 # If buildcopy_source is not specified, source will be used as input. 61 bld(name = 'foo', 62 features = 'py buildcopy', 63 use = 'foo_ext', 64 source = bld.path.ant_glob('src/foo/*.py'), 65 install_from = 'src') 66 67 # The bar module has a non-Python dependency to resource.txt which we want to copy, 68 # but in this case we cannot add resource.txt to the sources because there's no feature 69 # for it. Therefore, we use the attribute buildcopy_source instead. 70 bld(name = 'bar', 71 features = 'py buildcopy', 72 source = bld.path.ant_glob('src/bar/*.py'), 73 buildcopy_source = bld.path.ant_glob('src/bar/*.py') + ['src/bar/resource.txt'], 74 install_from = 'src') 75 76 # Unit test example using the built in module unittest and let that discover 77 # any test cases. 78 # By using ``foo bar baz_ext`` the relevant variables for those taskgens 79 # will be added to sys.path via ``PYTHONPATH`` as well as any library paths from 80 # dependent libraries to the system library path e.g. ``LD_LIBRARY_PATH``. 81 # 82 # The dependency chain looks like the following: 83 # 84 # foo_test -> foo -> foo_ext -> libpython (external) 85 # -> bar -> (resource.txt) 86 # -> baz_ext -> libpython (external) 87 # 88 bld(name = 'py_test', 89 features = 'pytest', 90 use = 'foo bar baz_ext', 91 pytest_source = bld.path.ant_glob('test/*.py'), 92 ut_str = '${PYTHON} -B -m unittest discover') 93 94 bld.add_post_fun(test_results)