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