wscript 3.2 KB

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