Skip to content

Commit 698c7d9

Browse files
authored
Merge pull request #5819 from geky/test-common
Add COMMON folder for tests
2 parents b8abbab + d82d988 commit 698c7d9

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

tools/test_api.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import datetime
2929
import threading
3030
import ctypes
31+
import functools
3132
from types import ListType
3233
from colorama import Fore, Back, Style
3334
from prettytable import PrettyTable
@@ -71,7 +72,6 @@
7172
from tools.utils import argparse_uppercase_type
7273
from tools.utils import argparse_lowercase_type
7374
from tools.utils import argparse_many
74-
from tools.utils import get_path_depth
7575

7676
import tools.host_tests.host_tests_plugins as host_tests_plugins
7777

@@ -2019,9 +2019,15 @@ def find_tests(base_dir, target_name, toolchain_name, app_config=None):
20192019
toolchain_name: name of the toolchain to use for scanning (ex. 'GCC_ARM')
20202020
options: Compile options to pass to the toolchain (ex. ['debug-info'])
20212021
app_config - location of a chosen mbed_app.json file
2022+
2023+
returns a dictionary where keys are the test name, and the values are
2024+
lists of paths needed to biuld the test.
20222025
"""
20232026

2027+
# Temporary structure: tests referenced by (name, base, group, case) tuple
20242028
tests = {}
2029+
# List of common folders: (predicate function, path) tuple
2030+
commons = []
20252031

20262032
# Prepare the toolchain
20272033
toolchain = prepare_toolchain([base_dir], None, target_name, toolchain_name,
@@ -2042,32 +2048,52 @@ def find_tests(base_dir, target_name, toolchain_name, app_config=None):
20422048
# Loop through all subdirectories
20432049
for d in test_resources.inc_dirs:
20442050

2045-
# If the test case folder is not called 'host_tests' and it is
2051+
# If the test case folder is not called 'host_tests' or 'COMMON' and it is
20462052
# located two folders down from the main 'TESTS' folder (ex. TESTS/testgroup/testcase)
20472053
# then add it to the tests
2048-
path_depth = get_path_depth(relpath(d, walk_base_dir))
2049-
if path_depth == 2:
2054+
relative_path = relpath(d, walk_base_dir)
2055+
relative_path_parts = os.path.normpath(relative_path).split(os.sep)
2056+
if len(relative_path_parts) == 2:
20502057
test_group_directory_path, test_case_directory = os.path.split(d)
20512058
test_group_directory = os.path.basename(test_group_directory_path)
20522059

2053-
# Check to make sure discoverd folder is not in a host test directory
2054-
if test_case_directory != 'host_tests' and test_group_directory != 'host_tests':
2060+
# Check to make sure discoverd folder is not in a host test directory or common directory
2061+
special_dirs = ['host_tests', 'COMMON']
2062+
if test_group_directory not in special_dirs and test_case_directory not in special_dirs:
20552063
test_name = test_path_to_name(d, base_dir)
2056-
tests[test_name] = d
2064+
tests[(test_name, walk_base_dir, test_group_directory, test_case_directory)] = [d]
2065+
2066+
# Also find any COMMON paths, we'll add these later once we find all the base tests
2067+
if 'COMMON' in relative_path_parts:
2068+
if relative_path_parts[0] != 'COMMON':
2069+
def predicate(base_pred, group_pred, (name, base, group, case)):
2070+
return base == base_pred and group == group_pred
2071+
commons.append((functools.partial(predicate, walk_base_dir, relative_path_parts[0]), d))
2072+
else:
2073+
def predicate(base_pred, (name, base, group, case)):
2074+
return base == base_pred
2075+
commons.append((functools.partial(predicate, walk_base_dir), d))
20572076

2058-
return tests
2077+
# Apply common directories
2078+
for pred, path in commons:
2079+
for test_identity, test_paths in tests.iteritems():
2080+
if pred(test_identity):
2081+
test_paths.append(path)
2082+
2083+
# Drop identity besides name
2084+
return {name: paths for (name, _, _, _), paths in tests.iteritems()}
20592085

20602086
def print_tests(tests, format="list", sort=True):
20612087
"""Given a dictionary of tests (as returned from "find_tests"), print them
20622088
in the specified format"""
20632089
if format == "list":
20642090
for test_name in sorted(tests.keys()):
2065-
test_path = tests[test_name]
2091+
test_path = tests[test_name][0]
20662092
print "Test Case:"
20672093
print " Name: %s" % test_name
20682094
print " Path: %s" % test_path
20692095
elif format == "json":
2070-
print json.dumps(tests, indent=2)
2096+
print json.dumps({test_name: test_path[0] for test_name, test_paths in tests}, indent=2)
20712097
else:
20722098
print "Unknown format '%s'" % format
20732099
sys.exit(1)
@@ -2164,13 +2190,16 @@ def build_tests(tests, base_source_paths, build_path, target, toolchain_name,
21642190
jobs_count = int(jobs if jobs else cpu_count())
21652191
p = Pool(processes=jobs_count)
21662192
results = []
2167-
for test_name, test_path in tests.iteritems():
2168-
test_build_path = os.path.join(build_path, test_path)
2169-
src_path = base_source_paths + [test_path]
2193+
for test_name, test_paths in tests.iteritems():
2194+
if type(test_paths) != ListType:
2195+
test_paths = [test_paths]
2196+
2197+
test_build_path = os.path.join(build_path, test_paths[0])
2198+
src_paths = base_source_paths + test_paths
21702199
bin_file = None
2171-
test_case_folder_name = os.path.basename(test_path)
2200+
test_case_folder_name = os.path.basename(test_paths[0])
21722201

2173-
args = (src_path, test_build_path, target, toolchain_name)
2202+
args = (src_paths, test_build_path, target, toolchain_name)
21742203
kwargs = {
21752204
'jobs': 1,
21762205
'clean': clean,

0 commit comments

Comments
 (0)