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