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