diff options
Diffstat (limited to 'run_tests')
-rwxr-xr-x | run_tests | 50 |
1 files changed, 29 insertions, 21 deletions
@@ -1,5 +1,4 @@ | |||
1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python3 |
2 | # -*- coding:utf-8 -*- | ||
3 | # Copyright 2019 The Android Open Source Project | 2 | # Copyright 2019 The Android Open Source Project |
4 | # | 3 | # |
5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
@@ -16,37 +15,46 @@ | |||
16 | 15 | ||
17 | """Wrapper to run pytest with the right settings.""" | 16 | """Wrapper to run pytest with the right settings.""" |
18 | 17 | ||
19 | from __future__ import print_function | ||
20 | |||
21 | import errno | 18 | import errno |
22 | import os | 19 | import os |
20 | import shutil | ||
23 | import subprocess | 21 | import subprocess |
24 | import sys | 22 | import sys |
25 | 23 | ||
26 | 24 | ||
27 | def run_pytest(cmd, argv): | 25 | def find_pytest(): |
28 | """Run the unittests via |cmd|.""" | 26 | """Try to locate a good version of pytest.""" |
29 | try: | 27 | # If we're in a virtualenv, assume that it's provided the right pytest. |
30 | return subprocess.call([cmd] + argv) | 28 | if 'VIRTUAL_ENV' in os.environ: |
31 | except OSError as e: | 29 | return 'pytest' |
32 | if e.errno == errno.ENOENT: | 30 | |
33 | print('%s: unable to run `%s`: %s' % (__file__, cmd, e), file=sys.stderr) | 31 | # Use the Python 3 version if available. |
34 | print('%s: Try installing pytest: sudo apt-get install python-pytest' % | 32 | ret = shutil.which('pytest-3') |
35 | (__file__,), file=sys.stderr) | 33 | if ret: |
36 | return 127 | 34 | return ret |
37 | else: | 35 | |
38 | raise | 36 | # Hopefully this is a Python 3 version. |
37 | ret = shutil.which('pytest') | ||
38 | if ret: | ||
39 | return ret | ||
40 | |||
41 | print('%s: unable to find pytest.' % (__file__,), file=sys.stderr) | ||
42 | print('%s: Try installing: sudo apt-get install python-pytest' % (__file__,), | ||
43 | file=sys.stderr) | ||
39 | 44 | ||
40 | 45 | ||
41 | def main(argv): | 46 | def main(argv): |
42 | """The main entry.""" | 47 | """The main entry.""" |
43 | # Add the repo tree to PYTHONPATH as the tests expect to be able to import | 48 | # Add the repo tree to PYTHONPATH as the tests expect to be able to import |
44 | # modules directly. | 49 | # modules directly. |
45 | topdir = os.path.dirname(os.path.realpath(__file__)) | 50 | pythonpath = os.path.dirname(os.path.realpath(__file__)) |
46 | pythonpath = os.environ.get('PYTHONPATH', '') | 51 | oldpythonpath = os.environ.get('PYTHONPATH', None) |
47 | os.environ['PYTHONPATH'] = '%s:%s' % (topdir, pythonpath) | 52 | if oldpythonpath is not None: |
48 | 53 | pythonpath += os.pathsep + oldpythonpath | |
49 | return run_pytest('pytest', argv) | 54 | os.environ['PYTHONPATH'] = pythonpath |
55 | |||
56 | pytest = find_pytest() | ||
57 | return subprocess.run([pytest] + argv, check=False).returncode | ||
50 | 58 | ||
51 | 59 | ||
52 | if __name__ == '__main__': | 60 | if __name__ == '__main__': |