run_tests 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. # Copyright 2019 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Wrapper to run pytest with the right settings."""
  16. import errno
  17. import os
  18. import shutil
  19. import subprocess
  20. import sys
  21. def find_pytest():
  22. """Try to locate a good version of pytest."""
  23. # Use the Python 3 version if available.
  24. ret = shutil.which('pytest-3')
  25. if ret:
  26. return ret
  27. # Hopefully this is a Python 3 version.
  28. ret = shutil.which('pytest')
  29. if ret:
  30. return ret
  31. print(f'{__file__}: unable to find pytest.', file=sys.stderr)
  32. print(f'{__file__}: Try installing: sudo apt-get install python-pytest',
  33. file=sys.stderr)
  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. pytest = find_pytest()
  44. return subprocess.run([pytest] + argv, check=True)
  45. if __name__ == '__main__':
  46. sys.exit(main(sys.argv[1:]))