joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test.py Source File

test.py

00001 #! /usr/bin/env python2
00002 """
00003 mbed SDK
00004 Copyright (c) 2011-2013 ARM Limited
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010     http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017 
00018 
00019 TEST BUILD & RUN
00020 """
00021 import sys
00022 import os
00023 import json
00024 import fnmatch
00025 
00026 ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
00027 sys.path.insert(0, ROOT)
00028 
00029 from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds
00030 from tools.options import get_default_options_parser
00031 from tools.build_api import build_project, build_library
00032 from tools.build_api import print_build_memory_usage
00033 from tools.targets import TARGET_MAP
00034 from tools.utils import mkdir, ToolException, NotSupportedException, args_error
00035 from tools.test_exporters import ReportExporter, ResultExporterType
00036 from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many
00037 from utils import argparse_dir_not_parent
00038 from tools.toolchains import mbedToolchain
00039 from tools.settings import CLI_COLOR_MAP
00040 
00041 if __name__ == '__main__':
00042     try:
00043         # Parse Options
00044         parser = get_default_options_parser(add_app_config=True)
00045         
00046         parser.add_argument("-D",
00047                           action="append",
00048                           dest="macros",
00049                           help="Add a macro definition")
00050        
00051         parser.add_argument("-j", "--jobs",
00052                           type=int,
00053                           dest="jobs",
00054                           default=0,
00055                           help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
00056 
00057         parser.add_argument("--source", dest="source_dir",
00058                           type=argparse_filestring_type,
00059                             default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
00060 
00061         parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
00062                           default=None, help="The build (output) directory")
00063 
00064         parser.add_argument("-l", "--list", action="store_true", dest="list",
00065                           default=False, help="List (recursively) available tests in order and exit")
00066 
00067         parser.add_argument("-p", "--paths", dest="paths",
00068                           type=argparse_many(argparse_filestring_type),
00069                           default=None, help="Limit the tests to those within the specified comma separated list of paths")
00070 
00071         format_choices = ["list", "json"]
00072         format_default_choice = "list"
00073         format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice)
00074         parser.add_argument("-f", "--format", dest="format",
00075                             type=argparse_lowercase_type(format_choices, "format"),
00076                             default=format_default_choice, help=format_help)
00077         
00078         parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail",
00079                           default=None, help="Continue trying to build all tests if a build failure occurs")
00080 
00081         #TODO validate the names instead of just passing through str
00082         parser.add_argument("-n", "--names", dest="names", type=argparse_many(str),
00083                           default=None, help="Limit the tests to a comma separated list of names")
00084                           
00085         parser.add_argument("--test-spec", dest="test_spec",
00086                           default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool")
00087         
00088         parser.add_argument("--build-report-junit", dest="build_report_junit",
00089                           default=None, help="Destination path for a build report in the JUnit xml format")
00090         
00091         parser.add_argument("-v", "--verbose",
00092                           action="store_true",
00093                           dest="verbose",
00094                           default=False,
00095                           help="Verbose diagnostic output")
00096 
00097         options = parser.parse_args()
00098 
00099         # Filter tests by path if specified
00100         if options.paths:
00101             all_paths = options.paths
00102         else:
00103             all_paths = ["."]
00104 
00105         all_tests = {}
00106         tests = {}
00107 
00108         # Target
00109         if options.mcu is None :
00110             args_error(parser, "argument -m/--mcu is required")
00111         mcu = options.mcu[0]
00112 
00113         # Toolchain
00114         if options.tool is None:
00115             args_error(parser, "argument -t/--tool is required")
00116         toolchain = options.tool[0]
00117 
00118         # Find all tests in the relevant paths
00119         for path in all_paths:
00120             all_tests.update(find_tests(path, mcu, toolchain, options.options,
00121                                         app_config=options.app_config))
00122 
00123         # Filter tests by name if specified
00124         if options.names:
00125             all_names = options.names
00126             all_names = [x.lower() for x in all_names]
00127 
00128             for name in all_names:
00129                 if any(fnmatch.fnmatch(testname, name) for testname in all_tests):
00130                     for testname, test in all_tests.items():
00131                         if fnmatch.fnmatch(testname, name):
00132                             tests[testname] = test
00133                 else:
00134                     print "[Warning] Test with name '%s' was not found in the available tests" % (name)
00135         else:
00136             tests = all_tests
00137 
00138         if options.color:
00139             # This import happens late to prevent initializing colorization when we don't need it
00140             import colorize
00141             if options.verbose:
00142                 notify = mbedToolchain.print_notify_verbose
00143             else:
00144                 notify = mbedToolchain.print_notify
00145             notify = colorize.print_in_color_notifier(CLI_COLOR_MAP, notify)
00146         else:
00147             notify = None
00148 
00149         if options.list:
00150             # Print available tests in order and exit
00151             print_tests(tests, options.format)
00152             sys.exit(0)
00153         else:
00154             # Build all tests
00155             if not options.build_dir:
00156                 args_error(parser, "argument --build is required")
00157 
00158             base_source_paths = options.source_dir
00159 
00160             # Default base source path is the current directory
00161             if not base_source_paths:
00162                 base_source_paths = ['.']
00163             
00164             build_report = {}
00165             build_properties = {}
00166 
00167             library_build_success = False
00168             try:
00169                 # Build sources
00170                 build_library(base_source_paths, options.build_dir, mcu, toolchain,
00171                                                 options=options.options,
00172                                                 jobs=options.jobs,
00173                                                 clean=options.clean,
00174                                                 report=build_report,
00175                                                 properties=build_properties,
00176                                                 name="mbed-build",
00177                                                 macros=options.macros,
00178                                                 verbose=options.verbose,
00179                                                 notify=notify,
00180                                                 archive=False,
00181                                                 remove_config_header_file=True,
00182                                                 app_config=options.app_config)
00183 
00184                 library_build_success = True
00185             except ToolException, e:
00186                 # ToolException output is handled by the build log
00187                 pass
00188             except NotSupportedException, e:
00189                 # NotSupportedException is handled by the build log
00190                 pass
00191             except Exception, e:
00192                 # Some other exception occurred, print the error message
00193                 print e
00194 
00195             if not library_build_success:
00196                 print "Failed to build library"
00197             else:
00198                 # Build all the tests
00199                 test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, mcu, toolchain,
00200                         options=options.options,
00201                         clean=options.clean,
00202                         report=build_report,
00203                         properties=build_properties,
00204                         macros=options.macros,
00205                         verbose=options.verbose,
00206                         notify=notify,
00207                         jobs=options.jobs,
00208                         continue_on_build_fail=options.continue_on_build_fail,
00209                         app_config=options.app_config)
00210 
00211                 # If a path to a test spec is provided, write it to a file
00212                 if options.test_spec:
00213                     test_spec_data = test_spec_from_test_builds(test_build)
00214 
00215                     # Create the target dir for the test spec if necessary
00216                     # mkdir will not create the dir if it already exists
00217                     test_spec_dir = os.path.dirname(options.test_spec)
00218                     if test_spec_dir:
00219                         mkdir(test_spec_dir)
00220 
00221                     try:
00222                         with open(options.test_spec, 'w') as f:
00223                             f.write(json.dumps(test_spec_data, indent=2))
00224                     except IOError, e:
00225                         print "[ERROR] Error writing test spec to file"
00226                         print e
00227 
00228             # If a path to a JUnit build report spec is provided, write it to a file
00229             if options.build_report_junit:
00230                 report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build")
00231                 report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties)
00232 
00233             # Print memory map summary on screen
00234             if build_report:
00235                 print
00236                 print print_build_memory_usage(build_report)
00237 
00238             print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
00239             status = print_report_exporter.report(build_report)
00240 
00241             if status:
00242                 sys.exit(0)
00243             else:
00244                 sys.exit(1)
00245 
00246     except KeyboardInterrupt, e:
00247         print "\n[CTRL+c] exit"
00248     except Exception,e:
00249         import traceback
00250         traceback.print_exc(file=sys.stdout)
00251         print "[ERROR] %s" % str(e)
00252         sys.exit(1)