run_tests 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Copyright 2019 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Wrapper to run pytest with the right settings."""
  17. from __future__ import print_function
  18. import errno
  19. import os
  20. import subprocess
  21. import sys
  22. def run_pytest(cmd, argv):
  23. """Run the unittests via |cmd|."""
  24. try:
  25. return subprocess.call([cmd] + argv)
  26. except OSError as e:
  27. if e.errno == errno.ENOENT:
  28. print('%s: unable to run `%s`: %s' % (__file__, cmd, e), file=sys.stderr)
  29. print('%s: Try installing pytest: sudo apt-get install python-pytest' %
  30. (__file__,), file=sys.stderr)
  31. return 127
  32. else:
  33. raise
  34. def main(argv):
  35. """The main entry."""
  36. # Add the repo tree to PYTHONPATH as the tests expect to be able to import
  37. # modules directly.
  38. pythonpath = os.path.dirname(os.path.realpath(__file__))
  39. oldpythonpath = os.environ.get('PYTHONPATH', None)
  40. if oldpythonpath is not None:
  41. pythonpath += os.pathsep + oldpythonpath
  42. os.environ['PYTHONPATH'] = pythonpath
  43. return run_pytest('pytest', argv)
  44. if __name__ == '__main__':
  45. sys.exit(main(sys.argv[1:]))