Mike Frysinger | 9dfd69f | 2020-12-01 13:27:56 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 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 | |
| 17 | """Wrapper to run pytest with the right settings.""" |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import errno |
| 22 | import os |
Mike Frysinger | 9dfd69f | 2020-12-01 13:27:56 -0500 | [diff] [blame^] | 23 | import shutil |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 24 | import subprocess |
| 25 | import sys |
| 26 | |
| 27 | |
Mike Frysinger | 9dfd69f | 2020-12-01 13:27:56 -0500 | [diff] [blame^] | 28 | def find_pytest(): |
| 29 | """Try to locate a good version of pytest.""" |
| 30 | # Use the Python 3 version if available. |
| 31 | ret = shutil.which('pytest-3') |
| 32 | if ret: |
| 33 | return ret |
| 34 | |
| 35 | # Hopefully this is a Python 3 version. |
| 36 | ret = shutil.which('pytest') |
| 37 | if ret: |
| 38 | return ret |
| 39 | |
| 40 | print(f'{__file__}: unable to find pytest.', file=sys.stderr) |
| 41 | print(f'{__file__}: Try installing: sudo apt-get install python-pytest', |
| 42 | file=sys.stderr) |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def main(argv): |
| 46 | """The main entry.""" |
| 47 | # Add the repo tree to PYTHONPATH as the tests expect to be able to import |
| 48 | # modules directly. |
Mike Frysinger | d216384 | 2020-02-15 13:44:56 -0500 | [diff] [blame] | 49 | pythonpath = os.path.dirname(os.path.realpath(__file__)) |
| 50 | oldpythonpath = os.environ.get('PYTHONPATH', None) |
| 51 | if oldpythonpath is not None: |
| 52 | pythonpath += os.pathsep + oldpythonpath |
| 53 | os.environ['PYTHONPATH'] = pythonpath |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 54 | |
Mike Frysinger | 9dfd69f | 2020-12-01 13:27:56 -0500 | [diff] [blame^] | 55 | pytest = find_pytest() |
| 56 | return subprocess.run([pytest] + argv, check=True) |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | sys.exit(main(sys.argv[1:])) |