From 9dfd69f7730bc73d3a1e3a97e99041dcf28f3d1f Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 1 Dec 2020 13:27:56 -0500 Subject: [PATCH] run_tests: rewrite to use Python 3 Some distros still have `pytest` as Python 2 and sep `pytest-3`. Rewrite this script to use `pytest-3` if available. Change-Id: I430ed8792e7b0da9b217f948f2e983aa62bf1299 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/290503 Reviewed-by: Michael Mortensen Tested-by: Mike Frysinger --- run_tests | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/run_tests b/run_tests index a09ab382..30e7b55a 100755 --- a/run_tests +++ b/run_tests @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright 2019 The Android Open Source Project # @@ -20,22 +20,26 @@ from __future__ import print_function import errno import os +import shutil import subprocess import sys -def run_pytest(cmd, argv): - """Run the unittests via |cmd|.""" - try: - return subprocess.call([cmd] + argv) - except OSError as e: - if e.errno == errno.ENOENT: - print('%s: unable to run `%s`: %s' % (__file__, cmd, e), file=sys.stderr) - print('%s: Try installing pytest: sudo apt-get install python-pytest' % - (__file__,), file=sys.stderr) - return 127 - else: - raise +def find_pytest(): + """Try to locate a good version of pytest.""" + # Use the Python 3 version if available. + ret = shutil.which('pytest-3') + if ret: + return ret + + # Hopefully this is a Python 3 version. + ret = shutil.which('pytest') + if ret: + return ret + + print(f'{__file__}: unable to find pytest.', file=sys.stderr) + print(f'{__file__}: Try installing: sudo apt-get install python-pytest', + file=sys.stderr) def main(argv): @@ -48,7 +52,8 @@ def main(argv): pythonpath += os.pathsep + oldpythonpath os.environ['PYTHONPATH'] = pythonpath - return run_pytest('pytest', argv) + pytest = find_pytest() + return subprocess.run([pytest] + argv, check=True) if __name__ == '__main__':