Clone of official tools
test.py@22:9e85236d8716, 2016-07-16 (annotated)
- Committer:
- screamer
- Date:
- Sat Jul 16 00:34:03 2016 +0100
- Revision:
- 22:9e85236d8716
- Parent:
- 13:ab47a20b66f0
- Child:
- 24:25bff2709c20
Sync with the mbedmicro/mbed tools
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 | 13:ab47a20b66f0 | 24 | import fnmatch |
screamer | 0:66f3b5499f7f | 25 | |
screamer | 0:66f3b5499f7f | 26 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
screamer | 0:66f3b5499f7f | 27 | sys.path.insert(0, ROOT) |
screamer | 0:66f3b5499f7f | 28 | |
screamer | 13:ab47a20b66f0 | 29 | from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds |
screamer | 0:66f3b5499f7f | 30 | from tools.options import get_default_options_parser |
screamer | 0:66f3b5499f7f | 31 | from tools.build_api import build_project, build_library |
screamer | 22:9e85236d8716 | 32 | from tools.build_api import print_build_memory_usage_results |
screamer | 0:66f3b5499f7f | 33 | from tools.targets import TARGET_MAP |
screamer | 13:ab47a20b66f0 | 34 | from tools.utils import mkdir, ToolException, NotSupportedException |
screamer | 13:ab47a20b66f0 | 35 | from tools.test_exporters import ReportExporter, ResultExporterType |
screamer | 22:9e85236d8716 | 36 | from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many |
screamer | 22:9e85236d8716 | 37 | from tools.toolchains import mbedToolchain |
screamer | 22:9e85236d8716 | 38 | from tools.settings import CLI_COLOR_MAP |
screamer | 0:66f3b5499f7f | 39 | |
screamer | 0:66f3b5499f7f | 40 | if __name__ == '__main__': |
screamer | 0:66f3b5499f7f | 41 | try: |
screamer | 0:66f3b5499f7f | 42 | # Parse Options |
screamer | 0:66f3b5499f7f | 43 | parser = get_default_options_parser() |
screamer | 0:66f3b5499f7f | 44 | |
screamer | 22:9e85236d8716 | 45 | parser.add_argument("-D", |
screamer | 13:ab47a20b66f0 | 46 | action="append", |
screamer | 13:ab47a20b66f0 | 47 | dest="macros", |
screamer | 13:ab47a20b66f0 | 48 | help="Add a macro definition") |
screamer | 22:9e85236d8716 | 49 | |
screamer | 22:9e85236d8716 | 50 | parser.add_argument("-j", "--jobs", |
screamer | 22:9e85236d8716 | 51 | type=int, |
screamer | 0:66f3b5499f7f | 52 | dest="jobs", |
screamer | 13:ab47a20b66f0 | 53 | default=0, |
screamer | 13:ab47a20b66f0 | 54 | help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") |
screamer | 0:66f3b5499f7f | 55 | |
screamer | 22:9e85236d8716 | 56 | parser.add_argument("--source", dest="source_dir", |
screamer | 22:9e85236d8716 | 57 | type=argparse_filestring_type, |
screamer | 22:9e85236d8716 | 58 | default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append") |
screamer | 0:66f3b5499f7f | 59 | |
screamer | 22:9e85236d8716 | 60 | parser.add_argument("--build", dest="build_dir", |
screamer | 0:66f3b5499f7f | 61 | default=None, help="The build (output) directory") |
screamer | 0:66f3b5499f7f | 62 | |
screamer | 22:9e85236d8716 | 63 | parser.add_argument("-l", "--list", action="store_true", dest="list", |
screamer | 0:66f3b5499f7f | 64 | default=False, help="List (recursively) available tests in order and exit") |
screamer | 0:66f3b5499f7f | 65 | |
screamer | 22:9e85236d8716 | 66 | parser.add_argument("-p", "--paths", dest="paths", |
screamer | 22:9e85236d8716 | 67 | type=argparse_many(argparse_filestring_type), |
screamer | 0:66f3b5499f7f | 68 | default=None, help="Limit the tests to those within the specified comma separated list of paths") |
screamer | 22:9e85236d8716 | 69 | |
screamer | 0:66f3b5499f7f | 70 | format_choices = ["list", "json"] |
screamer | 0:66f3b5499f7f | 71 | format_default_choice = "list" |
screamer | 0:66f3b5499f7f | 72 | format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice) |
screamer | 22:9e85236d8716 | 73 | parser.add_argument("-f", "--format", dest="format", |
screamer | 22:9e85236d8716 | 74 | type=argparse_lowercase_type(format_choices, "format"), |
screamer | 22:9e85236d8716 | 75 | default=format_default_choice, help=format_help) |
screamer | 0:66f3b5499f7f | 76 | |
screamer | 22:9e85236d8716 | 77 | parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail", |
screamer | 13:ab47a20b66f0 | 78 | default=None, help="Continue trying to build all tests if a build failure occurs") |
screamer | 13:ab47a20b66f0 | 79 | |
screamer | 22:9e85236d8716 | 80 | #TODO validate the names instead of just passing through str |
screamer | 22:9e85236d8716 | 81 | parser.add_argument("-n", "--names", dest="names", type=argparse_many(str), |
screamer | 0:66f3b5499f7f | 82 | default=None, help="Limit the tests to a comma separated list of names") |
screamer | 0:66f3b5499f7f | 83 | |
screamer | 22:9e85236d8716 | 84 | parser.add_argument("--test-spec", dest="test_spec", |
screamer | 0:66f3b5499f7f | 85 | default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool") |
screamer | 0:66f3b5499f7f | 86 | |
screamer | 22:9e85236d8716 | 87 | parser.add_argument("--build-report-junit", dest="build_report_junit", |
screamer | 13:ab47a20b66f0 | 88 | default=None, help="Destination path for a build report in the JUnit xml format") |
screamer | 13:ab47a20b66f0 | 89 | |
screamer | 22:9e85236d8716 | 90 | parser.add_argument("-v", "--verbose", |
screamer | 0:66f3b5499f7f | 91 | action="store_true", |
screamer | 0:66f3b5499f7f | 92 | dest="verbose", |
screamer | 0:66f3b5499f7f | 93 | default=False, |
screamer | 0:66f3b5499f7f | 94 | help="Verbose diagnostic output") |
screamer | 0:66f3b5499f7f | 95 | |
screamer | 22:9e85236d8716 | 96 | options = parser.parse_args() |
screamer | 0:66f3b5499f7f | 97 | |
screamer | 22:9e85236d8716 | 98 | # Filter tests by path if specified |
screamer | 0:66f3b5499f7f | 99 | if options.paths: |
screamer | 22:9e85236d8716 | 100 | all_paths = options.paths |
screamer | 0:66f3b5499f7f | 101 | else: |
screamer | 0:66f3b5499f7f | 102 | all_paths = ["."] |
screamer | 22:9e85236d8716 | 103 | |
screamer | 0:66f3b5499f7f | 104 | all_tests = {} |
screamer | 0:66f3b5499f7f | 105 | tests = {} |
screamer | 22:9e85236d8716 | 106 | |
screamer | 0:66f3b5499f7f | 107 | # Find all tests in the relevant paths |
screamer | 0:66f3b5499f7f | 108 | for path in all_paths: |
screamer | 0:66f3b5499f7f | 109 | all_tests.update(find_tests(path)) |
screamer | 22:9e85236d8716 | 110 | |
screamer | 0:66f3b5499f7f | 111 | # Filter tests by name if specified |
screamer | 0:66f3b5499f7f | 112 | if options.names: |
screamer | 22:9e85236d8716 | 113 | all_names = options.names |
screamer | 13:ab47a20b66f0 | 114 | all_names = [x.lower() for x in all_names] |
screamer | 22:9e85236d8716 | 115 | |
screamer | 0:66f3b5499f7f | 116 | for name in all_names: |
screamer | 13:ab47a20b66f0 | 117 | if any(fnmatch.fnmatch(testname, name) for testname in all_tests): |
screamer | 13:ab47a20b66f0 | 118 | for testname, test in all_tests.items(): |
screamer | 13:ab47a20b66f0 | 119 | if fnmatch.fnmatch(testname, name): |
screamer | 13:ab47a20b66f0 | 120 | tests[testname] = test |
screamer | 0:66f3b5499f7f | 121 | else: |
screamer | 0:66f3b5499f7f | 122 | print "[Warning] Test with name '%s' was not found in the available tests" % (name) |
screamer | 0:66f3b5499f7f | 123 | else: |
screamer | 0:66f3b5499f7f | 124 | tests = all_tests |
screamer | 0:66f3b5499f7f | 125 | |
screamer | 22:9e85236d8716 | 126 | if options.color: |
screamer | 22:9e85236d8716 | 127 | # This import happens late to prevent initializing colorization when we don't need it |
screamer | 22:9e85236d8716 | 128 | import colorize |
screamer | 22:9e85236d8716 | 129 | if options.verbose: |
screamer | 22:9e85236d8716 | 130 | notify = mbedToolchain.print_notify_verbose |
screamer | 22:9e85236d8716 | 131 | else: |
screamer | 22:9e85236d8716 | 132 | notify = mbedToolchain.print_notify |
screamer | 22:9e85236d8716 | 133 | notify = colorize.print_in_color_notifier(CLI_COLOR_MAP, notify) |
screamer | 22:9e85236d8716 | 134 | else: |
screamer | 22:9e85236d8716 | 135 | notify = None |
screamer | 22:9e85236d8716 | 136 | |
screamer | 0:66f3b5499f7f | 137 | if options.list: |
screamer | 0:66f3b5499f7f | 138 | # Print available tests in order and exit |
screamer | 0:66f3b5499f7f | 139 | print_tests(tests, options.format) |
screamer | 13:ab47a20b66f0 | 140 | sys.exit(0) |
screamer | 0:66f3b5499f7f | 141 | else: |
screamer | 0:66f3b5499f7f | 142 | # Build all tests |
screamer | 0:66f3b5499f7f | 143 | if not options.build_dir: |
screamer | 0:66f3b5499f7f | 144 | print "[ERROR] You must specify a build path" |
screamer | 0:66f3b5499f7f | 145 | sys.exit(1) |
screamer | 22:9e85236d8716 | 146 | |
screamer | 0:66f3b5499f7f | 147 | base_source_paths = options.source_dir |
screamer | 22:9e85236d8716 | 148 | |
screamer | 0:66f3b5499f7f | 149 | # Default base source path is the current directory |
screamer | 0:66f3b5499f7f | 150 | if not base_source_paths: |
screamer | 0:66f3b5499f7f | 151 | base_source_paths = ['.'] |
screamer | 0:66f3b5499f7f | 152 | |
screamer | 0:66f3b5499f7f | 153 | |
screamer | 22:9e85236d8716 | 154 | target = options.mcu[0] |
screamer | 0:66f3b5499f7f | 155 | |
screamer | 13:ab47a20b66f0 | 156 | build_report = {} |
screamer | 13:ab47a20b66f0 | 157 | build_properties = {} |
screamer | 13:ab47a20b66f0 | 158 | |
screamer | 13:ab47a20b66f0 | 159 | library_build_success = False |
screamer | 13:ab47a20b66f0 | 160 | try: |
screamer | 13:ab47a20b66f0 | 161 | # Build sources |
screamer | 22:9e85236d8716 | 162 | build_library(base_source_paths, options.build_dir, target, options.tool[0], |
screamer | 13:ab47a20b66f0 | 163 | options=options.options, |
screamer | 13:ab47a20b66f0 | 164 | jobs=options.jobs, |
screamer | 13:ab47a20b66f0 | 165 | clean=options.clean, |
screamer | 13:ab47a20b66f0 | 166 | report=build_report, |
screamer | 13:ab47a20b66f0 | 167 | properties=build_properties, |
screamer | 13:ab47a20b66f0 | 168 | name="mbed-build", |
screamer | 13:ab47a20b66f0 | 169 | macros=options.macros, |
screamer | 13:ab47a20b66f0 | 170 | verbose=options.verbose, |
screamer | 22:9e85236d8716 | 171 | notify=notify, |
screamer | 13:ab47a20b66f0 | 172 | archive=False) |
screamer | 22:9e85236d8716 | 173 | |
screamer | 13:ab47a20b66f0 | 174 | library_build_success = True |
screamer | 13:ab47a20b66f0 | 175 | except ToolException, e: |
screamer | 13:ab47a20b66f0 | 176 | # ToolException output is handled by the build log |
screamer | 13:ab47a20b66f0 | 177 | pass |
screamer | 13:ab47a20b66f0 | 178 | except NotSupportedException, e: |
screamer | 13:ab47a20b66f0 | 179 | # NotSupportedException is handled by the build log |
screamer | 13:ab47a20b66f0 | 180 | pass |
screamer | 13:ab47a20b66f0 | 181 | except Exception, e: |
screamer | 13:ab47a20b66f0 | 182 | # Some other exception occurred, print the error message |
screamer | 13:ab47a20b66f0 | 183 | print e |
screamer | 22:9e85236d8716 | 184 | |
screamer | 13:ab47a20b66f0 | 185 | if not library_build_success: |
screamer | 13:ab47a20b66f0 | 186 | print "Failed to build library" |
screamer | 13:ab47a20b66f0 | 187 | else: |
screamer | 13:ab47a20b66f0 | 188 | # Build all the tests |
screamer | 22:9e85236d8716 | 189 | test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, target, options.tool[0], |
screamer | 13:ab47a20b66f0 | 190 | options=options.options, |
screamer | 13:ab47a20b66f0 | 191 | clean=options.clean, |
screamer | 13:ab47a20b66f0 | 192 | report=build_report, |
screamer | 13:ab47a20b66f0 | 193 | properties=build_properties, |
screamer | 13:ab47a20b66f0 | 194 | macros=options.macros, |
screamer | 13:ab47a20b66f0 | 195 | verbose=options.verbose, |
screamer | 22:9e85236d8716 | 196 | notify=notify, |
screamer | 13:ab47a20b66f0 | 197 | jobs=options.jobs, |
screamer | 13:ab47a20b66f0 | 198 | continue_on_build_fail=options.continue_on_build_fail) |
screamer | 22:9e85236d8716 | 199 | |
screamer | 13:ab47a20b66f0 | 200 | # If a path to a test spec is provided, write it to a file |
screamer | 13:ab47a20b66f0 | 201 | if options.test_spec: |
screamer | 13:ab47a20b66f0 | 202 | test_spec_data = test_spec_from_test_builds(test_build) |
screamer | 22:9e85236d8716 | 203 | |
screamer | 13:ab47a20b66f0 | 204 | # Create the target dir for the test spec if necessary |
screamer | 13:ab47a20b66f0 | 205 | # mkdir will not create the dir if it already exists |
screamer | 13:ab47a20b66f0 | 206 | test_spec_dir = os.path.dirname(options.test_spec) |
screamer | 13:ab47a20b66f0 | 207 | if test_spec_dir: |
screamer | 13:ab47a20b66f0 | 208 | mkdir(test_spec_dir) |
screamer | 22:9e85236d8716 | 209 | |
screamer | 13:ab47a20b66f0 | 210 | try: |
screamer | 13:ab47a20b66f0 | 211 | with open(options.test_spec, 'w') as f: |
screamer | 13:ab47a20b66f0 | 212 | f.write(json.dumps(test_spec_data, indent=2)) |
screamer | 13:ab47a20b66f0 | 213 | except IOError, e: |
screamer | 13:ab47a20b66f0 | 214 | print "[ERROR] Error writing test spec to file" |
screamer | 13:ab47a20b66f0 | 215 | print e |
screamer | 22:9e85236d8716 | 216 | |
screamer | 13:ab47a20b66f0 | 217 | # If a path to a JUnit build report spec is provided, write it to a file |
screamer | 13:ab47a20b66f0 | 218 | if options.build_report_junit: |
screamer | 13:ab47a20b66f0 | 219 | report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build") |
screamer | 13:ab47a20b66f0 | 220 | report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties) |
screamer | 22:9e85236d8716 | 221 | |
screamer | 22:9e85236d8716 | 222 | # Print memory map summary on screen |
screamer | 22:9e85236d8716 | 223 | if build_report: |
screamer | 22:9e85236d8716 | 224 | |
screamer | 22:9e85236d8716 | 225 | print print_build_memory_usage_results(build_report) |
screamer | 22:9e85236d8716 | 226 | |
screamer | 13:ab47a20b66f0 | 227 | print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build") |
screamer | 13:ab47a20b66f0 | 228 | status = print_report_exporter.report(build_report) |
screamer | 22:9e85236d8716 | 229 | |
screamer | 13:ab47a20b66f0 | 230 | if status: |
screamer | 13:ab47a20b66f0 | 231 | sys.exit(0) |
screamer | 13:ab47a20b66f0 | 232 | else: |
screamer | 13:ab47a20b66f0 | 233 | sys.exit(1) |
screamer | 0:66f3b5499f7f | 234 | |
screamer | 0:66f3b5499f7f | 235 | except KeyboardInterrupt, e: |
screamer | 0:66f3b5499f7f | 236 | print "\n[CTRL+c] exit" |
screamer | 0:66f3b5499f7f | 237 | except Exception,e: |
screamer | 0:66f3b5499f7f | 238 | import traceback |
screamer | 0:66f3b5499f7f | 239 | traceback.print_exc(file=sys.stdout) |
screamer | 0:66f3b5499f7f | 240 | print "[ERROR] %s" % str(e) |
screamer | 0:66f3b5499f7f | 241 | sys.exit(1) |