Skip to content

Commit 9a126c4

Browse files
committed
Adding script in the tools directory that allows you to discover tests
1 parent 0a14cc4 commit 9a126c4

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tools/test.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#! /usr/bin/env python2
2+
"""
3+
mbed SDK
4+
Copyright (c) 2011-2013 ARM Limited
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
19+
TEST BUILD & RUN
20+
"""
21+
import sys
22+
import os
23+
import json
24+
25+
from optparse import OptionParser
26+
27+
def test_path_to_name(path):
28+
# Change all slashes in a path into hyphens
29+
# This creates a unique cross-platform test name based on the path
30+
# This can eventually be overriden by a to-be-determined meta-data mechanism
31+
name_parts = []
32+
head, tail = os.path.split(path)
33+
while (tail != "" and tail != "."):
34+
name_parts.insert(0, tail)
35+
head, tail = os.path.split(head)
36+
37+
return "-".join(name_parts)
38+
39+
def find_tests_in_tests_directory(directory):
40+
tests = {}
41+
42+
for d in os.listdir(directory):
43+
# dir name host_tests is reserved for host python scripts.
44+
if d != "host_tests":
45+
# Loop on test case directories
46+
for td in os.listdir(os.path.join(directory, d)):
47+
# Add test case to the results if it is a directory
48+
test_case_path = os.path.join(directory, d, td)
49+
if os.path.isdir(test_case_path):
50+
tests[test_path_to_name(test_case_path)] = test_case_path
51+
52+
return tests
53+
54+
def find_tests(base_dir):
55+
tests_path = 'TESTS'
56+
57+
# Determine if "base_dir" is already a "TESTS" directory
58+
_, top_folder = os.path.split(base_dir)
59+
60+
if top_folder == tests_path:
61+
# Already pointing at a "TESTS" directory
62+
return find_tests_in_tests_directory(base_dir)
63+
else:
64+
# Not pointing at a "TESTS" directory, so go find one!
65+
tests = {}
66+
67+
for root, dirs, files in os.walk(base_dir):
68+
# Don't search build directories
69+
if '.build' in dirs:
70+
dirs.remove('.build')
71+
72+
# If a "TESTS" directory is found, find the tests inside of it
73+
if tests_path in dirs:
74+
# Remove it from the directory walk
75+
dirs.remove(tests_path)
76+
77+
# Get the tests inside of the "TESTS" directory
78+
new_tests = find_tests_in_tests_directory(os.path.join(root, tests_path))
79+
if new_tests:
80+
tests.update(new_tests)
81+
82+
return tests
83+
84+
def print_tests(tests, format="list"):
85+
if format == "list":
86+
for test_name, test_path in tests.iteritems():
87+
print "Test Case:"
88+
print " Name: %s" % test_name
89+
print " Path: %s" % test_path
90+
elif format == "json":
91+
print json.dumps(tests)
92+
else:
93+
print "Unknown format '%s'" % format
94+
sys.exit(1)
95+
96+
if __name__ == '__main__':
97+
try:
98+
# Parse Options
99+
parser = OptionParser()
100+
101+
parser.add_option("-l", "--list", action="store_true", dest="list",
102+
default=False, help="List (recursively) available tests in order and exit")
103+
104+
parser.add_option("-p", "--paths", dest="paths",
105+
default=None, help="Limit the tests to those within the specified comma separated list of paths")
106+
107+
parser.add_option("-f", "--format", type="choice", dest="format",
108+
choices=["list", "json"], default="list", help="List available tests in order and exit")
109+
110+
(options, args) = parser.parse_args()
111+
112+
# Print available tests in order and exit
113+
if options.list is True:
114+
tests = {}
115+
116+
if options.paths:
117+
all_paths = options.paths.split(",")
118+
for path in all_paths:
119+
tests.update(find_tests(path))
120+
else:
121+
tests = find_tests('.')
122+
123+
print_tests(tests, options.format)
124+
sys.exit()
125+
126+
except KeyboardInterrupt, e:
127+
print "\n[CTRL+c] exit"
128+
except Exception,e:
129+
import traceback
130+
traceback.print_exc(file=sys.stdout)
131+
print "[ERROR] %s" % str(e)
132+
sys.exit(1)

0 commit comments

Comments
 (0)