Skip to content

Commit e472a51

Browse files
committed
Merge pull request #64 from bridadan/add-test-discovery-tool
Adding test.py script in tools that allows you to discover tests
2 parents 22e1e98 + 1a8b973 commit e472a51

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

tools/test.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
from optparse import OptionParser
25+
26+
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
27+
sys.path.insert(0, ROOT)
28+
29+
from tools.test_api import test_path_to_name, find_tests, print_tests
30+
31+
if __name__ == '__main__':
32+
try:
33+
# Parse Options
34+
parser = OptionParser()
35+
36+
parser.add_option("-l", "--list", action="store_true", dest="list",
37+
default=False, help="List (recursively) available tests in order and exit")
38+
39+
parser.add_option("-p", "--paths", dest="paths",
40+
default=None, help="Limit the tests to those within the specified comma separated list of paths")
41+
42+
parser.add_option("-f", "--format", type="choice", dest="format",
43+
choices=["list", "json"], default="list", help="List available tests in order and exit")
44+
45+
(options, args) = parser.parse_args()
46+
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('.')
57+
58+
print_tests(tests, options.format)
59+
sys.exit()
60+
61+
except KeyboardInterrupt, e:
62+
print "\n[CTRL+c] exit"
63+
except Exception,e:
64+
import traceback
65+
traceback.print_exc(file=sys.stdout)
66+
print "[ERROR] %s" % str(e)
67+
sys.exit(1)

tools/test_api.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,3 +1949,78 @@ def get_default_test_options_parser():
19491949
action="store_true",
19501950
help='Prints script version and exits')
19511951
return parser
1952+
1953+
def test_path_to_name(path):
1954+
"""Change all slashes in a path into hyphens
1955+
This creates a unique cross-platform test name based on the path
1956+
This can eventually be overriden by a to-be-determined meta-data mechanism"""
1957+
name_parts = []
1958+
head, tail = os.path.split(path)
1959+
while (tail and tail != "."):
1960+
name_parts.insert(0, tail)
1961+
head, tail = os.path.split(head)
1962+
1963+
return "-".join(name_parts)
1964+
1965+
def find_tests(base_dir):
1966+
"""Given any directory, walk through the subdirectories and find all tests"""
1967+
1968+
def find_tests_in_tests_directory(directory):
1969+
"""Given a 'TESTS' directory, return a dictionary of test names and test paths.
1970+
The formate of the dictionary is {"test-name": "./path/to/test"}"""
1971+
tests = {}
1972+
1973+
for d in os.listdir(directory):
1974+
# dir name host_tests is reserved for host python scripts.
1975+
if d != "host_tests":
1976+
# Loop on test case directories
1977+
for td in os.listdir(os.path.join(directory, d)):
1978+
# Add test case to the results if it is a directory
1979+
test_case_path = os.path.join(directory, d, td)
1980+
if os.path.isdir(test_case_path):
1981+
tests[test_path_to_name(test_case_path)] = test_case_path
1982+
1983+
return tests
1984+
1985+
tests_path = 'TESTS'
1986+
1987+
# Determine if "base_dir" is already a "TESTS" directory
1988+
_, top_folder = os.path.split(base_dir)
1989+
1990+
if top_folder == tests_path:
1991+
# Already pointing at a "TESTS" directory
1992+
return find_tests_in_tests_directory(base_dir)
1993+
else:
1994+
# Not pointing at a "TESTS" directory, so go find one!
1995+
tests = {}
1996+
1997+
for root, dirs, files in os.walk(base_dir):
1998+
# Don't search build directories
1999+
if '.build' in dirs:
2000+
dirs.remove('.build')
2001+
2002+
# If a "TESTS" directory is found, find the tests inside of it
2003+
if tests_path in dirs:
2004+
# Remove it from the directory walk
2005+
dirs.remove(tests_path)
2006+
2007+
# Get the tests inside of the "TESTS" directory
2008+
new_tests = find_tests_in_tests_directory(os.path.join(root, tests_path))
2009+
if new_tests:
2010+
tests.update(new_tests)
2011+
2012+
return tests
2013+
2014+
def print_tests(tests, format="list"):
2015+
"""Given a dictionary of tests (as returned from "find_tests"), print them
2016+
in the specified format"""
2017+
if format == "list":
2018+
for test_name, test_path in tests.iteritems():
2019+
print "Test Case:"
2020+
print " Name: %s" % test_name
2021+
print " Path: %s" % test_path
2022+
elif format == "json":
2023+
print json.dumps(tests, indent=2)
2024+
else:
2025+
print "Unknown format '%s'" % format
2026+
sys.exit(1)

0 commit comments

Comments
 (0)