blob: 30e7b55a04fb86925e5479ddbd4b13471b0a29da [file] [log] [blame]
Mike Frysinger9dfd69f2020-12-01 13:27:56 -05001#!/usr/bin/env python3
Mike Frysinger4f42a972019-06-12 17:42:43 -04002# -*- coding:utf-8 -*-
3# Copyright 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Wrapper to run pytest with the right settings."""
18
19from __future__ import print_function
20
21import errno
22import os
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050023import shutil
Mike Frysinger4f42a972019-06-12 17:42:43 -040024import subprocess
25import sys
26
27
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050028def find_pytest():
29 """Try to locate a good version of pytest."""
30 # Use the Python 3 version if available.
31 ret = shutil.which('pytest-3')
32 if ret:
33 return ret
34
35 # Hopefully this is a Python 3 version.
36 ret = shutil.which('pytest')
37 if ret:
38 return ret
39
40 print(f'{__file__}: unable to find pytest.', file=sys.stderr)
41 print(f'{__file__}: Try installing: sudo apt-get install python-pytest',
42 file=sys.stderr)
Mike Frysinger4f42a972019-06-12 17:42:43 -040043
44
45def main(argv):
46 """The main entry."""
47 # Add the repo tree to PYTHONPATH as the tests expect to be able to import
48 # modules directly.
Mike Frysingerd2163842020-02-15 13:44:56 -050049 pythonpath = os.path.dirname(os.path.realpath(__file__))
50 oldpythonpath = os.environ.get('PYTHONPATH', None)
51 if oldpythonpath is not None:
52 pythonpath += os.pathsep + oldpythonpath
53 os.environ['PYTHONPATH'] = pythonpath
Mike Frysinger4f42a972019-06-12 17:42:43 -040054
Mike Frysinger9dfd69f2020-12-01 13:27:56 -050055 pytest = find_pytest()
56 return subprocess.run([pytest] + argv, check=True)
Mike Frysinger4f42a972019-06-12 17:42:43 -040057
58
59if __name__ == '__main__':
60 sys.exit(main(sys.argv[1:]))