Clone of official tools
test.py@0:66f3b5499f7f, 2016-05-19 (annotated)
- Committer:
- screamer
- Date:
- Thu May 19 19:44:41 2016 +0100
- Revision:
- 0:66f3b5499f7f
- Child:
- 13:ab47a20b66f0
Initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | #! /usr/bin/env python2 |
screamer | 0:66f3b5499f7f | 2 | """ |
screamer | 0:66f3b5499f7f | 3 | mbed SDK |
screamer | 0:66f3b5499f7f | 4 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 5 | |
screamer | 0:66f3b5499f7f | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 7 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 8 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 9 | |
screamer | 0:66f3b5499f7f | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 11 | |
screamer | 0:66f3b5499f7f | 12 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 15 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 16 | limitations under the License. |
screamer | 0:66f3b5499f7f | 17 | |
screamer | 0:66f3b5499f7f | 18 | |
screamer | 0:66f3b5499f7f | 19 | TEST BUILD & RUN |
screamer | 0:66f3b5499f7f | 20 | """ |
screamer | 0:66f3b5499f7f | 21 | import sys |
screamer | 0:66f3b5499f7f | 22 | import os |
screamer | 0:66f3b5499f7f | 23 | import json |
screamer | 0:66f3b5499f7f | 24 | |
screamer | 0:66f3b5499f7f | 25 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
screamer | 0:66f3b5499f7f | 26 | sys.path.insert(0, ROOT) |
screamer | 0:66f3b5499f7f | 27 | |
screamer | 0:66f3b5499f7f | 28 | from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_build |
screamer | 0:66f3b5499f7f | 29 | from tools.options import get_default_options_parser |
screamer | 0:66f3b5499f7f | 30 | from tools.build_api import build_project, build_library |
screamer | 0:66f3b5499f7f | 31 | from tools.targets import TARGET_MAP |
screamer | 0:66f3b5499f7f | 32 | from tools.utils import mkdir |
screamer | 0:66f3b5499f7f | 33 | |
screamer | 0:66f3b5499f7f | 34 | if __name__ == '__main__': |
screamer | 0:66f3b5499f7f | 35 | try: |
screamer | 0:66f3b5499f7f | 36 | # Parse Options |
screamer | 0:66f3b5499f7f | 37 | parser = get_default_options_parser() |
screamer | 0:66f3b5499f7f | 38 | |
screamer | 0:66f3b5499f7f | 39 | parser.add_option("-j", "--jobs", |
screamer | 0:66f3b5499f7f | 40 | type="int", |
screamer | 0:66f3b5499f7f | 41 | dest="jobs", |
screamer | 0:66f3b5499f7f | 42 | default=1, |
screamer | 0:66f3b5499f7f | 43 | help="Number of concurrent jobs (default 1). Use 0 for auto based on host machine's number of CPUs") |
screamer | 0:66f3b5499f7f | 44 | |
screamer | 0:66f3b5499f7f | 45 | parser.add_option("--source", dest="source_dir", |
screamer | 0:66f3b5499f7f | 46 | default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append") |
screamer | 0:66f3b5499f7f | 47 | |
screamer | 0:66f3b5499f7f | 48 | parser.add_option("--build", dest="build_dir", |
screamer | 0:66f3b5499f7f | 49 | default=None, help="The build (output) directory") |
screamer | 0:66f3b5499f7f | 50 | |
screamer | 0:66f3b5499f7f | 51 | parser.add_option("-l", "--list", action="store_true", dest="list", |
screamer | 0:66f3b5499f7f | 52 | default=False, help="List (recursively) available tests in order and exit") |
screamer | 0:66f3b5499f7f | 53 | |
screamer | 0:66f3b5499f7f | 54 | parser.add_option("-p", "--paths", dest="paths", |
screamer | 0:66f3b5499f7f | 55 | default=None, help="Limit the tests to those within the specified comma separated list of paths") |
screamer | 0:66f3b5499f7f | 56 | |
screamer | 0:66f3b5499f7f | 57 | format_choices = ["list", "json"] |
screamer | 0:66f3b5499f7f | 58 | format_default_choice = "list" |
screamer | 0:66f3b5499f7f | 59 | format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice) |
screamer | 0:66f3b5499f7f | 60 | parser.add_option("-f", "--format", type="choice", dest="format", |
screamer | 0:66f3b5499f7f | 61 | choices=format_choices, default=format_default_choice, help=format_help) |
screamer | 0:66f3b5499f7f | 62 | |
screamer | 0:66f3b5499f7f | 63 | parser.add_option("-n", "--names", dest="names", |
screamer | 0:66f3b5499f7f | 64 | default=None, help="Limit the tests to a comma separated list of names") |
screamer | 0:66f3b5499f7f | 65 | |
screamer | 0:66f3b5499f7f | 66 | parser.add_option("--test-spec", dest="test_spec", |
screamer | 0:66f3b5499f7f | 67 | default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool") |
screamer | 0:66f3b5499f7f | 68 | |
screamer | 0:66f3b5499f7f | 69 | parser.add_option("-v", "--verbose", |
screamer | 0:66f3b5499f7f | 70 | action="store_true", |
screamer | 0:66f3b5499f7f | 71 | dest="verbose", |
screamer | 0:66f3b5499f7f | 72 | default=False, |
screamer | 0:66f3b5499f7f | 73 | help="Verbose diagnostic output") |
screamer | 0:66f3b5499f7f | 74 | |
screamer | 0:66f3b5499f7f | 75 | (options, args) = parser.parse_args() |
screamer | 0:66f3b5499f7f | 76 | |
screamer | 0:66f3b5499f7f | 77 | # Filter tests by path if specified |
screamer | 0:66f3b5499f7f | 78 | if options.paths: |
screamer | 0:66f3b5499f7f | 79 | all_paths = options.paths.split(",") |
screamer | 0:66f3b5499f7f | 80 | else: |
screamer | 0:66f3b5499f7f | 81 | all_paths = ["."] |
screamer | 0:66f3b5499f7f | 82 | |
screamer | 0:66f3b5499f7f | 83 | all_tests = {} |
screamer | 0:66f3b5499f7f | 84 | tests = {} |
screamer | 0:66f3b5499f7f | 85 | |
screamer | 0:66f3b5499f7f | 86 | # Find all tests in the relevant paths |
screamer | 0:66f3b5499f7f | 87 | for path in all_paths: |
screamer | 0:66f3b5499f7f | 88 | all_tests.update(find_tests(path)) |
screamer | 0:66f3b5499f7f | 89 | |
screamer | 0:66f3b5499f7f | 90 | # Filter tests by name if specified |
screamer | 0:66f3b5499f7f | 91 | if options.names: |
screamer | 0:66f3b5499f7f | 92 | all_names = options.names.split(",") |
screamer | 0:66f3b5499f7f | 93 | |
screamer | 0:66f3b5499f7f | 94 | all_tests_keys = all_tests.keys() |
screamer | 0:66f3b5499f7f | 95 | for name in all_names: |
screamer | 0:66f3b5499f7f | 96 | if name in all_tests_keys: |
screamer | 0:66f3b5499f7f | 97 | tests[name] = all_tests[name] |
screamer | 0:66f3b5499f7f | 98 | else: |
screamer | 0:66f3b5499f7f | 99 | print "[Warning] Test with name '%s' was not found in the available tests" % (name) |
screamer | 0:66f3b5499f7f | 100 | else: |
screamer | 0:66f3b5499f7f | 101 | tests = all_tests |
screamer | 0:66f3b5499f7f | 102 | |
screamer | 0:66f3b5499f7f | 103 | if options.list: |
screamer | 0:66f3b5499f7f | 104 | # Print available tests in order and exit |
screamer | 0:66f3b5499f7f | 105 | print_tests(tests, options.format) |
screamer | 0:66f3b5499f7f | 106 | else: |
screamer | 0:66f3b5499f7f | 107 | # Build all tests |
screamer | 0:66f3b5499f7f | 108 | if not options.build_dir: |
screamer | 0:66f3b5499f7f | 109 | print "[ERROR] You must specify a build path" |
screamer | 0:66f3b5499f7f | 110 | sys.exit(1) |
screamer | 0:66f3b5499f7f | 111 | |
screamer | 0:66f3b5499f7f | 112 | base_source_paths = options.source_dir |
screamer | 0:66f3b5499f7f | 113 | |
screamer | 0:66f3b5499f7f | 114 | # Default base source path is the current directory |
screamer | 0:66f3b5499f7f | 115 | if not base_source_paths: |
screamer | 0:66f3b5499f7f | 116 | base_source_paths = ['.'] |
screamer | 0:66f3b5499f7f | 117 | |
screamer | 0:66f3b5499f7f | 118 | |
screamer | 0:66f3b5499f7f | 119 | target = TARGET_MAP[options.mcu] |
screamer | 0:66f3b5499f7f | 120 | |
screamer | 0:66f3b5499f7f | 121 | lib_build_res = build_library(base_source_paths, options.build_dir, target, options.tool, |
screamer | 0:66f3b5499f7f | 122 | options=options.options, |
screamer | 0:66f3b5499f7f | 123 | jobs=options.jobs, |
screamer | 0:66f3b5499f7f | 124 | clean=options.clean, |
screamer | 0:66f3b5499f7f | 125 | archive=False) |
screamer | 0:66f3b5499f7f | 126 | |
screamer | 0:66f3b5499f7f | 127 | # Build all the tests |
screamer | 0:66f3b5499f7f | 128 | test_build = build_tests(tests, [options.build_dir], options.build_dir, target, options.tool, |
screamer | 0:66f3b5499f7f | 129 | options=options.options, |
screamer | 0:66f3b5499f7f | 130 | clean=options.clean, |
screamer | 0:66f3b5499f7f | 131 | jobs=options.jobs) |
screamer | 0:66f3b5499f7f | 132 | |
screamer | 0:66f3b5499f7f | 133 | # If a path to a test spec is provided, write it to a file |
screamer | 0:66f3b5499f7f | 134 | if options.test_spec: |
screamer | 0:66f3b5499f7f | 135 | test_spec_data = test_spec_from_test_build(test_build) |
screamer | 0:66f3b5499f7f | 136 | |
screamer | 0:66f3b5499f7f | 137 | # Create the target dir for the test spec if necessary |
screamer | 0:66f3b5499f7f | 138 | # mkdir will not create the dir if it already exists |
screamer | 0:66f3b5499f7f | 139 | test_spec_dir = os.path.dirname(options.test_spec) |
screamer | 0:66f3b5499f7f | 140 | if test_spec_dir: |
screamer | 0:66f3b5499f7f | 141 | mkdir(test_spec_dir) |
screamer | 0:66f3b5499f7f | 142 | |
screamer | 0:66f3b5499f7f | 143 | try: |
screamer | 0:66f3b5499f7f | 144 | with open(options.test_spec, 'w') as f: |
screamer | 0:66f3b5499f7f | 145 | f.write(json.dumps(test_spec_data, indent=2)) |
screamer | 0:66f3b5499f7f | 146 | except IOError, e: |
screamer | 0:66f3b5499f7f | 147 | print "[ERROR] Error writing test spec to file" |
screamer | 0:66f3b5499f7f | 148 | print e |
screamer | 0:66f3b5499f7f | 149 | |
screamer | 0:66f3b5499f7f | 150 | sys.exit() |
screamer | 0:66f3b5499f7f | 151 | |
screamer | 0:66f3b5499f7f | 152 | except KeyboardInterrupt, e: |
screamer | 0:66f3b5499f7f | 153 | print "\n[CTRL+c] exit" |
screamer | 0:66f3b5499f7f | 154 | except Exception,e: |
screamer | 0:66f3b5499f7f | 155 | import traceback |
screamer | 0:66f3b5499f7f | 156 | traceback.print_exc(file=sys.stdout) |
screamer | 0:66f3b5499f7f | 157 | print "[ERROR] %s" % str(e) |
screamer | 0:66f3b5499f7f | 158 | sys.exit(1) |