Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | A simple testing framework for lldb using python's unit testing framework. |
| 5 | |
| 6 | Tests for lldb are written as python scripts which take advantage of the script |
| 7 | bridging provided by LLDB.framework to interact with lldb core. |
| 8 | |
| 9 | A specific naming pattern is followed by the .py script to be recognized as |
| 10 | a module which implements a test scenario, namely, Test*.py. |
| 11 | |
| 12 | To specify the directories where "Test*.py" python test scripts are located, |
| 13 | you need to pass in a list of directory names. By default, the current |
| 14 | working directory is searched if nothing is specified on the command line. |
| 15 | """ |
| 16 | |
| 17 | import os |
| 18 | import sys |
| 19 | import unittest |
| 20 | |
| 21 | # |
| 22 | # Global variables: |
| 23 | # |
| 24 | |
| 25 | # The test suite. |
| 26 | suite = unittest.TestSuite() |
| 27 | |
| 28 | # Default verbosity is 0. |
| 29 | verbose = 0 |
| 30 | |
| 31 | # By default, search from the current working directory. |
| 32 | testdirs = [ os.getcwd() ] |
| 33 | |
| 34 | |
| 35 | def usage(): |
| 36 | print """ |
| 37 | Usage: dotest.py [option] [args] |
| 38 | where options: |
| 39 | -h : print this help message and exit (also --help) |
| 40 | -v : do verbose mode of unittest framework |
| 41 | |
| 42 | and: |
| 43 | args : specify a list of directory names to search for python Test*.py scripts |
| 44 | if empty, search from the curret working directory, instead |
| 45 | """ |
| 46 | |
| 47 | |
| 48 | def setupSysPath(): |
| 49 | """Add LLDB.framework/Resources/Python to the search paths for modules.""" |
| 50 | |
| 51 | # Get the directory containing the current script. |
| 52 | testPath = sys.path[0] |
| 53 | if not testPath.endswith('test'): |
| 54 | print "This script expects to reside in lldb's test directory." |
| 55 | sys.exit(-1) |
| 56 | |
| 57 | base = os.path.abspath(os.path.join(testPath, os.pardir)) |
| 58 | dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework', |
| 59 | 'Resources', 'Python') |
| 60 | relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework', |
| 61 | 'Resources', 'Python') |
| 62 | |
| 63 | lldbPath = None |
| 64 | if os.path.isfile(os.path.join(dbgPath, 'lldb.py')): |
| 65 | lldbPath = dbgPath |
| 66 | elif os.path.isfile(os.path.join(relPath, 'lldb.py')): |
| 67 | lldbPath = relPath |
| 68 | |
| 69 | if not lldbPath: |
| 70 | print 'This script requires lldb.py to be in either ' + dbgPath, |
| 71 | print ' or' + relPath |
| 72 | sys.exit(-1) |
| 73 | |
| 74 | sys.path.append(lldbPath) |
| 75 | #print 'sys.path =', sys.path |
| 76 | |
| 77 | |
| 78 | def initTestdirs(): |
| 79 | """Initialize the list of directories containing our unittest scripts. |
| 80 | |
| 81 | '-h/--help as the first option prints out usage info and exit the program. |
| 82 | """ |
| 83 | |
| 84 | global verbose |
| 85 | global testdirs |
| 86 | |
| 87 | if len(sys.argv) == 1: |
| 88 | pass |
| 89 | elif sys.argv[1].find('-h') != -1: |
| 90 | # Print usage info and exit. |
| 91 | usage() |
| 92 | sys.exit(0) |
| 93 | else: |
| 94 | # Process possible verbose flag. |
| 95 | index = 1 |
| 96 | if sys.argv[1].find('-v') != -1: |
| 97 | verbose = 2 |
| 98 | index += 1 |
| 99 | |
| 100 | # Gather all the dirs passed on the command line. |
| 101 | if len(sys.argv) > index: |
| 102 | testdirs = map(os.path.abspath, sys.argv[index:]) |
| 103 | |
| 104 | #print "testdirs =", testdirs |
| 105 | |
| 106 | |
| 107 | def visit(prefix, dir, names): |
| 108 | """Visitor function for os.path.walk(path, visit, arg).""" |
| 109 | |
| 110 | global suite |
| 111 | |
| 112 | for name in names: |
| 113 | if os.path.isdir(os.path.join(dir, name)): |
| 114 | continue |
| 115 | |
| 116 | if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): |
| 117 | # We found a pattern match for our test case. Add it to the suite. |
| 118 | sys.path.append(dir) |
| 119 | base = os.path.splitext(name)[0] |
| 120 | suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base)) |
| 121 | |
| 122 | |
| 123 | # |
| 124 | # Start the actions by first setting up the module search path for lldb, |
| 125 | # followed by initializing the test directories, and then walking the directory |
| 126 | # trees, while collecting the tests into our test suite. |
| 127 | # |
| 128 | setupSysPath() |
| 129 | initTestdirs() |
| 130 | for testdir in testdirs: |
| 131 | os.path.walk(testdir, visit, 'Test') |
| 132 | |
| 133 | # Now that we have loaded all the test cases, run the whole test suite. |
| 134 | #print "test suite =", suite |
| 135 | #print "verbose =", verbose |
| 136 | unittest.TextTestRunner(verbosity=verbose).run(suite) |