2 Test discovery functions.
8 from lit.TestingConfig import TestingConfig
9 from lit import LitConfig, Test
11 def dirContainsTestSuite(path, lit_config):
12 cfgpath = os.path.join(path, lit_config.site_config_name)
13 if os.path.exists(cfgpath):
15 cfgpath = os.path.join(path, lit_config.config_name)
16 if os.path.exists(cfgpath):
19 def getTestSuite(item, litConfig, cache):
20 """getTestSuite(item, litConfig, cache) -> (suite, relative_path)
22 Find the test suite containing @arg item.
24 @retval (None, ...) - Indicates no test suite contains @arg item.
25 @retval (suite, relative_path) - The suite that @arg item is in, and its
26 relative path inside that suite.
29 # Check for a site config or a lit config.
30 cfgpath = dirContainsTestSuite(path, litConfig)
32 # If we didn't find a config file, keep looking.
34 parent,base = os.path.split(path)
38 ts, relative = search(parent)
39 return (ts, relative + (base,))
41 # We found a config file, load it.
43 litConfig.note('loading suite config %r' % cfgpath)
45 cfg = TestingConfig.frompath(cfgpath, None, litConfig, mustExist = True)
46 source_root = os.path.realpath(cfg.test_source_root or path)
47 exec_root = os.path.realpath(cfg.test_exec_root or path)
48 return Test.TestSuite(cfg.name, source_root, exec_root, cfg), ()
51 # Check for an already instantiated test suite.
54 cache[path] = res = search1(path)
57 # Canonicalize the path.
58 item = os.path.realpath(item)
60 # Skip files and virtual components.
62 while not os.path.isdir(item):
63 parent,base = os.path.split(item)
66 components.append(base)
70 ts, relative = search(item)
71 return ts, tuple(relative + tuple(components))
73 def getLocalConfig(ts, path_in_suite, litConfig, cache):
74 def search1(path_in_suite):
75 # Get the parent config.
79 parent = search(path_in_suite[:-1])
81 # Load the local configuration.
82 source_path = ts.getSourcePath(path_in_suite)
83 cfgpath = os.path.join(source_path, litConfig.local_config_name)
85 litConfig.note('loading local config %r' % cfgpath)
86 return TestingConfig.frompath(cfgpath, parent.clone(cfgpath), litConfig,
89 def search(path_in_suite):
90 key = (ts, path_in_suite)
93 cache[key] = res = search1(path_in_suite)
96 return search(path_in_suite)
98 def getTests(path, litConfig, testSuiteCache, localConfigCache):
99 # Find the test suite for this input and its relative path.
100 ts,path_in_suite = getTestSuite(path, litConfig, testSuiteCache)
102 litConfig.warning('unable to find test suite for %r' % path)
106 litConfig.note('resolved input %r to %r::%r' % (path, ts.name,
109 return ts, getTestsInSuite(ts, path_in_suite, litConfig,
110 testSuiteCache, localConfigCache)
112 def getTestsInSuite(ts, path_in_suite, litConfig,
113 testSuiteCache, localConfigCache):
114 # Check that the source path exists (errors here are reported by the
116 source_path = ts.getSourcePath(path_in_suite)
117 if not os.path.exists(source_path):
120 # Check if the user named a test directly.
121 if not os.path.isdir(source_path):
122 lc = getLocalConfig(ts, path_in_suite[:-1], litConfig, localConfigCache)
123 yield Test.Test(ts, path_in_suite, lc)
126 # Otherwise we have a directory to search for tests, start by getting the
127 # local configuration.
128 lc = getLocalConfig(ts, path_in_suite, litConfig, localConfigCache)
131 if lc.test_format is not None:
132 for res in lc.test_format.getTestsInDirectory(ts, path_in_suite,
136 # Search subdirectories.
137 for filename in os.listdir(source_path):
138 # FIXME: This doesn't belong here?
139 if filename in ('Output', '.svn', '.git') or filename in lc.excludes:
142 # Ignore non-directories.
143 file_sourcepath = os.path.join(source_path, filename)
144 if not os.path.isdir(file_sourcepath):
147 # Check for nested test suites, first in the execpath in case there is a
148 # site configuration and then in the source path.
149 subpath = path_in_suite + (filename,)
150 file_execpath = ts.getExecPath(subpath)
151 if dirContainsTestSuite(file_execpath, litConfig):
152 sub_ts, subpath_in_suite = getTestSuite(file_execpath, litConfig,
154 elif dirContainsTestSuite(file_sourcepath, litConfig):
155 sub_ts, subpath_in_suite = getTestSuite(file_sourcepath, litConfig,
160 # If the this directory recursively maps back to the current test suite,
161 # disregard it (this can happen if the exec root is located inside the
162 # current test suite, for example).
166 # Otherwise, load from the nested test suite, if present.
167 if sub_ts is not None:
168 subiter = getTestsInSuite(sub_ts, subpath_in_suite, litConfig,
169 testSuiteCache, localConfigCache)
171 subiter = getTestsInSuite(ts, subpath, litConfig, testSuiteCache,
179 litConfig.warning('test suite %r contained no tests' % sub_ts.name)
181 def find_tests_for_inputs(lit_config, inputs):
183 find_tests_for_inputs(lit_config, inputs) -> [Test]
185 Given a configuration object and a list of input specifiers, find all the
189 # Expand '@...' form in inputs.
192 if os.path.exists(input) or not input.startswith('@'):
193 actual_inputs.append(input)
200 actual_inputs.append(ln)
204 # Load the tests from the inputs.
206 test_suite_cache = {}
207 local_config_cache = {}
208 for input in actual_inputs:
210 tests.extend(getTests(input, lit_config,
211 test_suite_cache, local_config_cache)[1])
212 if prev == len(tests):
213 lit_config.warning('input %r contained no tests' % input)
215 # If there were any errors during test discovery, exit now.
216 if lit_config.numErrors:
217 sys.stderr.write('%d errors, exiting.\n' % lit_config.numErrors)
222 def load_test_suite(inputs):
225 from lit.LitTestCase import LitTestCase
227 # Create the global config object.
228 litConfig = LitConfig.LitConfig(progname = 'lit',
232 valgrindLeakCheck = False,
236 isWindows = (platform.system()=='Windows'),
239 tests = find_tests_for_inputs(litConfig, inputs)
241 # Return a unittest test suite which just runs the tests in order.
242 return unittest.TestSuite([LitTestCase(test, litConfig) for test in tests])