@@ -1949,3 +1949,78 @@ def get_default_test_options_parser():
1949
1949
action = "store_true" ,
1950
1950
help = 'Prints script version and exits' )
1951
1951
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