1#!/usr/bin/env python 2 3# Copyright JS Foundation and other contributors, http://js.foundation 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 17from __future__ import print_function 18import argparse 19import glob 20import os 21import subprocess 22import sys 23 24import util 25 26 27def get_arguments(): 28 runtime = os.environ.get('RUNTIME') 29 parser = argparse.ArgumentParser() 30 parser.add_argument('-q', '--quiet', action='store_true', 31 help='Only print out failing tests') 32 parser.add_argument('--runtime', metavar='FILE', default=runtime, 33 help='Execution runtime (e.g. qemu)') 34 parser.add_argument('path', 35 help='Path of test binaries') 36 37 script_args = parser.parse_args() 38 return script_args 39 40 41def get_unittests(path): 42 unittests = [] 43 files = glob.glob(os.path.join(path, 'unit-*')) 44 for testfile in files: 45 if os.path.isfile(testfile) and os.access(testfile, os.X_OK): 46 if sys.platform != 'win32' or testfile.endswith(".exe"): 47 unittests.append(testfile) 48 unittests.sort() 49 return unittests 50 51 52def main(args): 53 unittests = get_unittests(args.path) 54 total = len(unittests) 55 if total == 0: 56 print("%s: no unit-* test to execute", args.path) 57 return 1 58 59 test_cmd = [args.runtime] if args.runtime else [] 60 61 tested = 0 62 passed = 0 63 failed = 0 64 for test in unittests: 65 tested += 1 66 test_path = os.path.relpath(test) 67 try: 68 subprocess.check_output(test_cmd + [test], stderr=subprocess.STDOUT, universal_newlines=True) 69 passed += 1 70 if not args.quiet: 71 util.print_test_result(tested, total, True, 'PASS', test_path) 72 except subprocess.CalledProcessError as err: 73 failed += 1 74 util.print_test_result(tested, total, False, 'FAIL (%d)' % err.returncode, test_path) 75 print("================================================") 76 print(err.output) 77 print("================================================") 78 79 util.print_test_summary(os.path.join(os.path.relpath(args.path), "unit-*"), total, passed, failed) 80 81 if failed > 0: 82 return 1 83 return 0 84 85 86if __name__ == "__main__": 87 sys.exit(main(get_arguments())) 88