Moving test discovery logic into test_api.py. test.py is now just the CLI interface to these functions

Brian Daniels 2016-04-22 16:29:55 -05:00
parent 9a126c4e3a
commit 1a8b973aab
2 changed files with 78 additions and 68 deletions

View File

@ -21,77 +21,12 @@ TEST BUILD & RUN
import sys
import os
import json
from optparse import OptionParser
def test_path_to_name(path):
# Change all slashes in a path into hyphens
# This creates a unique cross-platform test name based on the path
# This can eventually be overriden by a to-be-determined meta-data mechanism
name_parts = []
head, tail = os.path.split(path)
while (tail != "" and tail != "."):
name_parts.insert(0, tail)
head, tail = os.path.split(head)
return "-".join(name_parts)
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, ROOT)
def find_tests_in_tests_directory(directory):
tests = {}
for d in os.listdir(directory):
# dir name host_tests is reserved for host python scripts.
if d != "host_tests":
# Loop on test case directories
for td in os.listdir(os.path.join(directory, d)):
# Add test case to the results if it is a directory
test_case_path = os.path.join(directory, d, td)
if os.path.isdir(test_case_path):
tests[test_path_to_name(test_case_path)] = test_case_path
return tests
def find_tests(base_dir):
tests_path = 'TESTS'
# Determine if "base_dir" is already a "TESTS" directory
_, top_folder = os.path.split(base_dir)
if top_folder == tests_path:
# Already pointing at a "TESTS" directory
return find_tests_in_tests_directory(base_dir)
else:
# Not pointing at a "TESTS" directory, so go find one!
tests = {}
for root, dirs, files in os.walk(base_dir):
# Don't search build directories
if '.build' in dirs:
dirs.remove('.build')
# If a "TESTS" directory is found, find the tests inside of it
if tests_path in dirs:
# Remove it from the directory walk
dirs.remove(tests_path)
# Get the tests inside of the "TESTS" directory
new_tests = find_tests_in_tests_directory(os.path.join(root, tests_path))
if new_tests:
tests.update(new_tests)
return tests
def print_tests(tests, format="list"):
if format == "list":
for test_name, test_path in tests.iteritems():
print "Test Case:"
print " Name: %s" % test_name
print " Path: %s" % test_path
elif format == "json":
print json.dumps(tests)
else:
print "Unknown format '%s'" % format
sys.exit(1)
from tools.test_api import test_path_to_name, find_tests, print_tests
if __name__ == '__main__':
try:

View File

@ -1949,3 +1949,78 @@ def get_default_test_options_parser():
action="store_true",
help='Prints script version and exits')
return parser
def test_path_to_name(path):
"""Change all slashes in a path into hyphens
This creates a unique cross-platform test name based on the path
This can eventually be overriden by a to-be-determined meta-data mechanism"""
name_parts = []
head, tail = os.path.split(path)
while (tail and tail != "."):
name_parts.insert(0, tail)
head, tail = os.path.split(head)
return "-".join(name_parts)
def find_tests(base_dir):
"""Given any directory, walk through the subdirectories and find all tests"""
def find_tests_in_tests_directory(directory):
"""Given a 'TESTS' directory, return a dictionary of test names and test paths.
The formate of the dictionary is {"test-name": "./path/to/test"}"""
tests = {}
for d in os.listdir(directory):
# dir name host_tests is reserved for host python scripts.
if d != "host_tests":
# Loop on test case directories
for td in os.listdir(os.path.join(directory, d)):
# Add test case to the results if it is a directory
test_case_path = os.path.join(directory, d, td)
if os.path.isdir(test_case_path):
tests[test_path_to_name(test_case_path)] = test_case_path
return tests
tests_path = 'TESTS'
# Determine if "base_dir" is already a "TESTS" directory
_, top_folder = os.path.split(base_dir)
if top_folder == tests_path:
# Already pointing at a "TESTS" directory
return find_tests_in_tests_directory(base_dir)
else:
# Not pointing at a "TESTS" directory, so go find one!
tests = {}
for root, dirs, files in os.walk(base_dir):
# Don't search build directories
if '.build' in dirs:
dirs.remove('.build')
# If a "TESTS" directory is found, find the tests inside of it
if tests_path in dirs:
# Remove it from the directory walk
dirs.remove(tests_path)
# Get the tests inside of the "TESTS" directory
new_tests = find_tests_in_tests_directory(os.path.join(root, tests_path))
if new_tests:
tests.update(new_tests)
return tests
def print_tests(tests, format="list"):
"""Given a dictionary of tests (as returned from "find_tests"), print them
in the specified format"""
if format == "list":
for test_name, test_path in tests.iteritems():
print "Test Case:"
print " Name: %s" % test_name
print " Path: %s" % test_path
elif format == "json":
print json.dumps(tests, indent=2)
else:
print "Unknown format '%s'" % format
sys.exit(1)