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. |
Johnny Chen | 872aee1 | 2010-09-16 15:44:23 +0000 | [diff] [blame] | 15 | |
| 16 | Type: |
| 17 | |
| 18 | ./dotest.py -h |
| 19 | |
| 20 | for available options. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 21 | """ |
| 22 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 23 | import os, signal, sys, time |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 24 | import unittest2 |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 25 | |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 26 | class _WritelnDecorator(object): |
| 27 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 28 | def __init__(self,stream): |
| 29 | self.stream = stream |
| 30 | |
| 31 | def __getattr__(self, attr): |
| 32 | if attr in ('stream', '__getstate__'): |
| 33 | raise AttributeError(attr) |
| 34 | return getattr(self.stream,attr) |
| 35 | |
| 36 | def writeln(self, arg=None): |
| 37 | if arg: |
| 38 | self.write(arg) |
| 39 | self.write('\n') # text-mode streams translate to \r\n if needed |
| 40 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 41 | # |
| 42 | # Global variables: |
| 43 | # |
| 44 | |
| 45 | # The test suite. |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 46 | suite = unittest2.TestSuite() |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 47 | |
Johnny Chen | 9fdb0a9 | 2010-09-18 00:16:47 +0000 | [diff] [blame] | 48 | # The config file is optional. |
| 49 | configFile = None |
| 50 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 51 | # The dictionary as a result of sourcing configFile. |
| 52 | config = {} |
| 53 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 54 | # Delay startup in order for the debugger to attach. |
| 55 | delay = False |
| 56 | |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 57 | # The filter (testcase.testmethod) used to admit tests into our test suite. |
| 58 | filterspec = None |
| 59 | |
| 60 | # If '-g' is specified, the filterspec must be consulted for each test module, default to False. |
| 61 | fs4all = False |
| 62 | |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 63 | # Ignore the build search path relative to this script to locate the lldb.py module. |
| 64 | ignore = False |
| 65 | |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 66 | # By default, we skip long running test case. Use '-l' option to override. |
Johnny Chen | 4199819 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 67 | skipLongRunningTest = True |
| 68 | |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 69 | # The regular expression pattern to match against eligible filenames as our test cases. |
| 70 | regexp = None |
| 71 | |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 72 | # By default, tests are executed in place and cleanups are performed afterwards. |
| 73 | # Use '-r dir' option to relocate the tests and their intermediate files to a |
| 74 | # different directory and to forgo any cleanups. The directory specified must |
| 75 | # not exist yet. |
| 76 | rdir = None |
| 77 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 78 | # Default verbosity is 0. |
| 79 | verbose = 0 |
| 80 | |
| 81 | # By default, search from the current working directory. |
| 82 | testdirs = [ os.getcwd() ] |
| 83 | |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 84 | # Separator string. |
| 85 | separator = '-' * 70 |
| 86 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 87 | |
| 88 | def usage(): |
| 89 | print """ |
| 90 | Usage: dotest.py [option] [args] |
| 91 | where options: |
| 92 | -h : print this help message and exit (also --help) |
Johnny Chen | 9fdb0a9 | 2010-09-18 00:16:47 +0000 | [diff] [blame] | 93 | -c : read a config file specified after this option |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 94 | (see also lldb-trunk/example/test/usage-config) |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 95 | -d : delay startup for 10 seconds (in order for the debugger to attach) |
Johnny Chen | 46be75d | 2010-10-11 16:19:48 +0000 | [diff] [blame] | 96 | -f : specify a filter, which consists of the test class name, a dot, followed by |
| 97 | the test method, to admit tests into the test suite |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 98 | e.g., -f 'ClassTypesTestCase.test_with_dwarf_and_python_api' |
| 99 | -g : if specified, only the modules with the corect filter will be run |
| 100 | it has no effect if no '-f' option is present |
| 101 | '-f filterspec -g' can be used with '-p filename-regexp' to select only |
| 102 | the testfile.testclass.testmethod to run |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 103 | -i : ignore (don't bailout) if 'lldb.py' module cannot be located in the build |
| 104 | tree relative to this script; use PYTHONPATH to locate the module |
Johnny Chen | 4199819 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 105 | -l : don't skip long running test |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 106 | -p : specify a regexp filename pattern for inclusion in the test suite |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 107 | -r : specify a dir to relocate the tests and their intermediate files to; |
| 108 | the directory must not exist before running this test driver; |
| 109 | no cleanup of intermediate test files is performed in this case |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 110 | -t : trace lldb command execution and result |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 111 | -v : do verbose mode of unittest framework |
Johnny Chen | e47649c | 2010-10-07 02:04:14 +0000 | [diff] [blame] | 112 | -w : insert some wait time (currently 0.5 sec) between consecutive test cases |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 113 | |
| 114 | and: |
| 115 | args : specify a list of directory names to search for python Test*.py scripts |
| 116 | if empty, search from the curret working directory, instead |
Johnny Chen | 58f9392 | 2010-06-29 23:10:39 +0000 | [diff] [blame] | 117 | |
| 118 | Running of this script also sets up the LLDB_TEST environment variable so that |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 119 | individual test cases can locate their supporting files correctly. The script |
| 120 | tries to set up Python's search paths for modules by looking at the build tree |
| 121 | relative to this script. See also the '-i' option. |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 122 | |
| 123 | Environment variables related to loggings: |
| 124 | |
| 125 | o LLDB_LOG: if defined, specifies the log file pathname for the 'lldb' subsystem |
| 126 | with a default option of 'event process' if LLDB_LOG_OPTION is not defined. |
| 127 | |
| 128 | o GDB_REMOTE_LOG: if defined, specifies the log file pathname for the |
| 129 | 'process.gdb-remote' subsystem with a default option of 'packets' if |
| 130 | GDB_REMOTE_LOG_OPTION is not defined. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 131 | """ |
Johnny Chen | 9fdb0a9 | 2010-09-18 00:16:47 +0000 | [diff] [blame] | 132 | sys.exit(0) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 133 | |
| 134 | |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 135 | def parseOptionsAndInitTestdirs(): |
| 136 | """Initialize the list of directories containing our unittest scripts. |
| 137 | |
| 138 | '-h/--help as the first option prints out usage info and exit the program. |
| 139 | """ |
| 140 | |
Johnny Chen | 9fdb0a9 | 2010-09-18 00:16:47 +0000 | [diff] [blame] | 141 | global configFile |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 142 | global delay |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 143 | global filterspec |
| 144 | global fs4all |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 145 | global ignore |
Johnny Chen | 4199819 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 146 | global skipLongRunningTest |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 147 | global regexp |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 148 | global rdir |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 149 | global verbose |
| 150 | global testdirs |
| 151 | |
| 152 | if len(sys.argv) == 1: |
| 153 | return |
| 154 | |
| 155 | # Process possible trace and/or verbose flag, among other things. |
| 156 | index = 1 |
Johnny Chen | ce2212c | 2010-10-07 15:41:55 +0000 | [diff] [blame] | 157 | while index < len(sys.argv): |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 158 | if not sys.argv[index].startswith('-'): |
| 159 | # End of option processing. |
| 160 | break |
| 161 | |
| 162 | if sys.argv[index].find('-h') != -1: |
| 163 | usage() |
Johnny Chen | 9fdb0a9 | 2010-09-18 00:16:47 +0000 | [diff] [blame] | 164 | elif sys.argv[index].startswith('-c'): |
| 165 | # Increment by 1 to fetch the config file name option argument. |
| 166 | index += 1 |
| 167 | if index >= len(sys.argv) or sys.argv[index].startswith('-'): |
| 168 | usage() |
| 169 | configFile = sys.argv[index] |
| 170 | if not os.path.isfile(configFile): |
| 171 | print "Config file:", configFile, "does not exist!" |
| 172 | usage() |
| 173 | index += 1 |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 174 | elif sys.argv[index].startswith('-d'): |
| 175 | delay = True |
| 176 | index += 1 |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 177 | elif sys.argv[index].startswith('-f'): |
| 178 | # Increment by 1 to fetch the filter spec. |
| 179 | index += 1 |
| 180 | if index >= len(sys.argv) or sys.argv[index].startswith('-'): |
| 181 | usage() |
| 182 | filterspec = sys.argv[index] |
| 183 | index += 1 |
| 184 | elif sys.argv[index].startswith('-g'): |
| 185 | fs4all = True |
| 186 | index += 1 |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 187 | elif sys.argv[index].startswith('-i'): |
| 188 | ignore = True |
| 189 | index += 1 |
Johnny Chen | 4199819 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 190 | elif sys.argv[index].startswith('-l'): |
| 191 | skipLongRunningTest = False |
| 192 | index += 1 |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 193 | elif sys.argv[index].startswith('-p'): |
| 194 | # Increment by 1 to fetch the reg exp pattern argument. |
| 195 | index += 1 |
| 196 | if index >= len(sys.argv) or sys.argv[index].startswith('-'): |
| 197 | usage() |
| 198 | regexp = sys.argv[index] |
| 199 | index += 1 |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 200 | elif sys.argv[index].startswith('-r'): |
| 201 | # Increment by 1 to fetch the relocated directory argument. |
| 202 | index += 1 |
| 203 | if index >= len(sys.argv) or sys.argv[index].startswith('-'): |
| 204 | usage() |
| 205 | rdir = os.path.abspath(sys.argv[index]) |
| 206 | if os.path.exists(rdir): |
| 207 | print "Relocated directory:", rdir, "must not exist!" |
| 208 | usage() |
| 209 | index += 1 |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 210 | elif sys.argv[index].startswith('-t'): |
| 211 | os.environ["LLDB_COMMAND_TRACE"] = "YES" |
| 212 | index += 1 |
| 213 | elif sys.argv[index].startswith('-v'): |
| 214 | verbose = 2 |
| 215 | index += 1 |
Johnny Chen | e47649c | 2010-10-07 02:04:14 +0000 | [diff] [blame] | 216 | elif sys.argv[index].startswith('-w'): |
| 217 | os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] = 'YES' |
| 218 | index += 1 |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 219 | else: |
| 220 | print "Unknown option: ", sys.argv[index] |
| 221 | usage() |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 222 | |
| 223 | # Gather all the dirs passed on the command line. |
| 224 | if len(sys.argv) > index: |
| 225 | testdirs = map(os.path.abspath, sys.argv[index:]) |
| 226 | |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 227 | # If '-r dir' is specified, the tests should be run under the relocated |
| 228 | # directory. Let's copy the testdirs over. |
| 229 | if rdir: |
| 230 | from shutil import copytree, ignore_patterns |
| 231 | |
| 232 | tmpdirs = [] |
| 233 | for srcdir in testdirs: |
| 234 | dstdir = os.path.join(rdir, os.path.basename(srcdir)) |
| 235 | # Don't copy the *.pyc and .svn stuffs. |
| 236 | copytree(srcdir, dstdir, ignore=ignore_patterns('*.pyc', '.svn')) |
| 237 | tmpdirs.append(dstdir) |
| 238 | |
| 239 | # This will be our modified testdirs. |
| 240 | testdirs = tmpdirs |
| 241 | |
| 242 | # With '-r dir' specified, there's no cleanup of intermediate test files. |
| 243 | os.environ["LLDB_DO_CLEANUP"] = 'NO' |
| 244 | |
| 245 | # If testdirs is ['test'], the make directory has already been copied |
| 246 | # recursively and is contained within the rdir/test dir. For anything |
| 247 | # else, we would need to copy over the make directory and its contents, |
| 248 | # so that, os.listdir(rdir) looks like, for example: |
| 249 | # |
| 250 | # array_types conditional_break make |
| 251 | # |
| 252 | # where the make directory contains the Makefile.rules file. |
| 253 | if len(testdirs) != 1 or os.path.basename(testdirs[0]) != 'test': |
| 254 | # Don't copy the .svn stuffs. |
| 255 | copytree('make', os.path.join(rdir, 'make'), |
| 256 | ignore=ignore_patterns('.svn')) |
| 257 | |
| 258 | #print "testdirs:", testdirs |
| 259 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 260 | # Source the configFile if specified. |
| 261 | # The side effect, if any, will be felt from this point on. An example |
| 262 | # config file may be these simple two lines: |
| 263 | # |
| 264 | # sys.stderr = open("/tmp/lldbtest-stderr", "w") |
| 265 | # sys.stdout = open("/tmp/lldbtest-stdout", "w") |
| 266 | # |
| 267 | # which will reassign the two file objects to sys.stderr and sys.stdout, |
| 268 | # respectively. |
| 269 | # |
| 270 | # See also lldb-trunk/example/test/usage-config. |
| 271 | global config |
| 272 | if configFile: |
| 273 | # Pass config (a dictionary) as the locals namespace for side-effect. |
| 274 | execfile(configFile, globals(), config) |
| 275 | #print "config:", config |
| 276 | #print "sys.stderr:", sys.stderr |
| 277 | #print "sys.stdout:", sys.stdout |
| 278 | |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 279 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 280 | def setupSysPath(): |
| 281 | """Add LLDB.framework/Resources/Python to the search paths for modules.""" |
| 282 | |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 283 | global rdir |
| 284 | global testdirs |
| 285 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 286 | # Get the directory containing the current script. |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 287 | scriptPath = sys.path[0] |
| 288 | if not scriptPath.endswith('test'): |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 289 | print "This script expects to reside in lldb's test directory." |
| 290 | sys.exit(-1) |
| 291 | |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 292 | if rdir: |
| 293 | # Set up the LLDB_TEST environment variable appropriately, so that the |
| 294 | # individual tests can be located relatively. |
| 295 | # |
| 296 | # See also lldbtest.TestBase.setUpClass(cls). |
| 297 | if len(testdirs) == 1 and os.path.basename(testdirs[0]) == 'test': |
| 298 | os.environ["LLDB_TEST"] = os.path.join(rdir, 'test') |
| 299 | else: |
| 300 | os.environ["LLDB_TEST"] = rdir |
| 301 | else: |
| 302 | os.environ["LLDB_TEST"] = scriptPath |
Johnny Chen | 9de4ede | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 303 | pluginPath = os.path.join(scriptPath, 'plugins') |
Johnny Chen | 58f9392 | 2010-06-29 23:10:39 +0000 | [diff] [blame] | 304 | |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 305 | # Append script dir and plugin dir to the sys.path. |
| 306 | sys.path.append(scriptPath) |
| 307 | sys.path.append(pluginPath) |
| 308 | |
| 309 | global ignore |
| 310 | |
| 311 | # The '-i' option is used to skip looking for lldb.py in the build tree. |
| 312 | if ignore: |
| 313 | return |
| 314 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 315 | base = os.path.abspath(os.path.join(scriptPath, os.pardir)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 316 | dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework', |
| 317 | 'Resources', 'Python') |
| 318 | relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework', |
| 319 | 'Resources', 'Python') |
Johnny Chen | c202c46 | 2010-09-15 18:11:19 +0000 | [diff] [blame] | 320 | baiPath = os.path.join(base, 'build', 'BuildAndIntegration', |
| 321 | 'LLDB.framework', 'Resources', 'Python') |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 322 | |
| 323 | lldbPath = None |
| 324 | if os.path.isfile(os.path.join(dbgPath, 'lldb.py')): |
| 325 | lldbPath = dbgPath |
| 326 | elif os.path.isfile(os.path.join(relPath, 'lldb.py')): |
| 327 | lldbPath = relPath |
Johnny Chen | c202c46 | 2010-09-15 18:11:19 +0000 | [diff] [blame] | 328 | elif os.path.isfile(os.path.join(baiPath, 'lldb.py')): |
| 329 | lldbPath = baiPath |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 330 | |
| 331 | if not lldbPath: |
Johnny Chen | c202c46 | 2010-09-15 18:11:19 +0000 | [diff] [blame] | 332 | print 'This script requires lldb.py to be in either ' + dbgPath + ',', |
| 333 | print relPath + ', or ' + baiPath |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 334 | sys.exit(-1) |
| 335 | |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 336 | # This is to locate the lldb.py module. Insert it right after sys.path[0]. |
| 337 | sys.path[1:1] = [lldbPath] |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 338 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 339 | |
Johnny Chen | cd0279d | 2010-09-20 18:07:50 +0000 | [diff] [blame] | 340 | def doDelay(delta): |
| 341 | """Delaying startup for delta-seconds to facilitate debugger attachment.""" |
| 342 | def alarm_handler(*args): |
| 343 | raise Exception("timeout") |
| 344 | |
| 345 | signal.signal(signal.SIGALRM, alarm_handler) |
| 346 | signal.alarm(delta) |
| 347 | sys.stdout.write("pid=%d\n" % os.getpid()) |
| 348 | sys.stdout.write("Enter RET to proceed (or timeout after %d seconds):" % |
| 349 | delta) |
| 350 | sys.stdout.flush() |
| 351 | try: |
| 352 | text = sys.stdin.readline() |
| 353 | except: |
| 354 | text = "" |
| 355 | signal.alarm(0) |
| 356 | sys.stdout.write("proceeding...\n") |
| 357 | pass |
| 358 | |
| 359 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 360 | def visit(prefix, dir, names): |
| 361 | """Visitor function for os.path.walk(path, visit, arg).""" |
| 362 | |
| 363 | global suite |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 364 | global regexp |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 365 | global filterspec |
| 366 | global fs4all |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 367 | |
| 368 | for name in names: |
| 369 | if os.path.isdir(os.path.join(dir, name)): |
| 370 | continue |
| 371 | |
| 372 | if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): |
Johnny Chen | 7c52ff1 | 2010-09-27 23:29:54 +0000 | [diff] [blame] | 373 | # Try to match the regexp pattern, if specified. |
| 374 | if regexp: |
| 375 | import re |
| 376 | if re.search(regexp, name): |
| 377 | #print "Filename: '%s' matches pattern: '%s'" % (name, regexp) |
| 378 | pass |
| 379 | else: |
| 380 | #print "Filename: '%s' does not match pattern: '%s'" % (name, regexp) |
| 381 | continue |
| 382 | |
Johnny Chen | 953864a | 2010-10-12 21:35:54 +0000 | [diff] [blame] | 383 | # We found a match for our test. Add it to the suite. |
Johnny Chen | 7972335 | 2010-10-12 15:53:22 +0000 | [diff] [blame] | 384 | |
| 385 | # Update the sys.path first. |
Johnny Chen | a85d7ee | 2010-06-26 00:19:32 +0000 | [diff] [blame] | 386 | if not sys.path.count(dir): |
Johnny Chen | 548aefd | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 387 | sys.path.insert(0, dir) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 388 | base = os.path.splitext(name)[0] |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 389 | |
| 390 | # Thoroughly check the filterspec against the base module and admit |
| 391 | # the (base, filterspec) combination only when it makes sense. |
| 392 | if filterspec: |
| 393 | # Optimistically set the flag to True. |
| 394 | filtered = True |
| 395 | module = __import__(base) |
| 396 | parts = filterspec.split('.') |
| 397 | obj = module |
| 398 | for part in parts: |
| 399 | try: |
| 400 | parent, obj = obj, getattr(obj, part) |
| 401 | except AttributeError: |
| 402 | # The filterspec has failed. |
| 403 | filtered = False |
| 404 | break |
| 405 | # Forgo this module if the (base, filterspec) combo is invalid |
| 406 | # and the '-g' option is present. |
| 407 | if fs4all and not filtered: |
| 408 | continue |
| 409 | |
Johnny Chen | 953864a | 2010-10-12 21:35:54 +0000 | [diff] [blame] | 410 | # Add either the filtered test case or the entire test class. |
Johnny Chen | b62436b | 2010-10-06 20:40:56 +0000 | [diff] [blame] | 411 | if filterspec and filtered: |
| 412 | suite.addTests( |
| 413 | unittest2.defaultTestLoader.loadTestsFromName(filterspec, module)) |
| 414 | else: |
| 415 | # A simple case of just the module name. Also the failover case |
| 416 | # from the filterspec branch when the (base, filterspec) combo |
| 417 | # doesn't make sense. |
| 418 | suite.addTests(unittest2.defaultTestLoader.loadTestsFromName(base)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 419 | |
| 420 | |
Johnny Chen | cd0279d | 2010-09-20 18:07:50 +0000 | [diff] [blame] | 421 | def lldbLoggings(): |
| 422 | """Check and do lldb loggings if necessary.""" |
| 423 | |
| 424 | # Turn on logging for debugging purposes if ${LLDB_LOG} environment variable is |
| 425 | # defined. Use ${LLDB_LOG} to specify the log file. |
| 426 | ci = lldb.DBG.GetCommandInterpreter() |
| 427 | res = lldb.SBCommandReturnObject() |
| 428 | if ("LLDB_LOG" in os.environ): |
| 429 | if ("LLDB_LOG_OPTION" in os.environ): |
| 430 | lldb_log_option = os.environ["LLDB_LOG_OPTION"] |
| 431 | else: |
| 432 | lldb_log_option = "event process" |
| 433 | ci.HandleCommand( |
| 434 | "log enable -f " + os.environ["LLDB_LOG"] + " lldb " + lldb_log_option, |
| 435 | res) |
| 436 | if not res.Succeeded(): |
| 437 | raise Exception('log enable failed (check LLDB_LOG env variable.') |
| 438 | # Ditto for gdb-remote logging if ${GDB_REMOTE_LOG} environment variable is defined. |
| 439 | # Use ${GDB_REMOTE_LOG} to specify the log file. |
| 440 | if ("GDB_REMOTE_LOG" in os.environ): |
| 441 | if ("GDB_REMOTE_LOG_OPTION" in os.environ): |
| 442 | gdb_remote_log_option = os.environ["GDB_REMOTE_LOG_OPTION"] |
| 443 | else: |
| 444 | gdb_remote_log_option = "packets" |
| 445 | ci.HandleCommand( |
| 446 | "log enable -f " + os.environ["GDB_REMOTE_LOG"] + " process.gdb-remote " |
| 447 | + gdb_remote_log_option, |
| 448 | res) |
| 449 | if not res.Succeeded(): |
| 450 | raise Exception('log enable failed (check GDB_REMOTE_LOG env variable.') |
| 451 | |
| 452 | |
| 453 | ############################################ |
| 454 | # # |
| 455 | # Execution of the test driver starts here # |
| 456 | # # |
| 457 | ############################################ |
| 458 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 459 | # |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 460 | # Start the actions by first parsing the options while setting up the test |
| 461 | # directories, followed by setting up the search paths for lldb utilities; |
| 462 | # then, we walk the directory trees and collect the tests into our test suite. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 463 | # |
Johnny Chen | af149a0 | 2010-09-16 17:11:30 +0000 | [diff] [blame] | 464 | parseOptionsAndInitTestdirs() |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 465 | setupSysPath() |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 466 | |
| 467 | # |
| 468 | # If '-d' is specified, do a delay of 10 seconds for the debugger to attach. |
| 469 | # |
| 470 | if delay: |
Johnny Chen | cd0279d | 2010-09-20 18:07:50 +0000 | [diff] [blame] | 471 | doDelay(10) |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 472 | |
Johnny Chen | 49f2f7a | 2010-09-20 17:25:45 +0000 | [diff] [blame] | 473 | # |
Johnny Chen | 4199819 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 474 | # If '-l' is specified, do not skip the long running tests. |
| 475 | if not skipLongRunningTest: |
| 476 | os.environ["LLDB_SKIP_LONG_RUNNING_TEST"] = "NO" |
| 477 | |
| 478 | # |
Johnny Chen | 7972335 | 2010-10-12 15:53:22 +0000 | [diff] [blame] | 479 | # Walk through the testdirs while collecting tests. |
Johnny Chen | 49f2f7a | 2010-09-20 17:25:45 +0000 | [diff] [blame] | 480 | # |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 481 | for testdir in testdirs: |
| 482 | os.path.walk(testdir, visit, 'Test') |
| 483 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 484 | # |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 485 | # Now that we have loaded all the test cases, run the whole test suite. |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 486 | # |
Johnny Chen | cd0279d | 2010-09-20 18:07:50 +0000 | [diff] [blame] | 487 | |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 488 | # For the time being, let's bracket the test runner within the |
| 489 | # lldb.SBDebugger.Initialize()/Terminate() pair. |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 490 | import lldb, atexit |
Johnny Chen | 6b6f5ba | 2010-10-14 16:36:49 +0000 | [diff] [blame] | 491 | # Update: the act of importing lldb now executes lldb.SBDebugger.Initialize(), |
| 492 | # there's no need to call it a second time. |
| 493 | #lldb.SBDebugger.Initialize() |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 494 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 495 | |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 496 | # Create a singleton SBDebugger in the lldb namespace. |
| 497 | lldb.DBG = lldb.SBDebugger.Create() |
| 498 | |
Johnny Chen | cd0279d | 2010-09-20 18:07:50 +0000 | [diff] [blame] | 499 | # Turn on lldb loggings if necessary. |
| 500 | lldbLoggings() |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 501 | |
Johnny Chen | 7987ac9 | 2010-08-09 20:40:52 +0000 | [diff] [blame] | 502 | # Install the control-c handler. |
| 503 | unittest2.signals.installHandler() |
| 504 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 505 | # |
| 506 | # Invoke the default TextTestRunner to run the test suite, possibly iterating |
| 507 | # over different configurations. |
| 508 | # |
| 509 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 510 | iterArchs = False |
Johnny Chen | f032d90 | 2010-09-21 00:16:09 +0000 | [diff] [blame] | 511 | iterCompilers = False |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 512 | |
| 513 | from types import * |
| 514 | if "archs" in config: |
| 515 | archs = config["archs"] |
| 516 | if type(archs) is ListType and len(archs) >= 1: |
| 517 | iterArchs = True |
| 518 | if "compilers" in config: |
| 519 | compilers = config["compilers"] |
| 520 | if type(compilers) is ListType and len(compilers) >= 1: |
| 521 | iterCompilers = True |
| 522 | |
Johnny Chen | 953864a | 2010-10-12 21:35:54 +0000 | [diff] [blame] | 523 | # Make a shallow copy of sys.path, we need to manipulate the search paths later. |
| 524 | # This is only necessary if we are relocated and with different configurations. |
| 525 | if rdir and (iterArchs or iterCompilers): |
| 526 | old_sys_path = sys.path[:] |
| 527 | old_stderr = sys.stderr |
| 528 | old_stdout = sys.stdout |
| 529 | new_stderr = None |
| 530 | new_stdout = None |
| 531 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 532 | for ia in range(len(archs) if iterArchs else 1): |
| 533 | archConfig = "" |
| 534 | if iterArchs: |
Johnny Chen | 18a921f | 2010-09-30 17:11:58 +0000 | [diff] [blame] | 535 | os.environ["ARCH"] = archs[ia] |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 536 | archConfig = "arch=%s" % archs[ia] |
| 537 | for ic in range(len(compilers) if iterCompilers else 1): |
| 538 | if iterCompilers: |
Johnny Chen | 18a921f | 2010-09-30 17:11:58 +0000 | [diff] [blame] | 539 | os.environ["CC"] = compilers[ic] |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 540 | configString = "%s compiler=%s" % (archConfig, compilers[ic]) |
| 541 | else: |
| 542 | configString = archConfig |
| 543 | |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 544 | if iterArchs or iterCompilers: |
Johnny Chen | 953864a | 2010-10-12 21:35:54 +0000 | [diff] [blame] | 545 | # If we specified a relocated directory to run the test suite, do |
| 546 | # the extra housekeeping to copy the testdirs to a configStringified |
| 547 | # directory and to update sys.path before invoking the test runner. |
| 548 | # The purpose is to separate the configuration-specific directories |
| 549 | # from each other. |
| 550 | if rdir: |
| 551 | from string import maketrans |
| 552 | from shutil import copytree, ignore_patterns |
| 553 | |
| 554 | # Translate ' ' to '-' for dir name. |
| 555 | tbl = maketrans(' ', '-') |
| 556 | configPostfix = configString.translate(tbl) |
| 557 | newrdir = "%s.%s" % (rdir, configPostfix) |
| 558 | |
| 559 | # Copy the tree to a new directory with postfix name configPostfix. |
| 560 | copytree(rdir, newrdir, ignore=ignore_patterns('*.pyc', '*.o', '*.d')) |
| 561 | |
| 562 | # Check whether we need to split stderr/stdout into configuration |
| 563 | # specific files. |
| 564 | if old_stderr.name != '<stderr>' and config.get('split_stderr'): |
| 565 | if new_stderr: |
| 566 | new_stderr.close() |
| 567 | new_stderr = open("%s.%s" % (old_stderr.name, configPostfix), "w") |
| 568 | sys.stderr = new_stderr |
Johnny Chen | 4b6630e | 2010-10-12 21:50:36 +0000 | [diff] [blame] | 569 | if old_stdout.name != '<stdout>' and config.get('split_stdout'): |
Johnny Chen | 953864a | 2010-10-12 21:35:54 +0000 | [diff] [blame] | 570 | if new_stdout: |
| 571 | new_stdout.close() |
| 572 | new_stdout = open("%s.%s" % (old_stdout.name, configPostfix), "w") |
| 573 | sys.stdout = new_stdout |
| 574 | |
| 575 | # Update the LLDB_TEST environment variable to reflect new top |
| 576 | # level test directory. |
| 577 | # |
| 578 | # See also lldbtest.TestBase.setUpClass(cls). |
| 579 | if len(testdirs) == 1 and os.path.basename(testdirs[0]) == 'test': |
| 580 | os.environ["LLDB_TEST"] = os.path.join(newrdir, 'test') |
| 581 | else: |
| 582 | os.environ["LLDB_TEST"] = newrdir |
| 583 | |
| 584 | # And update the Python search paths for modules. |
| 585 | sys.path = [x.replace(rdir, newrdir, 1) for x in old_sys_path] |
| 586 | |
| 587 | # Output the configuration. |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 588 | sys.stderr.write("\nConfiguration: " + configString + "\n") |
Johnny Chen | 953864a | 2010-10-12 21:35:54 +0000 | [diff] [blame] | 589 | |
| 590 | #print "sys.stderr name is", sys.stderr.name |
| 591 | #print "sys.stdout name is", sys.stdout.name |
| 592 | |
| 593 | # First, write out the number of collected test cases. |
| 594 | sys.stderr.write(separator + "\n") |
| 595 | sys.stderr.write("Collected %d test%s\n\n" |
| 596 | % (suite.countTestCases(), |
| 597 | suite.countTestCases() != 1 and "s" or "")) |
| 598 | |
| 599 | # Invoke the test runner. |
Johnny Chen | 84a6d6f | 2010-10-15 01:18:29 +0000 | [diff] [blame^] | 600 | class LLDBTestResult(unittest2.TextTestResult): |
| 601 | """ |
| 602 | Enforce a singleton pattern to allow inspection of test progress. |
| 603 | """ |
| 604 | __singleton__ = None |
| 605 | |
| 606 | def __init__(self, *args): |
| 607 | if LLDBTestResult.__singleton__: |
| 608 | raise "LLDBTestResult instantiated more than once" |
| 609 | super(LLDBTestResult, self).__init__(*args) |
| 610 | LLDBTestResult.__singleton__ = self |
| 611 | # Now put this singleton into the lldb module namespace. |
| 612 | lldb.test_result = self |
| 613 | |
| 614 | def addFailure(self, test, err): |
| 615 | super(LLDBTestResult, self).addFailure(test, err) |
| 616 | method = getattr(test, "markFailure", None) |
| 617 | if method: |
| 618 | method() |
| 619 | setattr(test, "__failed__", True) |
| 620 | |
| 621 | result = unittest2.TextTestRunner(stream=sys.stderr, verbosity=verbose, |
| 622 | resultclass=LLDBTestResult).run(suite) |
Johnny Chen | b40056b | 2010-09-21 00:09:27 +0000 | [diff] [blame] | 623 | |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 624 | |
Johnny Chen | cd0279d | 2010-09-20 18:07:50 +0000 | [diff] [blame] | 625 | # Terminate the test suite if ${LLDB_TESTSUITE_FORCE_FINISH} is defined. |
| 626 | # This should not be necessary now. |
Johnny Chen | 83f6e51 | 2010-08-13 22:58:44 +0000 | [diff] [blame] | 627 | if ("LLDB_TESTSUITE_FORCE_FINISH" in os.environ): |
| 628 | import subprocess |
| 629 | print "Terminating Test suite..." |
| 630 | subprocess.Popen(["/bin/sh", "-c", "kill %s; exit 0" % (os.getpid())]) |
| 631 | |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 632 | # Exiting. |
| 633 | sys.exit(not result.wasSuccessful) |