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