1425bb815Sopenharmony_ci#!/usr/bin/env python
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
4425bb815Sopenharmony_ci#
5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
7425bb815Sopenharmony_ci# You may obtain a copy of the License at
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10425bb815Sopenharmony_ci#
11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
15425bb815Sopenharmony_ci# limitations under the License.
16425bb815Sopenharmony_ci
17425bb815Sopenharmony_cifrom __future__ import print_function
18425bb815Sopenharmony_ciimport argparse
19425bb815Sopenharmony_ciimport glob
20425bb815Sopenharmony_ciimport os
21425bb815Sopenharmony_ciimport subprocess
22425bb815Sopenharmony_ciimport sys
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_ciimport util
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_ci
27425bb815Sopenharmony_cidef get_arguments():
28425bb815Sopenharmony_ci    runtime = os.environ.get('RUNTIME')
29425bb815Sopenharmony_ci    parser = argparse.ArgumentParser()
30425bb815Sopenharmony_ci    parser.add_argument('-q', '--quiet', action='store_true',
31425bb815Sopenharmony_ci                        help='Only print out failing tests')
32425bb815Sopenharmony_ci    parser.add_argument('--runtime', metavar='FILE', default=runtime,
33425bb815Sopenharmony_ci                        help='Execution runtime (e.g. qemu)')
34425bb815Sopenharmony_ci    parser.add_argument('path',
35425bb815Sopenharmony_ci                        help='Path of test binaries')
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci    script_args = parser.parse_args()
38425bb815Sopenharmony_ci    return script_args
39425bb815Sopenharmony_ci
40425bb815Sopenharmony_ci
41425bb815Sopenharmony_cidef get_unittests(path):
42425bb815Sopenharmony_ci    unittests = []
43425bb815Sopenharmony_ci    files = glob.glob(os.path.join(path, 'unit-*'))
44425bb815Sopenharmony_ci    for testfile in files:
45425bb815Sopenharmony_ci        if os.path.isfile(testfile) and os.access(testfile, os.X_OK):
46425bb815Sopenharmony_ci            if sys.platform != 'win32' or testfile.endswith(".exe"):
47425bb815Sopenharmony_ci                unittests.append(testfile)
48425bb815Sopenharmony_ci    unittests.sort()
49425bb815Sopenharmony_ci    return unittests
50425bb815Sopenharmony_ci
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_cidef main(args):
53425bb815Sopenharmony_ci    unittests = get_unittests(args.path)
54425bb815Sopenharmony_ci    total = len(unittests)
55425bb815Sopenharmony_ci    if total == 0:
56425bb815Sopenharmony_ci        print("%s: no unit-* test to execute", args.path)
57425bb815Sopenharmony_ci        return 1
58425bb815Sopenharmony_ci
59425bb815Sopenharmony_ci    test_cmd = [args.runtime] if args.runtime else []
60425bb815Sopenharmony_ci
61425bb815Sopenharmony_ci    tested = 0
62425bb815Sopenharmony_ci    passed = 0
63425bb815Sopenharmony_ci    failed = 0
64425bb815Sopenharmony_ci    for test in unittests:
65425bb815Sopenharmony_ci        tested += 1
66425bb815Sopenharmony_ci        test_path = os.path.relpath(test)
67425bb815Sopenharmony_ci        try:
68425bb815Sopenharmony_ci            subprocess.check_output(test_cmd + [test], stderr=subprocess.STDOUT, universal_newlines=True)
69425bb815Sopenharmony_ci            passed += 1
70425bb815Sopenharmony_ci            if not args.quiet:
71425bb815Sopenharmony_ci                util.print_test_result(tested, total, True, 'PASS', test_path)
72425bb815Sopenharmony_ci        except subprocess.CalledProcessError as err:
73425bb815Sopenharmony_ci            failed += 1
74425bb815Sopenharmony_ci            util.print_test_result(tested, total, False, 'FAIL (%d)' % err.returncode, test_path)
75425bb815Sopenharmony_ci            print("================================================")
76425bb815Sopenharmony_ci            print(err.output)
77425bb815Sopenharmony_ci            print("================================================")
78425bb815Sopenharmony_ci
79425bb815Sopenharmony_ci    util.print_test_summary(os.path.join(os.path.relpath(args.path), "unit-*"), total, passed, failed)
80425bb815Sopenharmony_ci
81425bb815Sopenharmony_ci    if failed > 0:
82425bb815Sopenharmony_ci        return 1
83425bb815Sopenharmony_ci    return 0
84425bb815Sopenharmony_ci
85425bb815Sopenharmony_ci
86425bb815Sopenharmony_ciif __name__ == "__main__":
87425bb815Sopenharmony_ci    sys.exit(main(get_arguments()))
88