blob: 573dd44683524c423f2cc7637a83623ab51eb139 [file] [log] [blame]
Mike Frysinger9dfd69f2020-12-01 13:27:56 -05001#!/usr/bin/env python3
Mike Frysinger4f42a972019-06-12 17:42:43 -04002# 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 Frysinger4f42a972019-06-12 17:42:43 -040018import errno
19import os
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050020import shutil
Mike Frysinger4f42a972019-06-12 17:42:43 -040021import subprocess
22import sys
23
24
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050025def find_pytest():
26 """Try to locate a good version of pytest."""
Mike Frysinger816d82c2021-04-19 12:40:35 -040027 # If we're in a virtualenv, assume that it's provided the right pytest.
28 if 'VIRTUAL_ENV' in os.environ:
29 return 'pytest'
30
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050031 # Use the Python 3 version if available.
32 ret = shutil.which('pytest-3')
33 if ret:
34 return ret
35
36 # Hopefully this is a Python 3 version.
37 ret = shutil.which('pytest')
38 if ret:
39 return ret
40
Mike Frysingerbeea5de2021-03-10 22:31:41 -050041 print('%s: unable to find pytest.' % (__file__,), file=sys.stderr)
42 print('%s: Try installing: sudo apt-get install python-pytest' % (__file__,),
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050043 file=sys.stderr)
Mike Frysinger4f42a972019-06-12 17:42:43 -040044
45
46def main(argv):
47 """The main entry."""
48 # Add the repo tree to PYTHONPATH as the tests expect to be able to import
49 # modules directly.
Mike Frysingerd2163842020-02-15 13:44:56 -050050 pythonpath = os.path.dirname(os.path.realpath(__file__))
51 oldpythonpath = os.environ.get('PYTHONPATH', None)
52 if oldpythonpath is not None:
53 pythonpath += os.pathsep + oldpythonpath
54 os.environ['PYTHONPATH'] = pythonpath
Mike Frysinger4f42a972019-06-12 17:42:43 -040055
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050056 pytest = find_pytest()
Mike Frysingerbfbcfd92021-03-11 00:07:24 -050057 return subprocess.run([pytest] + argv, check=False).returncode
Mike Frysinger4f42a972019-06-12 17:42:43 -040058
59
60if __name__ == '__main__':
61 sys.exit(main(sys.argv[1:]))