Blame lang/python/tests/run-tests.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
#
Packit Service 672cf4
# This file is part of GPGME.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
# under the terms of the GNU General Public License as published by
Packit Service 672cf4
# the Free Software Foundation; either version 2 of the License, or
Packit Service 672cf4
# (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit Service 672cf4
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit Service 672cf4
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit Service 672cf4
# Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service 672cf4
Packit Service 6c01f9
from __future__ import absolute_import
Packit Service 6c01f9
from __future__ import division
Packit Service 6c01f9
from __future__ import print_function
Packit Service 6c01f9
from __future__ import unicode_literals
Packit Service 672cf4
Packit Service 672cf4
import argparse
Packit Service 672cf4
import glob
Packit Service 672cf4
import os
Packit Service 672cf4
import subprocess
Packit Service 672cf4
import sys
Packit Service 672cf4
Packit Service 672cf4
class SplitAndAccumulate(argparse.Action):
Packit Service 672cf4
    def __call__(self, parser, namespace, values, option_string=None):
Packit Service 672cf4
        current = getattr(namespace, self.dest, list())
Packit Service 672cf4
        current.extend(values.split())
Packit Service 672cf4
        setattr(namespace, self.dest, current)
Packit Service 672cf4
Packit Service 672cf4
parser = argparse.ArgumentParser(description='Run tests.')
Packit Service 6c01f9
parser.add_argument('tests', metavar='TEST', type=str, nargs='+',
Packit Service 6c01f9
                    help='A test to run')
Packit Service 6c01f9
parser.add_argument('-v', '--verbose', action="store_true", default=False,
Packit Service 6c01f9
                    help='Be verbose.')
Packit Service 6c01f9
parser.add_argument('-q', '--quiet', action="store_true", default=False,
Packit Service 6c01f9
                    help='Be quiet.')
Packit Service 6c01f9
parser.add_argument('--interpreters', metavar='PYTHON', type=str,
Packit Service 6c01f9
                    default=[], action=SplitAndAccumulate,
Packit Service 6c01f9
                    help='Use these interpreters to run the tests, ' +
Packit Service 6c01f9
                    'separated by spaces.')
Packit Service 6c01f9
parser.add_argument('--srcdir', type=str,
Packit Service 6c01f9
                    default=os.environ.get("srcdir", ""),
Packit Service 6c01f9
                    help='Location of the tests.')
Packit Service 6c01f9
parser.add_argument('--builddir', type=str,
Packit Service 6c01f9
                    default=os.environ.get("abs_builddir", ""),
Packit Service 6c01f9
                    help='Location of the tests.')
Packit Service 6c01f9
parser.add_argument('--python-libdir', type=str,
Packit Service 6c01f9
                    default=None,
Packit Service 6c01f9
                    help='Optional location of the in-tree module lib directory.')
Packit Service 6c01f9
parser.add_argument('--parallel', action="store_true", default=False,
Packit Service 6c01f9
                    help='Ignored.  For compatibility with run-tests.scm.')
Packit Service 672cf4
Packit Service 672cf4
args = parser.parse_args()
Packit Service 672cf4
if not args.interpreters:
Packit Service 672cf4
    args.interpreters = [sys.executable]
Packit Service 672cf4
Packit Service 672cf4
out = sys.stdout if args.verbose else None
Packit Service 672cf4
err = sys.stderr if args.verbose else None
Packit Service 672cf4
Packit Service 672cf4
def status_to_str(code):
Packit Service 672cf4
    return {0: "PASS", 77: "SKIP", 99: "ERROR"}.get(code, "FAIL")
Packit Service 672cf4
Packit Service 672cf4
results = list()
Packit Service 672cf4
for interpreter in args.interpreters:
Packit Service 6c01f9
    version = subprocess.check_output(
Packit Service 6c01f9
        [interpreter, "-c", "import sys; print('{0}.{1}'.format(sys.version_info[0], sys.version_info[1]))"]).strip().decode()
Packit Service 672cf4
Packit Service 672cf4
    if args.python_libdir:
Packit Service 672cf4
        python_libdir = args.python_libdir
Packit Service 672cf4
    else:
Packit Service 6c01f9
        pattern = os.path.join(args.builddir, "..",
Packit Service 6c01f9
                               "{0}-gpg".format(os.path.basename(interpreter)),
Packit Service 6c01f9
                               "lib*")
Packit Service 672cf4
        libdirs = glob.glob(pattern)
Packit Service 672cf4
        if len(libdirs) == 0:
Packit Service 6c01f9
            sys.exit("Build directory matching {0!r} not found.".format(pattern))
Packit Service 672cf4
        elif len(libdirs) > 1:
Packit Service 6c01f9
            sys.exit("Multiple build directories matching {0!r} found: {1}".format(
Packit Service 6c01f9
                pattern, libdirs))
Packit Service 672cf4
        python_libdir = libdirs[0]
Packit Service 672cf4
Packit Service 672cf4
    env = dict(os.environ)
Packit Service 672cf4
    env["PYTHONPATH"] = python_libdir
Packit Service 672cf4
Packit Service 672cf4
    if not args.quiet:
Packit Service 672cf4
        print("Running tests using {0} ({1})...".format(interpreter, version))
Packit Service 672cf4
Packit Service 672cf4
    for test in args.tests:
Packit Service 672cf4
        status = subprocess.call(
Packit Service 672cf4
            [interpreter, os.path.join(args.srcdir, test)],
Packit Service 6c01f9
            env=env, stdout=out, stderr=err)
Packit Service 672cf4
        if not args.quiet:
Packit Service 672cf4
            print("{0}: {1}".format(status_to_str(status), test))
Packit Service 672cf4
        results.append(status)
Packit Service 672cf4
Packit Service 672cf4
def count(status):
Packit Service 672cf4
    return len(list(filter(lambda x: x == status, results)))
Packit Service 672cf4
def failed():
Packit Service 672cf4
    return len(list(filter(lambda x: x not in (0, 77, 99), results)))
Packit Service 672cf4
Packit Service 672cf4
if not args.quiet:
Packit Service 672cf4
    print("{0} tests run, {1} succeeded, {2} failed, {3} skipped.".format(
Packit Service 672cf4
        len(results), count(0), failed(), count(77)))
Packit Service 672cf4
    sys.exit(len(results) - count(0) - count(77))
Packit Service 672cf4
sys.exit(results[0])