blob: 6c6f8594b64871078e89e54607d8b69f4ab119a5 [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."""
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 Frysingerbeea5de2021-03-10 22:31:41 -050037 print('%s: unable to find pytest.' % (__file__,), file=sys.stderr)
38 print('%s: Try installing: sudo apt-get install python-pytest' % (__file__,),
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050039 file=sys.stderr)
Mike Frysinger4f42a972019-06-12 17:42:43 -040040
41
42def 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 Frysingerd2163842020-02-15 13:44:56 -050046 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 Frysinger4f42a972019-06-12 17:42:43 -040051
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050052 pytest = find_pytest()
Mike Frysingerbfbcfd92021-03-11 00:07:24 -050053 return subprocess.run([pytest] + argv, check=False).returncode
Mike Frysinger4f42a972019-06-12 17:42:43 -040054
55
56if __name__ == '__main__':
57 sys.exit(main(sys.argv[1:]))