Clone of official tools

Revision:
13:ab47a20b66f0
Parent:
0:66f3b5499f7f
Child:
22:9e85236d8716
--- a/test.py	Tue Jun 14 11:33:06 2016 +0100
+++ b/test.py	Thu Jul 14 20:21:19 2016 +0100
@@ -21,26 +21,33 @@
 import sys
 import os
 import json
+import fnmatch
 
 ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
 sys.path.insert(0, ROOT)
 
-from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_build
+from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds
 from tools.options import get_default_options_parser
 from tools.build_api import build_project, build_library
 from tools.targets import TARGET_MAP
-from tools.utils import mkdir
+from tools.utils import mkdir, ToolException, NotSupportedException
+from tools.test_exporters import ReportExporter, ResultExporterType
 
 if __name__ == '__main__':
     try:
         # Parse Options
         parser = get_default_options_parser()
         
+        parser.add_option("-D", "",
+                          action="append",
+                          dest="macros",
+                          help="Add a macro definition")
+        
         parser.add_option("-j", "--jobs",
                           type="int",
                           dest="jobs",
-                          default=1,
-                          help="Number of concurrent jobs (default 1). Use 0 for auto based on host machine's number of CPUs")
+                          default=0,
+                          help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
 
         parser.add_option("--source", dest="source_dir",
                           default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
@@ -60,12 +67,18 @@
         parser.add_option("-f", "--format", type="choice", dest="format",
                           choices=format_choices, default=format_default_choice, help=format_help)
         
+        parser.add_option("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail",
+                          default=None, help="Continue trying to build all tests if a build failure occurs")
+
         parser.add_option("-n", "--names", dest="names",
                           default=None, help="Limit the tests to a comma separated list of names")
                           
         parser.add_option("--test-spec", dest="test_spec",
                           default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool")
         
+        parser.add_option("--build-report-junit", dest="build_report_junit",
+                          default=None, help="Destination path for a build report in the JUnit xml format")
+        
         parser.add_option("-v", "--verbose",
                           action="store_true",
                           dest="verbose",
@@ -90,11 +103,13 @@
         # Filter tests by name if specified
         if options.names:
             all_names = options.names.split(",")
+            all_names = [x.lower() for x in all_names]
             
-            all_tests_keys = all_tests.keys()
             for name in all_names:
-                if name in all_tests_keys:
-                    tests[name] = all_tests[name]
+                if any(fnmatch.fnmatch(testname, name) for testname in all_tests):
+                    for testname, test in all_tests.items():
+                        if fnmatch.fnmatch(testname, name):
+                            tests[testname] = test
                 else:
                     print "[Warning] Test with name '%s' was not found in the available tests" % (name)
         else:
@@ -103,6 +118,7 @@
         if options.list:
             # Print available tests in order and exit
             print_tests(tests, options.format)
+            sys.exit(0)
         else:
             # Build all tests
             if not options.build_dir:
@@ -116,38 +132,79 @@
                 base_source_paths = ['.']
             
             
-            target = TARGET_MAP[options.mcu]
+            target = options.mcu
             
-            lib_build_res = build_library(base_source_paths, options.build_dir, target, options.tool,
-                                            options=options.options,
-                                            jobs=options.jobs,
-                                            clean=options.clean,
-                                            archive=False)
-            
-            # Build all the tests
-            test_build = build_tests(tests, [options.build_dir], options.build_dir, target, options.tool,
-                    options=options.options,
-                    clean=options.clean,
-                    jobs=options.jobs)
+            build_report = {}
+            build_properties = {}
+
+            library_build_success = False
+            try:
+                # Build sources
+                build_library(base_source_paths, options.build_dir, target, options.tool,
+                                                options=options.options,
+                                                jobs=options.jobs,
+                                                clean=options.clean,
+                                                report=build_report,
+                                                properties=build_properties,
+                                                name="mbed-build",
+                                                macros=options.macros,
+                                                verbose=options.verbose,
+                                                archive=False)
+                
+                library_build_success = True
+            except ToolException, e:
+                # ToolException output is handled by the build log
+                pass
+            except NotSupportedException, e:
+                # NotSupportedException is handled by the build log
+                pass
+            except Exception, e:
+                # Some other exception occurred, print the error message
+                print e
             
-            # If a path to a test spec is provided, write it to a file
-            if options.test_spec:
-                test_spec_data = test_spec_from_test_build(test_build)
+            if not library_build_success:
+                print "Failed to build library"
+            else:
+                # Build all the tests
+                test_build_success, test_build = build_tests(tests, [options.build_dir], options.build_dir, target, options.tool,
+                        options=options.options,
+                        clean=options.clean,
+                        report=build_report,
+                        properties=build_properties,
+                        macros=options.macros,
+                        verbose=options.verbose,
+                        jobs=options.jobs,
+                        continue_on_build_fail=options.continue_on_build_fail)
                 
-                # Create the target dir for the test spec if necessary
-                # mkdir will not create the dir if it already exists
-                test_spec_dir = os.path.dirname(options.test_spec)
-                if test_spec_dir:
-                    mkdir(test_spec_dir)
-                
-                try:
-                    with open(options.test_spec, 'w') as f:
-                        f.write(json.dumps(test_spec_data, indent=2))
-                except IOError, e:
-                    print "[ERROR] Error writing test spec to file"
-                    print e
+                # If a path to a test spec is provided, write it to a file
+                if options.test_spec:
+                    test_spec_data = test_spec_from_test_builds(test_build)
+                    
+                    # Create the target dir for the test spec if necessary
+                    # mkdir will not create the dir if it already exists
+                    test_spec_dir = os.path.dirname(options.test_spec)
+                    if test_spec_dir:
+                        mkdir(test_spec_dir)
+                    
+                    try:
+                        with open(options.test_spec, 'w') as f:
+                            f.write(json.dumps(test_spec_data, indent=2))
+                    except IOError, e:
+                        print "[ERROR] Error writing test spec to file"
+                        print e
             
-        sys.exit()
+            # If a path to a JUnit build report spec is provided, write it to a file
+            if options.build_report_junit:
+                report_exporter = ReportExporter(ResultExporterType.JUNIT, package="build")
+                report_exporter.report_to_file(build_report, options.build_report_junit, test_suite_properties=build_properties)
+        
+            print_report_exporter = ReportExporter(ResultExporterType.PRINT, package="build")
+            status = print_report_exporter.report(build_report)
+        
+            if status:
+                sys.exit(0)
+            else:
+                sys.exit(1)
 
     except KeyboardInterrupt, e:
         print "\n[CTRL+c] exit"