summaryrefslogtreecommitdiffstats
path: root/run_tests
diff options
context:
space:
mode:
Diffstat (limited to 'run_tests')
-rwxr-xr-xrun_tests50
1 files changed, 29 insertions, 21 deletions
diff --git a/run_tests b/run_tests
index d7144b3c..573dd446 100755
--- a/run_tests
+++ b/run_tests
@@ -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
19from __future__ import print_function
20
21import errno 18import errno
22import os 19import os
20import shutil
23import subprocess 21import subprocess
24import sys 22import sys
25 23
26 24
27def run_pytest(cmd, argv): 25def 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
41def main(argv): 46def 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
52if __name__ == '__main__': 60if __name__ == '__main__':