Skip to content

Commit 1a8b973

Browse files
committed
Moving test discovery logic into test_api.py. test.py is now just the CLI interface to these functions
1 parent 9a126c4 commit 1a8b973

File tree

2 files changed

+78
-68
lines changed

2 files changed

+78
-68
lines changed

tools/test.py

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,77 +21,12 @@
2121
import sys
2222
import os
2323
import json
24-
2524
from optparse import OptionParser
2625

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
26+
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
27+
sys.path.insert(0, ROOT)
5328

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)
29+
from tools.test_api import test_path_to_name, find_tests, print_tests
9530

9631
if __name__ == '__main__':
9732
try:

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)