Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
tools/test.py@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | #! /usr/bin/env python2 |
switches | 0:5c4d7b2438d3 | 2 | """ |
switches | 0:5c4d7b2438d3 | 3 | mbed SDK |
switches | 0:5c4d7b2438d3 | 4 | Copyright (c) 2011-2013 ARM Limited |
switches | 0:5c4d7b2438d3 | 5 | |
switches | 0:5c4d7b2438d3 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:5c4d7b2438d3 | 7 | you may not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 8 | You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 9 | |
switches | 0:5c4d7b2438d3 | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 11 | |
switches | 0:5c4d7b2438d3 | 12 | Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:5c4d7b2438d3 | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 15 | See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 16 | limitations under the License. |
switches | 0:5c4d7b2438d3 | 17 | |
switches | 0:5c4d7b2438d3 | 18 | |
switches | 0:5c4d7b2438d3 | 19 | TEST BUILD & RUN |
switches | 0:5c4d7b2438d3 | 20 | """ |
switches | 0:5c4d7b2438d3 | 21 | import sys |
switches | 0:5c4d7b2438d3 | 22 | import os |
switches | 0:5c4d7b2438d3 | 23 | import json |
switches | 0:5c4d7b2438d3 | 24 | import fnmatch |
switches | 0:5c4d7b2438d3 | 25 | |
switches | 0:5c4d7b2438d3 | 26 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
switches | 0:5c4d7b2438d3 | 27 | sys.path.insert(0, ROOT) |
switches | 0:5c4d7b2438d3 | 28 | |
switches | 0:5c4d7b2438d3 | 29 | from tools.config import ConfigException |
switches | 0:5c4d7b2438d3 | 30 | from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds |
switches | 0:5c4d7b2438d3 | 31 | from tools.options import get_default_options_parser, extract_profile |
switches | 0:5c4d7b2438d3 | 32 | from tools.build_api import build_project, build_library |
switches | 0:5c4d7b2438d3 | 33 | from tools.build_api import print_build_memory_usage |
switches | 0:5c4d7b2438d3 | 34 | from tools.targets import TARGET_MAP |
switches | 0:5c4d7b2438d3 | 35 | from tools.utils import mkdir, ToolException, NotSupportedException, args_error |
switches | 0:5c4d7b2438d3 | 36 | from tools.test_exporters import ReportExporter, ResultExporterType |
switches | 0:5c4d7b2438d3 | 37 | from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many |
switches | 0:5c4d7b2438d3 | 38 | from utils import argparse_dir_not_parent |
switches | 0:5c4d7b2438d3 | 39 | from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS, TOOLCHAIN_CLASSES |
switches | 0:5c4d7b2438d3 | 40 | from tools.settings import CLI_COLOR_MAP |
switches | 0:5c4d7b2438d3 | 41 | |
switches | 0:5c4d7b2438d3 | 42 | if __name__ == '__main__': |
switches | 0:5c4d7b2438d3 | 43 | try: |
switches | 0:5c4d7b2438d3 | 44 | # Parse Options |
switches | 0:5c4d7b2438d3 | 45 | parser = get_default_options_parser(add_app_config=True) |
switches | 0:5c4d7b2438d3 | 46 | |
switches | 0:5c4d7b2438d3 | 47 | parser.add_argument("-D", |
switches | 0:5c4d7b2438d3 | 48 | action="append", |
switches | 0:5c4d7b2438d3 | 49 | dest="macros", |
switches | 0:5c4d7b2438d3 | 50 | help="Add a macro definition") |
switches | 0:5c4d7b2438d3 | 51 | |
switches | 0:5c4d7b2438d3 | 52 | parser.add_argument("-j", "--jobs", |
switches | 0:5c4d7b2438d3 | 53 | type=int, |
switches | 0:5c4d7b2438d3 | 54 | dest="jobs", |
switches | 0:5c4d7b2438d3 | 55 | default=0, |
switches | 0:5c4d7b2438d3 | 56 | help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") |
switches | 0:5c4d7b2438d3 | 57 | |
switches | 0:5c4d7b2438d3 | 58 | parser.add_argument("--source", dest="source_dir", |
switches | 0:5c4d7b2438d3 | 59 | type=argparse_filestring_type, |
switches | 0:5c4d7b2438d3 | 60 | default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append") |
switches | 0:5c4d7b2438d3 | 61 | |
switches | 0:5c4d7b2438d3 | 62 | parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT), |
switches | 0:5c4d7b2438d3 | 63 | default=None, help="The build (output) directory") |
switches | 0:5c4d7b2438d3 | 64 | |
switches | 0:5c4d7b2438d3 | 65 | parser.add_argument("-l", "--list", action="store_true", dest="list", |
switches | 0:5c4d7b2438d3 | 66 | default=False, help="List (recursively) available tests in order and exit") |
switches | 0:5c4d7b2438d3 | 67 | |
switches | 0:5c4d7b2438d3 | 68 | parser.add_argument("-p", "--paths", dest="paths", |
switches | 0:5c4d7b2438d3 | 69 | type=argparse_many(argparse_filestring_type), |
switches | 0:5c4d7b2438d3 | 70 | default=None, help="Limit the tests to those within the specified comma separated list of paths") |
switches | 0:5c4d7b2438d3 | 71 | |
switches | 0:5c4d7b2438d3 | 72 | format_choices = ["list", "json"] |
switches | 0:5c4d7b2438d3 | 73 | format_default_choice = "list" |
switches | 0:5c4d7b2438d3 | 74 | format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice) |
switches | 0:5c4d7b2438d3 | 75 | parser.add_argument("-f", "--format", dest="format", |
switches | 0:5c4d7b2438d3 | 76 | type=argparse_lowercase_type(format_choices, "format"), |
switches | 0:5c4d7b2438d3 | 77 | default=format_default_choice, help=format_help) |
switches | 0:5c4d7b2438d3 | 78 | |
switches | 0:5c4d7b2438d3 | 79 | parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail", |
switches | 0:5c4d7b2438d3 | 80 | default=None, help="Continue trying to build all tests if a build failure occurs") |
switches | 0:5c4d7b2438d3 | 81 | |
switches | 0:5c4d7b2438d3 | 82 | #TODO validate the names instead of just passing through str |
switches | 0:5c4d7b2438d3 | 83 | parser.add_argument("-n", "--names", dest="names", type=argparse_many(str), |
switches | 0:5c4d7b2438d3 | 84 | default=None, help="Limit the tests to a comma separated list of names") |
switches | 0:5c4d7b2438d3 | 85 | |
switches | 0:5c4d7b2438d3 | 86 | parser.add_argument("--test-spec", dest="test_spec", |
switches | 0:5c4d7b2438d3 | 87 | default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool") |
switches | 0:5c4d7b2438d3 | 88 | |
switches | 0:5c4d7b2438d3 | 89 | parser.add_argument("--build-report-junit", dest="build_report_junit", |
switches | 0:5c4d7b2438d3 | 90 | default=None, help="Destination path for a build report in the JUnit xml format") |
switches | 0:5c4d7b2438d3 | 91 | |
switches | 0:5c4d7b2438d3 | 92 | parser.add_argument("-v", "--verbose", |
switches | 0:5c4d7b2438d3 | 93 | action="store_true", |
switches | 0:5c4d7b2438d3 | 94 | dest="verbose", |
switches | 0:5c4d7b2438d3 | 95 | default=False, |
switches | 0:5c4d7b2438d3 | 96 | help="Verbose diagnostic output") |
switches | 0:5c4d7b2438d3 | 97 | |
switches | 0:5c4d7b2438d3 | 98 | options = parser.parse_args() |
switches | 0:5c4d7b2438d3 | 99 | |
switches | 0:5c4d7b2438d3 | 100 | # Filter tests by path if specified |
switches | 0:5c4d7b2438d3 | 101 | if options.paths: |
switches | 0:5c4d7b2438d3 | 102 | all_paths = options.paths |
switches | 0:5c4d7b2438d3 | 103 | else: |
switches | 0:5c4d7b2438d3 | 104 | all_paths = ["."] |
switches | 0:5c4d7b2438d3 | 105 | |
switches | 0:5c4d7b2438d3 | 106 | all_tests = {} |
switches | 0:5c4d7b2438d3 | 107 | tests = {} |
switches | 0:5c4d7b2438d3 | 108 | |
switches | 0:5c4d7b2438d3 | 109 | # Target |
switches | 0:5c4d7b2438d3 | 110 | if options.mcu is None : |
switches | 0:5c4d7b2438d3 | 111 | args_error(parser, "argument -m/--mcu is required") |
switches | 0:5c4d7b2438d3 | 112 | mcu = options.mcu[0] |
switches | 0:5c4d7b2438d3 | 113 | |
switches | 0:5c4d7b2438d3 | 114 | # Toolchain |
switches | 0:5c4d7b2438d3 | 115 | if options.tool is None: |
switches | 0:5c4d7b2438d3 | 116 | args_error(parser, "argument -t/--tool is required") |
switches | 0:5c4d7b2438d3 | 117 | toolchain = options.tool[0] |
switches | 0:5c4d7b2438d3 | 118 | |
switches | 0:5c4d7b2438d3 | 119 | if not TOOLCHAIN_CLASSES[toolchain].check_executable(): |
switches | 0:5c4d7b2438d3 | 120 | search_path = TOOLCHAIN_PATHS[toolchain] or "No path set" |
switches | 0:5c4d7b2438d3 | 121 | args_error(parser, "Could not find executable for %s.\n" |
switches | 0:5c4d7b2438d3 | 122 | "Currently set search path: %s" |
switches | 0:5c4d7b2438d3 | 123 | % (toolchain, search_path)) |
switches | 0:5c4d7b2438d3 | 124 | |
switches | 0:5c4d7b2438d3 | 125 | # Find all tests in the relevant paths |
switches | 0:5c4d7b2438d3 | 126 | for path in all_paths: |
switches | 0:5c4d7b2438d3 | 127 | all_tests.update(find_tests(path, mcu, toolchain, |
switches | 0:5c4d7b2438d3 | 128 | app_config=options.app_config)) |
switches | 0:5c4d7b2438d3 | 129 | |
switches | 0:5c4d7b2438d3 | 130 | # Filter tests by name if specified |
switches | 0:5c4d7b2438d3 | 131 | if options.names: |
switches | 0:5c4d7b2438d3 | 132 | all_names = options.names |
switches | 0:5c4d7b2438d3 | 133 | all_names = [x.lower() for x in all_names] |
switches | 0:5c4d7b2438d3 | 134 | |
switches | 0:5c4d7b2438d3 | 135 | for name in all_names: |
switches | 0:5c4d7b2438d3 | 136 | if any(fnmatch.fnmatch(testname, name) for testname in all_tests): |
switches | 0:5c4d7b2438d3 | 137 | for testname, test in all_tests.items(): |
switches | 0:5c4d7b2438d3 | 138 | if fnmatch.fnmatch(testname, name): |
switches | 0:5c4d7b2438d3 | 139 | tests[testname] = test |
switches | 0:5c4d7b2438d3 | 140 | else: |
switches | 0:5c4d7b2438d3 | 141 | print "[Warning] Test with name '%s' was not found in the available tests" % (name) |
switches | 0:5c4d7b2438d3 | 142 | else: |
switches | 0:5c4d7b2438d3 | 143 | tests = all_tests |
switches | 0:5c4d7b2438d3 | 144 | |
switches | 0:5c4d7b2438d3 | 145 | if options.color: |
switches | 0:5c4d7b2438d3 | 146 | # This import happens late to prevent initializing colorization when we don't need it |
switches | 0:5c4d7b2438d3 | 147 | import colorize |
switches | 0:5c4d7b2438d3 | 148 | if options.verbose: |
switches | 0:5c4d7b2438d3 | 149 | notify = mbedToolchain.print_notify_verbose |
switches | 0:5c4d7b2438d3 | 150 | else: |
switches | 0:5c4d7b2438d3 | 151 | notify = mbedToolchain.print_notify |
switches | 0:5c4d7b2438d3 | 152 | notify = colorize.print_in_color_notifier(CLI_COLOR_MAP, notify) |
switches | 0:5c4d7b2438d3 | 153 | else: |
switches | 0:5c4d7b2438d3 | 154 | notify = None |
switches | 0:5c4d7b2438d3 | 155 | |
switches | 0:5c4d7b2438d3 | 156 | if options.list: |
switches | 0:5c4d7b2438d3 | 157 | # Print available tests in order and exit |
switches | 0:5c4d7b2438d3 | 158 | print_tests(tests, options.format) |
switches | 0:5c4d7b2438d3 | 159 | sys.exit(0) |
switches | 0:5c4d7b2438d3 | 160 | else: |
switches | 0:5c4d7b2438d3 | 161 | # Build all tests |
switches | 0:5c4d7b2438d3 | 162 | if not options.build_dir: |
switches | 0:5c4d7b2438d3 | 163 | args_error(parser, "argument --build is required") |
switches | 0:5c4d7b2438d3 | 164 | |
switches | 0:5c4d7b2438d3 | 165 | base_source_paths = options.source_dir |
switches | 0:5c4d7b2438d3 | 166 | |
switches | 0:5c4d7b2438d3 | 167 | # Default base source path is the current directory |
switches | 0:5c4d7b2438d3 | 168 | if not base_source_paths: |
switches | 0:5c4d7b2438d3 | 169 | base_source_paths = ['.'] |
switches | 0:5c4d7b2438d3 | 170 | |
switches | 0:5c4d7b2438d3 | 171 | build_report = {} |
switches | 0:5c4d7b2438d3 | 172 | build_properties = {} |
switches | 0:5c4d7b2438d3 | 173 | |
switches | 0:5c4d7b2438d3 | 174 | library_build_success = False |
switches | 0:5c4d7b2438d3 | 175 | profile = extract_profile(parser, options, toolchain) |
switches | 0:5c4d7b2438d3 | 176 | try: |
switches | 0:5c4d7b2438d3 | 177 | # Build sources |
switches | 0:5c4d7b2438d3 | 178 | build_library(base_source_paths, options.build_dir, mcu, toolchain, |
switches | 0:5c4d7b2438d3 | 179 | jobs=options.jobs, |
switches | 0:5c4d7b2438d3 | 180 | clean=options.clean, |
switches | 0:5c4d7b2438d3 | 181 | report=build_report, |
switches | 0:5c4d7b2438d3 | 182 | properties=build_properties, |
switches | 0:5c4d7b2438d3 | 183 | name="mbed-build", |
switches | 0:5c4d7b2438d3 | 184 | macros=options.macros, |
switches | 0:5c4d7b2438d3 | 185 | verbose=options.verbose, |
switches | 0:5c4d7b2438d3 | 186 | notify=notify, |
switches | 0:5c4d7b2438d3 | 187 | archive=False, |
switches | 0:5c4d7b2438d3 | 188 | app_config=options.app_config, |
switches | 0:5c4d7b2438d3 | 189 | build_profile=profile) |
switches | 0:5c4d7b2438d3 | 190 | |
switches | 0:5c4d7b2438d3 | 191 | library_build_success = True |
switches | 0:5c4d7b2438d3 | 192 | except ToolException, e: |
switches | 0:5c4d7b2438d3 | 193 | # ToolException output is handled by the build log |
switches | 0:5c4d7b2438d3 | 194 | pass |
switches | 0:5c4d7b2438d3 | 195 | except NotSupportedException, e: |
switches | 0:5c4d7b2438d3 | 196 | # NotSupportedException is handled by the build log |
switches | 0:5c4d7b2438d3 | 197 | pass |
switches | 0:5c4d7b2438d3 | 198 | except Exception, e: |
switches | 0:5c4d7b2438d3 | 199 | # Some other exception occurred, print the error message |
switches | 0:5c4d7b2438d3 | 200 | print e |
switches | 0:5c4d7b2438d3 | 201 | |
switches | 0:5c4d7b2438d3 | 202 | if not library_build_success: |
switches | 0:5c4d7b2438d3 | 203 | print "Failed to build library" |
switches | 0:5c4d7b2438d3 | 204 | else: |
switches | 0:5c4d7b2438d3 | 205 | # Build all the tests |
switches | 0:5c4d7b2438d3 | 206 | |
switches | 0:5c4d7b2438d3 | 207 | test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, mcu, toolchain, |
switches | 0:5c4d7b2438d3 | 208 | clean=options.clean, |
switches | 0:5c4d7b2438d3 | 209 | report=build_report, |
switches | 0:5c4d7b2438d3 | 210 | properties=build_properties, |
switches | 0:5c4d7b2438d3 | 211 | macros=options.macros, |
switches | 0:5c4d7b2438d3 | 212 | verbose=options.verbose, |
switches | 0:5c4d7b2438d3 | 213 | notify=notify, |
switches | 0:5c4d7b2438d3 | 214 | jobs=options.jobs, |
switches | 0:5c4d7b2438d3 | 215 | continue_on_build_fail=options.continue_on_build_fail, |
switches | 0:5c4d7b2438d3 | 216 | app_config=options.app_config, |
switches | 0:5c4d7b2438d3 | 217 | build_profile=profile) |
switches | 0:5c4d7b2438d3 | 218 | |
switches | 0:5c4d7b2438d3 | 219 | # If a path to a test spec is provided, write it to a file |
switches | 0:5c4d7b2438d3 | 220 | if options.test_spec: |
switches | 0:5c4d7b2438d3 | 221 | test_spec_data = test_spec_from_test_builds(test_build) |
switches | 0:5c4d7b2438d3 | 222 | |
switches | 0:5c4d7b2438d3 | 223 | # Create the target dir for the test spec if necessary |
switches | 0:5c4d7b2438d3 | 224 | # mkdir will not create the dir if it already exists |
switches | 0:5c4d7b2438d3 | 225 | test_spec_dir = os.path.dirname(options.test_spec) |
switches | 0:5c4d7b2438d3 | 226 | if test_spec_dir: |
switches | 0:5c4d7b2438d3 | 227 | mkdir(test_spec_dir) |
switches | 0:5c4d7b2438d3 | 228 | |
switches | 0:5c4d7b2438d3 | 229 | try: |
switches | 0:5c4d7b2438d3 | 230 | with open(options.test_spec, 'w') as f: |
switches | 0:5c4d7b2438d3 | 231 | f.write(json.dumps(test_spec_data, indent=2)) |
switches | 0:5c4d7b2438d3 | 232 | except IOError, e: |
switches | 0:5c4d7b2438d3 | 233 | print "[ERROR] Error writing test spec to file" |
switches | 0:5c4d7b2438d3 | 234 | print e |
switches | 0:5c4d7b2438d3 | 235 | |
switches | 0:5c4d7b2438d3 | 236 | # If a path to a JUnit build report spec is provided, write it to a file |
switches | 0:5c4d7b2438d3 | 237 | if options.build_report_junit: |
switches | 0:5c4d7b2438d3 | 238 | report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build") |
switches | 0:5c4d7b2438d3 | 239 | report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties) |
switches | 0:5c4d7b2438d3 | 240 | |
switches | 0:5c4d7b2438d3 | 241 | # Print memory map summary on screen |
switches | 0:5c4d7b2438d3 | 242 | if build_report: |
switches | 0:5c4d7b2438d3 | 243 | |
switches | 0:5c4d7b2438d3 | 244 | print print_build_memory_usage(build_report) |
switches | 0:5c4d7b2438d3 | 245 | |
switches | 0:5c4d7b2438d3 | 246 | print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build") |
switches | 0:5c4d7b2438d3 | 247 | status = print_report_exporter.report(build_report) |
switches | 0:5c4d7b2438d3 | 248 | |
switches | 0:5c4d7b2438d3 | 249 | if status: |
switches | 0:5c4d7b2438d3 | 250 | sys.exit(0) |
switches | 0:5c4d7b2438d3 | 251 | else: |
switches | 0:5c4d7b2438d3 | 252 | sys.exit(1) |
switches | 0:5c4d7b2438d3 | 253 | |
switches | 0:5c4d7b2438d3 | 254 | except KeyboardInterrupt, e: |
switches | 0:5c4d7b2438d3 | 255 | print "\n[CTRL+c] exit" |
switches | 0:5c4d7b2438d3 | 256 | except ConfigException, e: |
switches | 0:5c4d7b2438d3 | 257 | # Catching ConfigException here to prevent a traceback |
switches | 0:5c4d7b2438d3 | 258 | print "[ERROR] %s" % str(e) |
switches | 0:5c4d7b2438d3 | 259 | except Exception,e: |
switches | 0:5c4d7b2438d3 | 260 | import traceback |
switches | 0:5c4d7b2438d3 | 261 | traceback.print_exc(file=sys.stdout) |
switches | 0:5c4d7b2438d3 | 262 | print "[ERROR] %s" % str(e) |
switches | 0:5c4d7b2438d3 | 263 | sys.exit(1) |