From 1a8b973aab5c32f2062310ab20d41d37a434187d Mon Sep 17 00:00:00 2001 From: Brian Daniels Date: Fri, 22 Apr 2016 16:29:55 -0500 Subject: [PATCH] Moving test discovery logic into test_api.py. test.py is now just the CLI interface to these functions --- tools/test.py | 71 ++------------------------------------------ tools/test_api.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 68 deletions(-) diff --git a/tools/test.py b/tools/test.py index b2a0027277..3d60b3bc58 100644 --- a/tools/test.py +++ b/tools/test.py @@ -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: diff --git a/tools/test_api.py b/tools/test_api.py index 4df448e7aa..737fc84c26 100644 --- a/tools/test_api.py +++ b/tools/test_api.py @@ -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) \ No newline at end of file