run_tests 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  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 shutil
  21. import subprocess
  22. import sys
  23. def find_pytest():
  24. """Try to locate a good version of pytest."""
  25. # Use the Python 3 version if available.
  26. ret = shutil.which('pytest-3')
  27. if ret:
  28. return ret
  29. # Hopefully this is a Python 3 version.
  30. ret = shutil.which('pytest')
  31. if ret:
  32. return ret
  33. print(f'{__file__}: unable to find pytest.', file=sys.stderr)
  34. print(f'{__file__}: Try installing: sudo apt-get install python-pytest',
  35. file=sys.stderr)
  36. def main(argv):
  37. """The main entry."""
  38. # Add the repo tree to PYTHONPATH as the tests expect to be able to import
  39. # modules directly.
  40. pythonpath = os.path.dirname(os.path.realpath(__file__))
  41. oldpythonpath = os.environ.get('PYTHONPATH', None)
  42. if oldpythonpath is not None:
  43. pythonpath += os.pathsep + oldpythonpath
  44. os.environ['PYTHONPATH'] = pythonpath
  45. pytest = find_pytest()
  46. return subprocess.run([pytest] + argv, check=True)
  47. if __name__ == '__main__':
  48. sys.exit(main(sys.argv[1:]))