|
21 | 21 | import sys
|
22 | 22 | import os
|
23 | 23 | import json
|
24 |
| -from optparse import OptionParser |
25 | 24 |
|
26 | 25 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
27 | 26 | sys.path.insert(0, ROOT)
|
28 | 27 |
|
29 |
| -from tools.test_api import test_path_to_name, find_tests, print_tests |
| 28 | +from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_build |
| 29 | +from tools.options import get_default_options_parser |
| 30 | +from tools.build_api import build_project |
| 31 | +from tools.targets import TARGET_MAP |
| 32 | +from tools.utils import mkdir |
30 | 33 |
|
31 | 34 | if __name__ == '__main__':
|
32 | 35 | try:
|
33 | 36 | # Parse Options
|
34 |
| - parser = OptionParser() |
| 37 | + parser = get_default_options_parser() |
| 38 | + |
| 39 | + parser.add_option("-j", "--jobs", |
| 40 | + type="int", |
| 41 | + dest="jobs", |
| 42 | + default=1, |
| 43 | + help="Number of concurrent jobs (default 1). Use 0 for auto based on host machine's number of CPUs") |
| 44 | + |
| 45 | + parser.add_option("--source", dest="source_dir", |
| 46 | + default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append") |
| 47 | + |
| 48 | + parser.add_option("--build", dest="build_dir", |
| 49 | + default=None, help="The build (output) directory") |
35 | 50 |
|
36 | 51 | parser.add_option("-l", "--list", action="store_true", dest="list",
|
37 | 52 | default=False, help="List (recursively) available tests in order and exit")
|
38 | 53 |
|
39 | 54 | parser.add_option("-p", "--paths", dest="paths",
|
40 | 55 | default=None, help="Limit the tests to those within the specified comma separated list of paths")
|
41 | 56 |
|
| 57 | + format_choices = ["list", "json"] |
| 58 | + format_default_choice = "list" |
| 59 | + format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice) |
42 | 60 | parser.add_option("-f", "--format", type="choice", dest="format",
|
43 |
| - choices=["list", "json"], default="list", help="List available tests in order and exit") |
| 61 | + choices=format_choices, default=format_default_choice, help=format_help) |
| 62 | + |
| 63 | + parser.add_option("-n", "--names", dest="names", |
| 64 | + default=None, help="Limit the tests to a comma separated list of names") |
| 65 | + |
| 66 | + parser.add_option("--test-spec", dest="test_spec", |
| 67 | + default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool") |
| 68 | + |
| 69 | + parser.add_option("-v", "--verbose", |
| 70 | + action="store_true", |
| 71 | + dest="verbose", |
| 72 | + default=False, |
| 73 | + help="Verbose diagnostic output") |
44 | 74 |
|
45 | 75 | (options, args) = parser.parse_args()
|
46 | 76 |
|
47 |
| - # Print available tests in order and exit |
48 |
| - if options.list is True: |
49 |
| - tests = {} |
50 |
| - |
51 |
| - if options.paths: |
52 |
| - all_paths = options.paths.split(",") |
53 |
| - for path in all_paths: |
54 |
| - tests.update(find_tests(path)) |
55 |
| - else: |
56 |
| - tests = find_tests('.') |
| 77 | + # Filter tests by path if specified |
| 78 | + if options.paths: |
| 79 | + all_paths = options.paths.split(",") |
| 80 | + else: |
| 81 | + all_paths = ["."] |
| 82 | + |
| 83 | + all_tests = {} |
| 84 | + tests = {} |
| 85 | + |
| 86 | + # Find all tests in the relevant paths |
| 87 | + for path in all_paths: |
| 88 | + all_tests.update(find_tests(path)) |
| 89 | + |
| 90 | + # Filter tests by name if specified |
| 91 | + if options.names: |
| 92 | + all_names = options.names.split(",") |
57 | 93 |
|
| 94 | + all_tests_keys = all_tests.keys() |
| 95 | + for name in all_names: |
| 96 | + if name in all_tests_keys: |
| 97 | + tests[name] = all_tests[name] |
| 98 | + else: |
| 99 | + print "[Warning] Test with name '%s' was not found in the available tests" % (name) |
| 100 | + else: |
| 101 | + tests = all_tests |
| 102 | + |
| 103 | + if options.list: |
| 104 | + # Print available tests in order and exit |
58 | 105 | print_tests(tests, options.format)
|
59 |
| - sys.exit() |
| 106 | + else: |
| 107 | + # Build all tests |
| 108 | + if not options.build_dir: |
| 109 | + print "[ERROR] You must specify a build path" |
| 110 | + sys.exit(1) |
| 111 | + |
| 112 | + base_source_paths = options.source_dir |
| 113 | + |
| 114 | + # Default base source path is the current directory |
| 115 | + if not base_source_paths: |
| 116 | + base_source_paths = ['.'] |
| 117 | + |
| 118 | + target = TARGET_MAP[options.mcu] |
| 119 | + |
| 120 | + # Build all the tests |
| 121 | + test_build = build_tests(tests, base_source_paths, options.build_dir, target, options.tool, |
| 122 | + options=options.options, |
| 123 | + clean=options.clean, |
| 124 | + jobs=options.jobs) |
| 125 | + |
| 126 | + # If a path to a test spec is provided, write it to a file |
| 127 | + if options.test_spec: |
| 128 | + test_spec_data = test_spec_from_test_build(test_build) |
| 129 | + |
| 130 | + # Create the target dir for the test spec if necessary |
| 131 | + # mkdir will not create the dir if it already exists |
| 132 | + test_spec_dir = os.path.dirname(options.test_spec) |
| 133 | + if test_spec_dir: |
| 134 | + mkdir(test_spec_dir) |
| 135 | + |
| 136 | + try: |
| 137 | + with open(options.test_spec, 'w') as f: |
| 138 | + f.write(json.dumps(test_spec_data, indent=2)) |
| 139 | + except IOError, e: |
| 140 | + print "[ERROR] Error writing test spec to file" |
| 141 | + print e |
| 142 | + |
| 143 | + sys.exit() |
60 | 144 |
|
61 | 145 | except KeyboardInterrupt, e:
|
62 | 146 | print "\n[CTRL+c] exit"
|
|
0 commit comments