test_util.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # SPDX-License-Identifier: GPL-2.0+
  2. #
  3. # Copyright (c) 2016 Google, Inc
  4. #
  5. import glob
  6. import os
  7. import sys
  8. import command
  9. def RunTestCoverage(prog, filter_fname, exclude_list, build_dir, required=None):
  10. """Run tests and check that we get 100% coverage
  11. Args:
  12. prog: Program to run (with be passed a '-t' argument to run tests
  13. filter_fname: Normally all *.py files in the program's directory will
  14. be included. If this is not None, then it is used to filter the
  15. list so that only filenames that don't contain filter_fname are
  16. included.
  17. exclude_list: List of file patterns to exclude from the coverage
  18. calculation
  19. build_dir: Build directory, used to locate libfdt.py
  20. required: List of modules which must be in the coverage report
  21. Raises:
  22. ValueError if the code coverage is not 100%
  23. """
  24. # This uses the build output from sandbox_spl to get _libfdt.so
  25. path = os.path.dirname(prog)
  26. if filter_fname:
  27. glob_list = glob.glob(os.path.join(path, '*.py'))
  28. glob_list = [fname for fname in glob_list if filter_fname in fname]
  29. else:
  30. glob_list = []
  31. glob_list += exclude_list
  32. glob_list += ['*libfdt.py', '*site-packages*']
  33. cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools python-coverage run '
  34. '--omit "%s" %s -t' % (build_dir, ','.join(glob_list), prog))
  35. os.system(cmd)
  36. stdout = command.Output('python-coverage', 'report')
  37. lines = stdout.splitlines()
  38. if required:
  39. # Convert '/path/to/name.py' just the module name 'name'
  40. test_set = set([os.path.splitext(os.path.basename(line.split()[0]))[0]
  41. for line in lines if '/etype/' in line])
  42. missing_list = required
  43. missing_list.difference_update(test_set)
  44. if missing_list:
  45. print 'Missing tests for %s' % (', '.join(missing_list))
  46. print stdout
  47. ok = False
  48. coverage = lines[-1].split(' ')[-1]
  49. ok = True
  50. print coverage
  51. if coverage != '100%':
  52. print stdout
  53. print ("Type 'python-coverage html' to get a report in "
  54. 'htmlcov/index.html')
  55. print 'Coverage error: %s, but should be 100%%' % coverage
  56. ok = False
  57. if not ok:
  58. raise ValueError('Test coverage failure')