[lit] Eliminate mustExist parameter from TestingConfig.frompath().
[oota-llvm.git] / utils / lit / lit / discovery.py
1 """
2 Test discovery functions.
3 """
4
5 import os
6 import sys
7
8 from lit.TestingConfig import TestingConfig
9 from lit import LitConfig, Test
10
11 def dirContainsTestSuite(path, lit_config):
12     cfgpath = os.path.join(path, lit_config.site_config_name)
13     if os.path.exists(cfgpath):
14         return cfgpath
15     cfgpath = os.path.join(path, lit_config.config_name)
16     if os.path.exists(cfgpath):
17         return cfgpath
18
19 def getTestSuite(item, litConfig, cache):
20     """getTestSuite(item, litConfig, cache) -> (suite, relative_path)
21
22     Find the test suite containing @arg item.
23
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.
27     """
28     def search1(path):
29         # Check for a site config or a lit config.
30         cfgpath = dirContainsTestSuite(path, litConfig)
31
32         # If we didn't find a config file, keep looking.
33         if not cfgpath:
34             parent,base = os.path.split(path)
35             if parent == path:
36                 return (None, ())
37
38             ts, relative = search(parent)
39             return (ts, relative + (base,))
40
41         # We found a config file, load it.
42         if litConfig.debug:
43             litConfig.note('loading suite config %r' % cfgpath)
44
45         cfg = TestingConfig.frompath(cfgpath, None, litConfig)
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), ()
49
50     def search(path):
51         # Check for an already instantiated test suite.
52         res = cache.get(path)
53         if res is None:
54             cache[path] = res = search1(path)
55         return res
56
57     # Canonicalize the path.
58     item = os.path.realpath(item)
59
60     # Skip files and virtual components.
61     components = []
62     while not os.path.isdir(item):
63         parent,base = os.path.split(item)
64         if parent == item:
65             return (None, ())
66         components.append(base)
67         item = parent
68     components.reverse()
69
70     ts, relative = search(item)
71     return ts, tuple(relative + tuple(components))
72
73 def getLocalConfig(ts, path_in_suite, litConfig, cache):
74     def search1(path_in_suite):
75         # Get the parent config.
76         if not path_in_suite:
77             parent = ts.config
78         else:
79             parent = search(path_in_suite[:-1])
80
81         # Check if there is a local configuration file.
82         source_path = ts.getSourcePath(path_in_suite)
83         cfgpath = os.path.join(source_path, litConfig.local_config_name)
84
85         # If not, just reuse the parent config.
86         if not os.path.exists(cfgpath):
87             return parent
88
89         # Otherwise, copy the current config and load the local configuration
90         # file into it.
91         config = parent.clone()
92         if litConfig.debug:
93             litConfig.note('loading local config %r' % cfgpath)
94         return TestingConfig.frompath(cfgpath, config, litConfig)
95
96     def search(path_in_suite):
97         key = (ts, path_in_suite)
98         res = cache.get(key)
99         if res is None:
100             cache[key] = res = search1(path_in_suite)
101         return res
102
103     return search(path_in_suite)
104
105 def getTests(path, litConfig, testSuiteCache, localConfigCache):
106     # Find the test suite for this input and its relative path.
107     ts,path_in_suite = getTestSuite(path, litConfig, testSuiteCache)
108     if ts is None:
109         litConfig.warning('unable to find test suite for %r' % path)
110         return (),()
111
112     if litConfig.debug:
113         litConfig.note('resolved input %r to %r::%r' % (path, ts.name,
114                                                         path_in_suite))
115
116     return ts, getTestsInSuite(ts, path_in_suite, litConfig,
117                                testSuiteCache, localConfigCache)
118
119 def getTestsInSuite(ts, path_in_suite, litConfig,
120                     testSuiteCache, localConfigCache):
121     # Check that the source path exists (errors here are reported by the
122     # caller).
123     source_path = ts.getSourcePath(path_in_suite)
124     if not os.path.exists(source_path):
125         return
126
127     # Check if the user named a test directly.
128     if not os.path.isdir(source_path):
129         lc = getLocalConfig(ts, path_in_suite[:-1], litConfig, localConfigCache)
130         yield Test.Test(ts, path_in_suite, lc)
131         return
132
133     # Otherwise we have a directory to search for tests, start by getting the
134     # local configuration.
135     lc = getLocalConfig(ts, path_in_suite, litConfig, localConfigCache)
136
137     # Search for tests.
138     if lc.test_format is not None:
139         for res in lc.test_format.getTestsInDirectory(ts, path_in_suite,
140                                                       litConfig, lc):
141             yield res
142
143     # Search subdirectories.
144     for filename in os.listdir(source_path):
145         # FIXME: This doesn't belong here?
146         if filename in ('Output', '.svn', '.git') or filename in lc.excludes:
147             continue
148
149         # Ignore non-directories.
150         file_sourcepath = os.path.join(source_path, filename)
151         if not os.path.isdir(file_sourcepath):
152             continue
153
154         # Check for nested test suites, first in the execpath in case there is a
155         # site configuration and then in the source path.
156         subpath = path_in_suite + (filename,)
157         file_execpath = ts.getExecPath(subpath)
158         if dirContainsTestSuite(file_execpath, litConfig):
159             sub_ts, subpath_in_suite = getTestSuite(file_execpath, litConfig,
160                                                     testSuiteCache)
161         elif dirContainsTestSuite(file_sourcepath, litConfig):
162             sub_ts, subpath_in_suite = getTestSuite(file_sourcepath, litConfig,
163                                                     testSuiteCache)
164         else:
165             sub_ts = None
166
167         # If the this directory recursively maps back to the current test suite,
168         # disregard it (this can happen if the exec root is located inside the
169         # current test suite, for example).
170         if sub_ts is ts:
171             continue
172
173         # Otherwise, load from the nested test suite, if present.
174         if sub_ts is not None:
175             subiter = getTestsInSuite(sub_ts, subpath_in_suite, litConfig,
176                                       testSuiteCache, localConfigCache)
177         else:
178             subiter = getTestsInSuite(ts, subpath, litConfig, testSuiteCache,
179                                       localConfigCache)
180
181         N = 0
182         for res in subiter:
183             N += 1
184             yield res
185         if sub_ts and not N:
186             litConfig.warning('test suite %r contained no tests' % sub_ts.name)
187
188 def find_tests_for_inputs(lit_config, inputs):
189     """
190     find_tests_for_inputs(lit_config, inputs) -> [Test]
191
192     Given a configuration object and a list of input specifiers, find all the
193     tests to execute.
194     """
195
196     # Expand '@...' form in inputs.
197     actual_inputs = []
198     for input in inputs:
199         if os.path.exists(input) or not input.startswith('@'):
200             actual_inputs.append(input)
201         else:
202             f = open(input[1:])
203             try:
204                 for ln in f:
205                     ln = ln.strip()
206                     if ln:
207                         actual_inputs.append(ln)
208             finally:
209                 f.close()
210                     
211     # Load the tests from the inputs.
212     tests = []
213     test_suite_cache = {}
214     local_config_cache = {}
215     for input in actual_inputs:
216         prev = len(tests)
217         tests.extend(getTests(input, lit_config,
218                               test_suite_cache, local_config_cache)[1])
219         if prev == len(tests):
220             lit_config.warning('input %r contained no tests' % input)
221
222     # If there were any errors during test discovery, exit now.
223     if lit_config.numErrors:
224         sys.stderr.write('%d errors, exiting.\n' % lit_config.numErrors)
225         sys.exit(2)
226
227     return tests
228
229 def load_test_suite(inputs):
230     import platform
231     import unittest
232     from lit.LitTestCase import LitTestCase
233
234     # Create the global config object.
235     litConfig = LitConfig.LitConfig(progname = 'lit',
236                                     path = [],
237                                     quiet = False,
238                                     useValgrind = False,
239                                     valgrindLeakCheck = False,
240                                     valgrindArgs = [],
241                                     noExecute = False,
242                                     debug = False,
243                                     isWindows = (platform.system()=='Windows'),
244                                     params = {})
245
246     tests = find_tests_for_inputs(litConfig, inputs)
247
248     # Return a unittest test suite which just runs the tests in order.
249     return unittest.TestSuite([LitTestCase(test, litConfig) for test in tests])