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 |
Johnny Chen | 16b9747 | 2010-06-29 23:17:15 +0000 | [diff] [blame] | 19 | import time |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 20 | import unittest2 |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 21 | |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 22 | class _WritelnDecorator(object): |
| 23 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 24 | def __init__(self,stream): |
| 25 | self.stream = stream |
| 26 | |
| 27 | def __getattr__(self, attr): |
| 28 | if attr in ('stream', '__getstate__'): |
| 29 | raise AttributeError(attr) |
| 30 | return getattr(self.stream,attr) |
| 31 | |
| 32 | def writeln(self, arg=None): |
| 33 | if arg: |
| 34 | self.write(arg) |
| 35 | self.write('\n') # text-mode streams translate to \r\n if needed |
| 36 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 37 | # |
| 38 | # Global variables: |
| 39 | # |
| 40 | |
| 41 | # The test suite. |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 42 | suite = unittest2.TestSuite() |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 43 | |
| 44 | # Default verbosity is 0. |
| 45 | verbose = 0 |
| 46 | |
| 47 | # By default, search from the current working directory. |
| 48 | testdirs = [ os.getcwd() ] |
| 49 | |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 50 | # Separator string. |
| 51 | separator = '-' * 70 |
| 52 | |
| 53 | # Decorated sys.stdout. |
| 54 | out = _WritelnDecorator(sys.stdout) |
| 55 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 56 | |
| 57 | def usage(): |
| 58 | print """ |
| 59 | Usage: dotest.py [option] [args] |
| 60 | where options: |
| 61 | -h : print this help message and exit (also --help) |
| 62 | -v : do verbose mode of unittest framework |
| 63 | |
| 64 | and: |
| 65 | args : specify a list of directory names to search for python Test*.py scripts |
| 66 | if empty, search from the curret working directory, instead |
Johnny Chen | 58f9392 | 2010-06-29 23:10:39 +0000 | [diff] [blame] | 67 | |
| 68 | Running of this script also sets up the LLDB_TEST environment variable so that |
| 69 | individual test cases can locate their supporting files correctly. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 70 | """ |
| 71 | |
| 72 | |
| 73 | def setupSysPath(): |
| 74 | """Add LLDB.framework/Resources/Python to the search paths for modules.""" |
| 75 | |
| 76 | # Get the directory containing the current script. |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 77 | scriptPath = sys.path[0] |
| 78 | if not scriptPath.endswith('test'): |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 79 | print "This script expects to reside in lldb's test directory." |
| 80 | sys.exit(-1) |
| 81 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 82 | os.environ["LLDB_TEST"] = scriptPath |
Johnny Chen | 58f9392 | 2010-06-29 23:10:39 +0000 | [diff] [blame] | 83 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 84 | base = os.path.abspath(os.path.join(scriptPath, os.pardir)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 85 | dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework', |
| 86 | 'Resources', 'Python') |
| 87 | relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework', |
| 88 | 'Resources', 'Python') |
| 89 | |
| 90 | lldbPath = None |
| 91 | if os.path.isfile(os.path.join(dbgPath, 'lldb.py')): |
| 92 | lldbPath = dbgPath |
| 93 | elif os.path.isfile(os.path.join(relPath, 'lldb.py')): |
| 94 | lldbPath = relPath |
| 95 | |
| 96 | if not lldbPath: |
| 97 | print 'This script requires lldb.py to be in either ' + dbgPath, |
| 98 | print ' or' + relPath |
| 99 | sys.exit(-1) |
| 100 | |
| 101 | sys.path.append(lldbPath) |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 102 | sys.path.append(scriptPath) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def initTestdirs(): |
| 106 | """Initialize the list of directories containing our unittest scripts. |
| 107 | |
| 108 | '-h/--help as the first option prints out usage info and exit the program. |
| 109 | """ |
| 110 | |
| 111 | global verbose |
| 112 | global testdirs |
| 113 | |
| 114 | if len(sys.argv) == 1: |
| 115 | pass |
| 116 | elif sys.argv[1].find('-h') != -1: |
| 117 | # Print usage info and exit. |
| 118 | usage() |
| 119 | sys.exit(0) |
| 120 | else: |
| 121 | # Process possible verbose flag. |
| 122 | index = 1 |
| 123 | if sys.argv[1].find('-v') != -1: |
| 124 | verbose = 2 |
| 125 | index += 1 |
| 126 | |
| 127 | # Gather all the dirs passed on the command line. |
| 128 | if len(sys.argv) > index: |
| 129 | testdirs = map(os.path.abspath, sys.argv[index:]) |
| 130 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 131 | |
| 132 | def visit(prefix, dir, names): |
| 133 | """Visitor function for os.path.walk(path, visit, arg).""" |
| 134 | |
| 135 | global suite |
| 136 | |
| 137 | for name in names: |
| 138 | if os.path.isdir(os.path.join(dir, name)): |
| 139 | continue |
| 140 | |
| 141 | if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): |
| 142 | # We found a pattern match for our test case. Add it to the suite. |
Johnny Chen | a85d7ee | 2010-06-26 00:19:32 +0000 | [diff] [blame] | 143 | if not sys.path.count(dir): |
| 144 | sys.path.append(dir) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 145 | base = os.path.splitext(name)[0] |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 146 | suite.addTests(unittest2.defaultTestLoader.loadTestsFromName(base)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | # |
| 150 | # Start the actions by first setting up the module search path for lldb, |
| 151 | # followed by initializing the test directories, and then walking the directory |
| 152 | # trees, while collecting the tests into our test suite. |
| 153 | # |
| 154 | setupSysPath() |
| 155 | initTestdirs() |
| 156 | for testdir in testdirs: |
| 157 | os.path.walk(testdir, visit, 'Test') |
| 158 | |
| 159 | # Now that we have loaded all the test cases, run the whole test suite. |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 160 | out.writeln(separator) |
| 161 | out.writeln("Collected %d test%s" % (suite.countTestCases(), |
| 162 | suite.countTestCases() != 1 and "s" or "")) |
| 163 | out.writeln() |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 164 | |
| 165 | # For the time being, let's bracket the test runner within the |
| 166 | # lldb.SBDebugger.Initialize()/Terminate() pair. |
| 167 | import lldb |
| 168 | lldb.SBDebugger.Initialize() |
| 169 | |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 170 | # Create a singleton SBDebugger in the lldb namespace. |
| 171 | lldb.DBG = lldb.SBDebugger.Create() |
| 172 | |
| 173 | # Turn on logging for debugging purposes if ${LLDB_LOG} environment variable is |
| 174 | # is defined. Use ${LLDB_LOG} to specify the log file. |
| 175 | ci = lldb.DBG.GetCommandInterpreter() |
| 176 | res = lldb.SBCommandReturnObject() |
| 177 | if ("LLDB_LOG" in os.environ): |
| 178 | ci.HandleCommand( |
| 179 | "log enable -f " + os.environ["LLDB_LOG"] + " lldb default", res) |
Johnny Chen | 797674b | 2010-07-02 20:35:23 +0000 | [diff] [blame] | 180 | if not res.Succeeded(): |
| 181 | raise Exception('log enable failed (check your LLDB_LOG env variable...') |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 182 | |
Johnny Chen | 7987ac9 | 2010-08-09 20:40:52 +0000 | [diff] [blame^] | 183 | # Install the control-c handler. |
| 184 | unittest2.signals.installHandler() |
| 185 | |
| 186 | # Invoke the default TextTestRunner to run the test suite. |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 187 | unittest2.TextTestRunner(verbosity=verbose).run(suite) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 188 | |
Johnny Chen | 16b9747 | 2010-06-29 23:17:15 +0000 | [diff] [blame] | 189 | # Add some delay before calling SBDebugger.Terminate(). |
| 190 | time.sleep(1) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 191 | lldb.SBDebugger.Terminate() |