4 lit - LLVM Integrated Tester.
6 See lit.pod for more information.
9 import math, os, platform, random, re, sys, time, threading, traceback
15 from TestingConfig import TestingConfig
19 # Configuration files to look for when discovering test suites. These can be
20 # overridden with --config-prefix.
22 # FIXME: Rename to 'config.lit', 'site.lit', and 'local.lit' ?
23 gConfigName = 'lit.cfg'
24 gSiteConfigName = 'lit.site.cfg'
26 kLocalConfigName = 'lit.local.cfg'
28 class TestingProgressDisplay:
29 def __init__(self, opts, numTests, progressBar=None):
31 self.numTests = numTests
33 self.lock = threading.Lock()
34 self.progressBar = progressBar
37 def update(self, test):
38 # Avoid locking overhead in quiet mode
39 if self.opts.quiet and not test.result.isFailure:
46 self.handleUpdate(test)
52 self.progressBar.clear()
55 elif self.opts.succinct:
56 sys.stdout.write('\n')
58 def handleUpdate(self, test):
61 self.progressBar.update(float(self.completed)/self.numTests,
64 if self.opts.succinct and not test.result.isFailure:
68 self.progressBar.clear()
70 print '%s: %s (%d of %d)' % (test.result.name, test.getFullName(),
71 self.completed, self.numTests)
73 if test.result.isFailure and self.opts.showOutput:
74 print "%s TEST '%s' FAILED %s" % ('*'*20, test.getFullName(),
82 def __init__(self, tests, maxTime):
83 self.maxTime = maxTime
84 self.iter = iter(tests)
85 self.lock = threading.Lock()
86 self.startTime = time.time()
89 # Check if we have run out of time.
90 if self.maxTime is not None:
91 if time.time() - self.startTime > self.maxTime:
94 # Otherwise take the next test.
97 item = self.iter.next()
103 class Tester(threading.Thread):
104 def __init__(self, litConfig, provider, display):
105 threading.Thread.__init__(self)
106 self.litConfig = litConfig
107 self.provider = provider
108 self.display = display
112 item = self.provider.get()
117 def runTest(self, test):
119 startTime = time.time()
121 result, output = test.config.test_format.execute(test,
123 except KeyboardInterrupt:
124 # This is a sad hack. Unfortunately subprocess goes
125 # bonkers with ctrl-c and we start forking merrily.
126 print '\nCtrl-C detected, goodbye.'
129 if self.litConfig.debug:
131 result = Test.UNRESOLVED
132 output = 'Exception during script execution:\n'
133 output += traceback.format_exc()
135 elapsed = time.time() - startTime
137 test.setResult(result, output, elapsed)
138 self.display.update(test)
140 def dirContainsTestSuite(path):
141 cfgpath = os.path.join(path, gSiteConfigName)
142 if os.path.exists(cfgpath):
144 cfgpath = os.path.join(path, gConfigName)
145 if os.path.exists(cfgpath):
148 def getTestSuite(item, litConfig, cache):
149 """getTestSuite(item, litConfig, cache) -> (suite, relative_path)
151 Find the test suite containing @arg item.
153 @retval (None, ...) - Indicates no test suite contains @arg item.
154 @retval (suite, relative_path) - The suite that @arg item is in, and its
155 relative path inside that suite.
158 # Check for a site config or a lit config.
159 cfgpath = dirContainsTestSuite(path)
161 # If we didn't find a config file, keep looking.
163 parent,base = os.path.split(path)
167 ts, relative = search(parent)
168 return (ts, relative + (base,))
170 # We found a config file, load it.
172 litConfig.note('loading suite config %r' % cfgpath)
174 cfg = TestingConfig.frompath(cfgpath, None, litConfig, mustExist = True)
175 source_root = os.path.realpath(cfg.test_source_root or path)
176 exec_root = os.path.realpath(cfg.test_exec_root or path)
177 return Test.TestSuite(cfg.name, source_root, exec_root, cfg), ()
180 # Check for an already instantiated test suite.
181 res = cache.get(path)
183 cache[path] = res = search1(path)
186 # Canonicalize the path.
187 item = os.path.realpath(item)
189 # Skip files and virtual components.
191 while not os.path.isdir(item):
192 parent,base = os.path.split(item)
195 components.append(base)
199 ts, relative = search(item)
200 return ts, tuple(relative + tuple(components))
202 def getLocalConfig(ts, path_in_suite, litConfig, cache):
203 def search1(path_in_suite):
204 # Get the parent config.
205 if not path_in_suite:
208 parent = search(path_in_suite[:-1])
210 # Load the local configuration.
211 source_path = ts.getSourcePath(path_in_suite)
212 cfgpath = os.path.join(source_path, kLocalConfigName)
214 litConfig.note('loading local config %r' % cfgpath)
215 return TestingConfig.frompath(cfgpath, parent, litConfig,
217 config = parent.clone(cfgpath))
219 def search(path_in_suite):
220 key = (ts, path_in_suite)
223 cache[key] = res = search1(path_in_suite)
226 return search(path_in_suite)
228 def getTests(path, litConfig, testSuiteCache, localConfigCache):
229 # Find the test suite for this input and its relative path.
230 ts,path_in_suite = getTestSuite(path, litConfig, testSuiteCache)
232 litConfig.warning('unable to find test suite for %r' % path)
236 litConfig.note('resolved input %r to %r::%r' % (path, ts.name,
239 return ts, getTestsInSuite(ts, path_in_suite, litConfig,
240 testSuiteCache, localConfigCache)
242 def getTestsInSuite(ts, path_in_suite, litConfig,
243 testSuiteCache, localConfigCache):
244 # Check that the source path exists (errors here are reported by the
246 source_path = ts.getSourcePath(path_in_suite)
247 if not os.path.exists(source_path):
250 # Check if the user named a test directly.
251 if not os.path.isdir(source_path):
252 lc = getLocalConfig(ts, path_in_suite[:-1], litConfig, localConfigCache)
253 yield Test.Test(ts, path_in_suite, lc)
256 # Otherwise we have a directory to search for tests, start by getting the
257 # local configuration.
258 lc = getLocalConfig(ts, path_in_suite, litConfig, localConfigCache)
261 if lc.test_format is not None:
262 for res in lc.test_format.getTestsInDirectory(ts, path_in_suite,
266 # Search subdirectories.
267 for filename in os.listdir(source_path):
268 # FIXME: This doesn't belong here?
269 if filename in ('Output', '.svn') or filename in lc.excludes:
272 # Ignore non-directories.
273 file_sourcepath = os.path.join(source_path, filename)
274 if not os.path.isdir(file_sourcepath):
277 # Check for nested test suites, first in the execpath in case there is a
278 # site configuration and then in the source path.
279 file_execpath = ts.getExecPath(path_in_suite + (filename,))
280 if dirContainsTestSuite(file_execpath):
281 sub_ts, subiter = getTests(file_execpath, litConfig,
282 testSuiteCache, localConfigCache)
283 elif dirContainsTestSuite(file_sourcepath):
284 sub_ts, subiter = getTests(file_sourcepath, litConfig,
285 testSuiteCache, localConfigCache)
287 # Otherwise, continue loading from inside this test suite.
288 subiter = getTestsInSuite(ts, path_in_suite + (filename,),
289 litConfig, testSuiteCache,
298 litConfig.warning('test suite %r contained no tests' % sub_ts.name)
300 def runTests(numThreads, litConfig, provider, display):
301 # If only using one testing thread, don't use threads at all; this lets us
302 # profile, among other things.
304 t = Tester(litConfig, provider, display)
308 # Otherwise spin up the testing threads and wait for them to finish.
309 testers = [Tester(litConfig, provider, display)
310 for i in range(numThreads)]
316 except KeyboardInterrupt:
319 def load_test_suite(inputs):
322 # Create the global config object.
323 litConfig = LitConfig.LitConfig(progname = 'lit',
327 valgrindLeakCheck = False,
332 isWindows = (platform.system()=='Windows'),
335 # Load the tests from the inputs.
338 localConfigCache = {}
341 tests.extend(getTests(input, litConfig,
342 testSuiteCache, localConfigCache)[1])
343 if prev == len(tests):
344 litConfig.warning('input %r contained no tests' % input)
346 # If there were any errors during test discovery, exit now.
347 if litConfig.numErrors:
348 print >>sys.stderr, '%d errors, exiting.' % litConfig.numErrors
351 # Return a unittest test suite which just runs the tests in order.
352 def get_test_fn(test):
353 return unittest.FunctionTestCase(
354 lambda: test.config.test_format.execute(
356 description = test.getFullName())
358 from LitTestCase import LitTestCase
359 return unittest.TestSuite([LitTestCase(test, litConfig) for test in tests])
362 # Bump the GIL check interval, its more important to get any one thread to a
363 # blocking operation (hopefully exec) than to try and unblock other threads.
365 # FIXME: This is a hack.
367 sys.setcheckinterval(1000)
370 from optparse import OptionParser, OptionGroup
371 parser = OptionParser("usage: %prog [options] {file-or-path}")
373 parser.add_option("-j", "--threads", dest="numThreads", metavar="N",
374 help="Number of testing threads",
375 type=int, action="store", default=None)
376 parser.add_option("", "--config-prefix", dest="configPrefix",
377 metavar="NAME", help="Prefix for 'lit' config files",
378 action="store", default=None)
379 parser.add_option("", "--param", dest="userParameters",
381 help="Add 'NAME' = 'VAL' to the user defined parameters",
382 type=str, action="append", default=[])
384 group = OptionGroup(parser, "Output Format")
385 # FIXME: I find these names very confusing, although I like the
387 group.add_option("-q", "--quiet", dest="quiet",
388 help="Suppress no error output",
389 action="store_true", default=False)
390 group.add_option("-s", "--succinct", dest="succinct",
391 help="Reduce amount of output",
392 action="store_true", default=False)
393 group.add_option("-v", "--verbose", dest="showOutput",
394 help="Show all test output",
395 action="store_true", default=False)
396 group.add_option("", "--no-progress-bar", dest="useProgressBar",
397 help="Do not use curses based progress bar",
398 action="store_false", default=True)
399 parser.add_option_group(group)
401 group = OptionGroup(parser, "Test Execution")
402 group.add_option("", "--path", dest="path",
403 help="Additional paths to add to testing environment",
404 action="append", type=str, default=[])
405 group.add_option("", "--vg", dest="useValgrind",
406 help="Run tests under valgrind",
407 action="store_true", default=False)
408 group.add_option("", "--vg-leak", dest="valgrindLeakCheck",
409 help="Check for memory leaks under valgrind",
410 action="store_true", default=False)
411 group.add_option("", "--vg-arg", dest="valgrindArgs", metavar="ARG",
412 help="Specify an extra argument for valgrind",
413 type=str, action="append", default=[])
414 group.add_option("", "--time-tests", dest="timeTests",
415 help="Track elapsed wall time for each test",
416 action="store_true", default=False)
417 group.add_option("", "--no-execute", dest="noExecute",
418 help="Don't execute any tests (assume PASS)",
419 action="store_true", default=False)
420 parser.add_option_group(group)
422 group = OptionGroup(parser, "Test Selection")
423 group.add_option("", "--max-tests", dest="maxTests", metavar="N",
424 help="Maximum number of tests to run",
425 action="store", type=int, default=None)
426 group.add_option("", "--max-time", dest="maxTime", metavar="N",
427 help="Maximum time to spend testing (in seconds)",
428 action="store", type=float, default=None)
429 group.add_option("", "--shuffle", dest="shuffle",
430 help="Run tests in random order",
431 action="store_true", default=False)
432 parser.add_option_group(group)
434 group = OptionGroup(parser, "Debug and Experimental Options")
435 group.add_option("", "--debug", dest="debug",
436 help="Enable debugging (for 'lit' development)",
437 action="store_true", default=False)
438 group.add_option("", "--show-suites", dest="showSuites",
439 help="Show discovered test suites",
440 action="store_true", default=False)
441 group.add_option("", "--no-tcl-as-sh", dest="useTclAsSh",
442 help="Don't run Tcl scripts using 'sh'",
443 action="store_false", default=True)
444 group.add_option("", "--repeat", dest="repeatTests", metavar="N",
445 help="Repeat tests N times (for timing)",
446 action="store", default=None, type=int)
447 parser.add_option_group(group)
449 (opts, args) = parser.parse_args()
452 parser.error('No inputs specified')
454 if opts.configPrefix is not None:
455 global gConfigName, gSiteConfigName
456 gConfigName = '%s.cfg' % opts.configPrefix
457 gSiteConfigName = '%s.site.cfg' % opts.configPrefix
459 if opts.numThreads is None:
460 # Python <2.5 has a race condition causing lit to always fail with numThreads>1
461 # http://bugs.python.org/issue1731717
462 # I haven't seen this bug occur with 2.5.2 and later, so only enable multiple
463 # threads by default there.
464 if sys.hexversion >= 0x2050200:
465 opts.numThreads = Util.detectCPUs()
471 # Create the user defined parameters.
473 for entry in opts.userParameters:
477 name,val = entry.split('=', 1)
478 userParams[name] = val
480 # Create the global config object.
481 litConfig = LitConfig.LitConfig(progname = os.path.basename(sys.argv[0]),
484 useValgrind = opts.useValgrind,
485 valgrindLeakCheck = opts.valgrindLeakCheck,
486 valgrindArgs = opts.valgrindArgs,
487 useTclAsSh = opts.useTclAsSh,
488 noExecute = opts.noExecute,
490 isWindows = (platform.system()=='Windows'),
493 # Load the tests from the inputs.
496 localConfigCache = {}
499 tests.extend(getTests(input, litConfig,
500 testSuiteCache, localConfigCache)[1])
501 if prev == len(tests):
502 litConfig.warning('input %r contained no tests' % input)
504 # If there were any errors during test discovery, exit now.
505 if litConfig.numErrors:
506 print >>sys.stderr, '%d errors, exiting.' % litConfig.numErrors
510 suitesAndTests = dict([(ts,[])
511 for ts,_ in testSuiteCache.values()
514 suitesAndTests[t.suite].append(t)
516 print '-- Test Suites --'
517 suitesAndTests = suitesAndTests.items()
518 suitesAndTests.sort(key = lambda (ts,_): ts.name)
519 for ts,ts_tests in suitesAndTests:
520 print ' %s - %d tests' %(ts.name, len(ts_tests))
521 print ' Source Root: %s' % ts.source_root
522 print ' Exec Root : %s' % ts.exec_root
524 # Select and order the tests.
525 numTotalTests = len(tests)
527 random.shuffle(tests)
529 tests.sort(key = lambda t: t.getFullName())
530 if opts.maxTests is not None:
531 tests = tests[:opts.maxTests]
534 if len(tests) != numTotalTests:
535 extra = ' of %d' % numTotalTests
536 header = '-- Testing: %d%s tests, %d threads --'%(len(tests),extra,
540 tests = [t.copyWithIndex(i)
542 for i in range(opts.repeatTests)]
546 if opts.succinct and opts.useProgressBar:
548 tc = ProgressBar.TerminalController()
549 progressBar = ProgressBar.ProgressBar(tc, header)
552 progressBar = ProgressBar.SimpleProgressBar('Testing: ')
556 # Don't create more threads than tests.
557 opts.numThreads = min(len(tests), opts.numThreads)
559 startTime = time.time()
560 display = TestingProgressDisplay(opts, len(tests), progressBar)
561 provider = TestProvider(tests, opts.maxTime)
562 runTests(opts.numThreads, litConfig, provider, display)
566 print 'Testing Time: %.2fs'%(time.time() - startTime)
568 # Update results for any tests which weren't run.
571 t.setResult(Test.UNRESOLVED, '', 0.0)
573 # List test results organized by kind.
577 if t.result not in byCode:
578 byCode[t.result] = []
579 byCode[t.result].append(t)
580 if t.result.isFailure:
583 # FIXME: Show unresolved and (optionally) unsupported tests.
584 for title,code in (('Unexpected Passing Tests', Test.XPASS),
585 ('Failing Tests', Test.FAIL)):
586 elts = byCode.get(code)
590 print '%s (%d):' % (title, len(elts))
592 print ' %s' % t.getFullName()
596 # Collate, in case we repeated tests.
599 key = t.getFullName()
600 times[key] = times.get(key, 0.) + t.elapsed
602 byTime = list(times.items())
603 byTime.sort(key = lambda (name,elapsed): elapsed)
605 Util.printHistogram(byTime, title='Tests')
607 for name,code in (('Expected Passes ', Test.PASS),
608 ('Expected Failures ', Test.XFAIL),
609 ('Unsupported Tests ', Test.UNSUPPORTED),
610 ('Unresolved Tests ', Test.UNRESOLVED),
611 ('Unexpected Passes ', Test.XPASS),
612 ('Unexpected Failures', Test.FAIL),):
613 if opts.quiet and not code.isFailure:
615 N = len(byCode.get(code,[]))
617 print ' %s: %d' % (name,N)
619 # If we encountered any additional errors, exit abnormally.
620 if litConfig.numErrors:
621 print >>sys.stderr, '\n%d error(s), exiting.' % litConfig.numErrors
624 # Warn about warnings.
625 if litConfig.numWarnings:
626 print >>sys.stderr, '\n%d warning(s) in tests.' % litConfig.numWarnings
632 if __name__=='__main__':