blob: 7c9ff41d8122f9e5485cdb706657b4cf0c0e2265 [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
Joanna Wanga6c52f52022-11-03 16:51:19 -040023import repo_trace
Mike Frysinger4f42a972019-06-12 17:42:43 -040024
25
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050026def find_pytest():
27 """Try to locate a good version of pytest."""
Mike Frysinger816d82c2021-04-19 12:40:35 -040028 # 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 Frysinger9dfd69f2020-12-01 13:27:56 -050032 # 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 Frysingerbeea5de2021-03-10 22:31:41 -050042 print('%s: unable to find pytest.' % (__file__,), file=sys.stderr)
43 print('%s: Try installing: sudo apt-get install python-pytest' % (__file__,),
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050044 file=sys.stderr)
Mike Frysinger4f42a972019-06-12 17:42:43 -040045
46
47def 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 Frysingerd2163842020-02-15 13:44:56 -050051 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 Frysinger4f42a972019-06-12 17:42:43 -040056
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050057 pytest = find_pytest()
Mike Frysingerbfbcfd92021-03-11 00:07:24 -050058 return subprocess.run([pytest] + argv, check=False).returncode
Mike Frysinger4f42a972019-06-12 17:42:43 -040059
60
61if __name__ == '__main__':
62 sys.exit(main(sys.argv[1:]))