Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1 | """ |
| 2 | LLDB module which provides the abstract base class of lldb test case. |
| 3 | |
| 4 | The concrete subclass can override lldbtest.TesBase in order to inherit the |
| 5 | common behavior for unitest.TestCase.setUp/tearDown implemented in this file. |
| 6 | |
| 7 | The subclass should override the attribute mydir in order for the python runtime |
| 8 | to locate the individual test cases when running as part of a large test suite |
| 9 | or when running each test case as a separate python invocation. |
| 10 | |
| 11 | ./dotest.py provides a test driver which sets up the environment to run the |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 12 | entire of part of the test suite . Example: |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 13 | |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 14 | # Exercises the test suite in the types directory.... |
| 15 | /Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 types |
Johnny Chen | 57b4738 | 2010-09-02 22:25:47 +0000 | [diff] [blame] | 16 | ... |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 17 | |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 18 | Session logs for test failures/errors/unexpected successes will go into directory '2012-05-16-13_35_42' |
| 19 | Command invoked: python ./dotest.py -A x86_64 types |
| 20 | compilers=['clang'] |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 21 | |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 22 | Configuration: arch=x86_64 compiler=clang |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 23 | ---------------------------------------------------------------------- |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 24 | Collected 72 tests |
| 25 | |
| 26 | ........................................................................ |
| 27 | ---------------------------------------------------------------------- |
| 28 | Ran 72 tests in 135.468s |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 29 | |
| 30 | OK |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 31 | $ |
| 32 | """ |
| 33 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 34 | import abc |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 35 | import glob |
Johnny Chen | 90312a8 | 2010-09-21 22:34:45 +0000 | [diff] [blame] | 36 | import os, sys, traceback |
Enrico Granata | 7e137e3 | 2012-10-24 18:14:21 +0000 | [diff] [blame] | 37 | import os.path |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 38 | import re |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 39 | import signal |
Johnny Chen | 8952a2d | 2010-08-30 21:35:00 +0000 | [diff] [blame] | 40 | from subprocess import * |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 41 | import StringIO |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 42 | import time |
Johnny Chen | a33a93c | 2010-08-30 23:08:52 +0000 | [diff] [blame] | 43 | import types |
Johnny Chen | 7325883 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 44 | import unittest2 |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 45 | import lldb |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 46 | import lldbtest_config |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 47 | import lldbutil |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 48 | from _pyio import __metaclass__ |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 49 | |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 50 | if sys.version_info.major < 3: |
| 51 | import urlparse |
| 52 | else: |
| 53 | import urllib.parse as urlparse |
| 54 | |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 55 | # dosep.py starts lots and lots of dotest instances |
| 56 | # This option helps you find if two (or more) dotest instances are using the same |
| 57 | # directory at the same time |
| 58 | # Enable it to cause test failures and stderr messages if dotest instances try to run in |
| 59 | # the same directory simultaneously |
| 60 | # it is disabled by default because it litters the test directories with ".dirlock" files |
| 61 | debug_confirm_directory_exclusivity = False |
| 62 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 63 | # See also dotest.parseOptionsAndInitTestdirs(), where the environment variables |
Johnny Chen | d2047fa | 2011-01-19 18:18:47 +0000 | [diff] [blame] | 64 | # LLDB_COMMAND_TRACE and LLDB_DO_CLEANUP are set from '-t' and '-r dir' options. |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 65 | |
| 66 | # By default, traceAlways is False. |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 67 | if "LLDB_COMMAND_TRACE" in os.environ and os.environ["LLDB_COMMAND_TRACE"]=="YES": |
| 68 | traceAlways = True |
| 69 | else: |
| 70 | traceAlways = False |
| 71 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 72 | # By default, doCleanup is True. |
| 73 | if "LLDB_DO_CLEANUP" in os.environ and os.environ["LLDB_DO_CLEANUP"]=="NO": |
| 74 | doCleanup = False |
| 75 | else: |
| 76 | doCleanup = True |
| 77 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 78 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 79 | # |
| 80 | # Some commonly used assert messages. |
| 81 | # |
| 82 | |
Johnny Chen | aa90292 | 2010-09-17 22:45:27 +0000 | [diff] [blame] | 83 | COMMAND_FAILED_AS_EXPECTED = "Command has failed as expected" |
| 84 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 85 | CURRENT_EXECUTABLE_SET = "Current executable set successfully" |
| 86 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 87 | PROCESS_IS_VALID = "Process is valid" |
| 88 | |
| 89 | PROCESS_KILLED = "Process is killed successfully" |
| 90 | |
Johnny Chen | d5f66fc | 2010-12-23 01:12:19 +0000 | [diff] [blame] | 91 | PROCESS_EXITED = "Process exited successfully" |
| 92 | |
| 93 | PROCESS_STOPPED = "Process status should be stopped" |
| 94 | |
Sean Callanan | 05834cd | 2015-07-01 23:56:30 +0000 | [diff] [blame] | 95 | RUN_SUCCEEDED = "Process is launched successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 96 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 97 | RUN_COMPLETED = "Process exited successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 98 | |
Johnny Chen | 67af43f | 2010-10-05 19:27:32 +0000 | [diff] [blame] | 99 | BACKTRACE_DISPLAYED_CORRECTLY = "Backtrace displayed correctly" |
| 100 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 101 | BREAKPOINT_CREATED = "Breakpoint created successfully" |
| 102 | |
Johnny Chen | f10af38 | 2010-12-04 00:07:24 +0000 | [diff] [blame] | 103 | BREAKPOINT_STATE_CORRECT = "Breakpoint state is correct" |
| 104 | |
Johnny Chen | e76896c | 2010-08-17 21:33:31 +0000 | [diff] [blame] | 105 | BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" |
| 106 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 107 | BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 108 | |
Johnny Chen | 703dbd0 | 2010-09-30 17:06:24 +0000 | [diff] [blame] | 109 | BREAKPOINT_HIT_TWICE = "Breakpoint resolved with hit cout = 2" |
| 110 | |
Johnny Chen | 164f1e1 | 2010-10-15 18:07:09 +0000 | [diff] [blame] | 111 | BREAKPOINT_HIT_THRICE = "Breakpoint resolved with hit cout = 3" |
| 112 | |
Greg Clayton | 5db6b79 | 2012-10-24 18:24:14 +0000 | [diff] [blame] | 113 | MISSING_EXPECTED_REGISTERS = "At least one expected register is unavailable." |
| 114 | |
Johnny Chen | 89109ed1 | 2011-06-27 20:05:23 +0000 | [diff] [blame] | 115 | OBJECT_PRINTED_CORRECTLY = "Object printed correctly" |
| 116 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 117 | SOURCE_DISPLAYED_CORRECTLY = "Source code displayed correctly" |
| 118 | |
Johnny Chen | c70b02a | 2010-09-22 23:00:20 +0000 | [diff] [blame] | 119 | STEP_OUT_SUCCEEDED = "Thread step-out succeeded" |
| 120 | |
Johnny Chen | 1691a16 | 2011-04-15 16:44:48 +0000 | [diff] [blame] | 121 | STOPPED_DUE_TO_EXC_BAD_ACCESS = "Process should be stopped due to bad access exception" |
| 122 | |
Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 123 | STOPPED_DUE_TO_ASSERT = "Process should be stopped due to an assertion" |
| 124 | |
Johnny Chen | 5d6c464 | 2010-11-10 23:46:38 +0000 | [diff] [blame] | 125 | STOPPED_DUE_TO_BREAKPOINT = "Process should be stopped due to breakpoint" |
Johnny Chen | de0338b | 2010-11-10 20:20:06 +0000 | [diff] [blame] | 126 | |
Johnny Chen | 5d6c464 | 2010-11-10 23:46:38 +0000 | [diff] [blame] | 127 | STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS = "%s, %s" % ( |
| 128 | STOPPED_DUE_TO_BREAKPOINT, "instead, the actual stop reason is: '%s'") |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 129 | |
Johnny Chen | 2e431ce | 2010-10-20 18:38:48 +0000 | [diff] [blame] | 130 | STOPPED_DUE_TO_BREAKPOINT_CONDITION = "Stopped due to breakpoint condition" |
| 131 | |
Johnny Chen | 0a3d1ca | 2010-12-13 21:49:58 +0000 | [diff] [blame] | 132 | STOPPED_DUE_TO_BREAKPOINT_IGNORE_COUNT = "Stopped due to breakpoint and ignore count" |
| 133 | |
Johnny Chen | c066ab4 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 134 | STOPPED_DUE_TO_SIGNAL = "Process state is stopped due to signal" |
| 135 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 136 | STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" |
| 137 | |
Johnny Chen | f68cc12 | 2011-09-15 21:09:59 +0000 | [diff] [blame] | 138 | STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint" |
| 139 | |
Johnny Chen | 3c884a0 | 2010-08-24 22:07:56 +0000 | [diff] [blame] | 140 | DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly" |
| 141 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 142 | VALID_BREAKPOINT = "Got a valid breakpoint" |
| 143 | |
Johnny Chen | 5bfb8ee | 2010-10-22 18:10:25 +0000 | [diff] [blame] | 144 | VALID_BREAKPOINT_LOCATION = "Got a valid breakpoint location" |
| 145 | |
Johnny Chen | 7209d84f | 2011-05-06 23:26:12 +0000 | [diff] [blame] | 146 | VALID_COMMAND_INTERPRETER = "Got a valid command interpreter" |
| 147 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 148 | VALID_FILESPEC = "Got a valid filespec" |
| 149 | |
Johnny Chen | 025d1b8 | 2010-12-08 01:25:21 +0000 | [diff] [blame] | 150 | VALID_MODULE = "Got a valid module" |
| 151 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 152 | VALID_PROCESS = "Got a valid process" |
| 153 | |
Johnny Chen | 025d1b8 | 2010-12-08 01:25:21 +0000 | [diff] [blame] | 154 | VALID_SYMBOL = "Got a valid symbol" |
| 155 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 156 | VALID_TARGET = "Got a valid target" |
| 157 | |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 158 | VALID_PLATFORM = "Got a valid platform" |
| 159 | |
Johnny Chen | 15f247a | 2012-02-03 20:43:00 +0000 | [diff] [blame] | 160 | VALID_TYPE = "Got a valid type" |
| 161 | |
Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 162 | VALID_VARIABLE = "Got a valid variable" |
| 163 | |
Johnny Chen | 981463d | 2010-08-25 19:00:04 +0000 | [diff] [blame] | 164 | VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 165 | |
Johnny Chen | f68cc12 | 2011-09-15 21:09:59 +0000 | [diff] [blame] | 166 | WATCHPOINT_CREATED = "Watchpoint created successfully" |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 167 | |
Sean Callanan | 05834cd | 2015-07-01 23:56:30 +0000 | [diff] [blame] | 168 | def CMD_MSG(str): |
| 169 | '''A generic "Command '%s' returns successfully" message generator.''' |
| 170 | return "Command '%s' returns successfully" % str |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 171 | |
Johnny Chen | 3bc8ae4 | 2012-03-15 19:10:00 +0000 | [diff] [blame] | 172 | def COMPLETION_MSG(str_before, str_after): |
Johnny Chen | 98aceb0 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 173 | '''A generic message generator for the completion mechanism.''' |
| 174 | return "'%s' successfully completes to '%s'" % (str_before, str_after) |
| 175 | |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 176 | def EXP_MSG(str, exe): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 177 | '''A generic "'%s' returns expected result" message generator if exe. |
| 178 | Otherwise, it generates "'%s' matches expected result" message.''' |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 179 | return "'%s' %s expected result" % (str, 'returns' if exe else 'matches') |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 180 | |
Johnny Chen | 3343f04 | 2010-10-19 19:11:38 +0000 | [diff] [blame] | 181 | def SETTING_MSG(setting): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 182 | '''A generic "Value of setting '%s' is correct" message generator.''' |
Johnny Chen | 3343f04 | 2010-10-19 19:11:38 +0000 | [diff] [blame] | 183 | return "Value of setting '%s' is correct" % setting |
| 184 | |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 185 | def EnvArray(): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 186 | """Returns an env variable array from the os.environ map object.""" |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 187 | return map(lambda k,v: k+"="+v, os.environ.keys(), os.environ.values()) |
| 188 | |
Johnny Chen | 47ceb03 | 2010-10-11 23:52:19 +0000 | [diff] [blame] | 189 | def line_number(filename, string_to_match): |
| 190 | """Helper function to return the line number of the first matched string.""" |
| 191 | with open(filename, 'r') as f: |
| 192 | for i, line in enumerate(f): |
| 193 | if line.find(string_to_match) != -1: |
| 194 | # Found our match. |
Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 195 | return i+1 |
Johnny Chen | 1691a16 | 2011-04-15 16:44:48 +0000 | [diff] [blame] | 196 | raise Exception("Unable to find '%s' within file %s" % (string_to_match, filename)) |
Johnny Chen | 47ceb03 | 2010-10-11 23:52:19 +0000 | [diff] [blame] | 197 | |
Johnny Chen | 67af43f | 2010-10-05 19:27:32 +0000 | [diff] [blame] | 198 | def pointer_size(): |
| 199 | """Return the pointer size of the host system.""" |
| 200 | import ctypes |
| 201 | a_pointer = ctypes.c_void_p(0xffff) |
| 202 | return 8 * ctypes.sizeof(a_pointer) |
| 203 | |
Johnny Chen | 5781673 | 2012-02-09 02:01:59 +0000 | [diff] [blame] | 204 | def is_exe(fpath): |
| 205 | """Returns true if fpath is an executable.""" |
| 206 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 207 | |
| 208 | def which(program): |
| 209 | """Returns the full path to a program; None otherwise.""" |
| 210 | fpath, fname = os.path.split(program) |
| 211 | if fpath: |
| 212 | if is_exe(program): |
| 213 | return program |
| 214 | else: |
| 215 | for path in os.environ["PATH"].split(os.pathsep): |
| 216 | exe_file = os.path.join(path, program) |
| 217 | if is_exe(exe_file): |
| 218 | return exe_file |
| 219 | return None |
| 220 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 221 | class recording(StringIO.StringIO): |
| 222 | """ |
| 223 | A nice little context manager for recording the debugger interactions into |
| 224 | our session object. If trace flag is ON, it also emits the interactions |
| 225 | into the stderr. |
| 226 | """ |
| 227 | def __init__(self, test, trace): |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 228 | """Create a StringIO instance; record the session obj and trace flag.""" |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 229 | StringIO.StringIO.__init__(self) |
Johnny Chen | 0241f14 | 2011-08-16 22:06:17 +0000 | [diff] [blame] | 230 | # The test might not have undergone the 'setUp(self)' phase yet, so that |
| 231 | # the attribute 'session' might not even exist yet. |
Johnny Chen | bfcf37f | 2011-08-16 17:06:45 +0000 | [diff] [blame] | 232 | self.session = getattr(test, "session", None) if test else None |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 233 | self.trace = trace |
| 234 | |
| 235 | def __enter__(self): |
| 236 | """ |
| 237 | Context management protocol on entry to the body of the with statement. |
| 238 | Just return the StringIO object. |
| 239 | """ |
| 240 | return self |
| 241 | |
| 242 | def __exit__(self, type, value, tb): |
| 243 | """ |
| 244 | Context management protocol on exit from the body of the with statement. |
| 245 | If trace is ON, it emits the recordings into stderr. Always add the |
| 246 | recordings to our session object. And close the StringIO object, too. |
| 247 | """ |
| 248 | if self.trace: |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 249 | print >> sys.stderr, self.getvalue() |
| 250 | if self.session: |
| 251 | print >> self.session, self.getvalue() |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 252 | self.close() |
| 253 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 254 | class _BaseProcess(object): |
| 255 | __metaclass__ = abc.ABCMeta |
| 256 | |
| 257 | @abc.abstractproperty |
| 258 | def pid(self): |
| 259 | """Returns process PID if has been launched already.""" |
| 260 | |
| 261 | @abc.abstractmethod |
| 262 | def launch(self, executable, args): |
| 263 | """Launches new process with given executable and args.""" |
| 264 | |
| 265 | @abc.abstractmethod |
| 266 | def terminate(self): |
| 267 | """Terminates previously launched process..""" |
| 268 | |
| 269 | class _LocalProcess(_BaseProcess): |
| 270 | |
| 271 | def __init__(self, trace_on): |
| 272 | self._proc = None |
| 273 | self._trace_on = trace_on |
Ilia K | 725abcb | 2015-04-15 13:35:49 +0000 | [diff] [blame] | 274 | self._delayafterterminate = 0.1 |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 275 | |
| 276 | @property |
| 277 | def pid(self): |
| 278 | return self._proc.pid |
| 279 | |
| 280 | def launch(self, executable, args): |
| 281 | self._proc = Popen([executable] + args, |
| 282 | stdout = open(os.devnull) if not self._trace_on else None, |
| 283 | stdin = PIPE) |
| 284 | |
| 285 | def terminate(self): |
| 286 | if self._proc.poll() == None: |
Ilia K | 725abcb | 2015-04-15 13:35:49 +0000 | [diff] [blame] | 287 | # Terminate _proc like it does the pexpect |
Adrian McCarthy | 137d7ba | 2015-07-07 14:47:34 +0000 | [diff] [blame] | 288 | signals_to_try = [sig for sig in ['SIGHUP', 'SIGCONT', 'SIGINT'] if sig in dir(signal)] |
| 289 | for sig in signals_to_try: |
| 290 | try: |
| 291 | self._proc.send_signal(getattr(signal, sig)) |
| 292 | time.sleep(self._delayafterterminate) |
| 293 | if self._proc.poll() != None: |
| 294 | return |
| 295 | except ValueError: |
| 296 | pass # Windows says SIGINT is not a valid signal to send |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 297 | self._proc.terminate() |
Ilia K | 725abcb | 2015-04-15 13:35:49 +0000 | [diff] [blame] | 298 | time.sleep(self._delayafterterminate) |
| 299 | if self._proc.poll() != None: |
| 300 | return |
| 301 | self._proc.kill() |
| 302 | time.sleep(self._delayafterterminate) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 303 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 304 | def poll(self): |
| 305 | return self._proc.poll() |
| 306 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 307 | class _RemoteProcess(_BaseProcess): |
| 308 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 309 | def __init__(self, install_remote): |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 310 | self._pid = None |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 311 | self._install_remote = install_remote |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 312 | |
| 313 | @property |
| 314 | def pid(self): |
| 315 | return self._pid |
| 316 | |
| 317 | def launch(self, executable, args): |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 318 | if self._install_remote: |
| 319 | src_path = executable |
Chaoren Lin | 5d76b1b | 2015-06-06 00:25:50 +0000 | [diff] [blame] | 320 | dst_path = lldbutil.append_to_process_working_directory(os.path.basename(executable)) |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 321 | |
| 322 | dst_file_spec = lldb.SBFileSpec(dst_path, False) |
| 323 | err = lldb.remote_platform.Install(lldb.SBFileSpec(src_path, True), dst_file_spec) |
| 324 | if err.Fail(): |
| 325 | raise Exception("remote_platform.Install('%s', '%s') failed: %s" % (src_path, dst_path, err)) |
| 326 | else: |
| 327 | dst_path = executable |
| 328 | dst_file_spec = lldb.SBFileSpec(executable, False) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 329 | |
| 330 | launch_info = lldb.SBLaunchInfo(args) |
| 331 | launch_info.SetExecutableFile(dst_file_spec, True) |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 332 | launch_info.SetWorkingDirectory(lldb.remote_platform.GetWorkingDirectory()) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 333 | |
| 334 | # Redirect stdout and stderr to /dev/null |
| 335 | launch_info.AddSuppressFileAction(1, False, True) |
| 336 | launch_info.AddSuppressFileAction(2, False, True) |
| 337 | |
| 338 | err = lldb.remote_platform.Launch(launch_info) |
| 339 | if err.Fail(): |
| 340 | raise Exception("remote_platform.Launch('%s', '%s') failed: %s" % (dst_path, args, err)) |
| 341 | self._pid = launch_info.GetProcessID() |
| 342 | |
| 343 | def terminate(self): |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 344 | lldb.remote_platform.Kill(self._pid) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 345 | |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 346 | # From 2.7's subprocess.check_output() convenience function. |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 347 | # Return a tuple (stdoutdata, stderrdata). |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 348 | def system(commands, **kwargs): |
Johnny Chen | 8eb14a9 | 2011-11-16 22:44:28 +0000 | [diff] [blame] | 349 | r"""Run an os command with arguments and return its output as a byte string. |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 350 | |
| 351 | If the exit code was non-zero it raises a CalledProcessError. The |
| 352 | CalledProcessError object will have the return code in the returncode |
| 353 | attribute and output in the output attribute. |
| 354 | |
| 355 | The arguments are the same as for the Popen constructor. Example: |
| 356 | |
| 357 | >>> check_output(["ls", "-l", "/dev/null"]) |
| 358 | 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 359 | |
| 360 | The stdout argument is not allowed as it is used internally. |
| 361 | To capture standard error in the result, use stderr=STDOUT. |
| 362 | |
| 363 | >>> check_output(["/bin/sh", "-c", |
| 364 | ... "ls -l non_existent_file ; exit 0"], |
| 365 | ... stderr=STDOUT) |
| 366 | 'ls: non_existent_file: No such file or directory\n' |
| 367 | """ |
| 368 | |
| 369 | # Assign the sender object to variable 'test' and remove it from kwargs. |
| 370 | test = kwargs.pop('sender', None) |
| 371 | |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 372 | # [['make', 'clean', 'foo'], ['make', 'foo']] -> ['make clean foo', 'make foo'] |
| 373 | commandList = [' '.join(x) for x in commands] |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 374 | output = "" |
| 375 | error = "" |
| 376 | for shellCommand in commandList: |
| 377 | if 'stdout' in kwargs: |
| 378 | raise ValueError('stdout argument not allowed, it will be overridden.') |
| 379 | if 'shell' in kwargs and kwargs['shell']==False: |
| 380 | raise ValueError('shell=False not allowed') |
| 381 | process = Popen(shellCommand, stdout=PIPE, stderr=PIPE, shell=True, **kwargs) |
| 382 | pid = process.pid |
| 383 | this_output, this_error = process.communicate() |
| 384 | retcode = process.poll() |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 385 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 386 | # Enable trace on failure return while tracking down FreeBSD buildbot issues |
| 387 | trace = traceAlways |
| 388 | if not trace and retcode and sys.platform.startswith("freebsd"): |
| 389 | trace = True |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 390 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 391 | with recording(test, trace) as sbuf: |
| 392 | print >> sbuf |
| 393 | print >> sbuf, "os command:", shellCommand |
| 394 | print >> sbuf, "with pid:", pid |
Adrian McCarthy | 1d57433 | 2015-04-03 17:10:30 +0000 | [diff] [blame] | 395 | print >> sbuf, "stdout:", this_output |
| 396 | print >> sbuf, "stderr:", this_error |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 397 | print >> sbuf, "retcode:", retcode |
| 398 | print >> sbuf |
Ed Maste | 6e49633 | 2014-08-05 20:33:17 +0000 | [diff] [blame] | 399 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 400 | if retcode: |
| 401 | cmd = kwargs.get("args") |
| 402 | if cmd is None: |
| 403 | cmd = shellCommand |
| 404 | raise CalledProcessError(retcode, cmd) |
| 405 | output = output + this_output |
| 406 | error = error + this_error |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 407 | return (output, error) |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 408 | |
Johnny Chen | ab9c1dd | 2010-11-01 20:35:01 +0000 | [diff] [blame] | 409 | def getsource_if_available(obj): |
| 410 | """ |
| 411 | Return the text of the source code for an object if available. Otherwise, |
| 412 | a print representation is returned. |
| 413 | """ |
| 414 | import inspect |
| 415 | try: |
| 416 | return inspect.getsource(obj) |
| 417 | except: |
| 418 | return repr(obj) |
| 419 | |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 420 | def builder_module(): |
Ed Maste | 4d90f0f | 2013-07-25 13:24:34 +0000 | [diff] [blame] | 421 | if sys.platform.startswith("freebsd"): |
| 422 | return __import__("builder_freebsd") |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 423 | return __import__("builder_" + sys.platform) |
| 424 | |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 425 | def run_adb_command(cmd, device_id): |
| 426 | device_id_args = [] |
| 427 | if device_id: |
| 428 | device_id_args = ["-s", device_id] |
| 429 | full_cmd = ["adb"] + device_id_args + cmd |
| 430 | p = Popen(full_cmd, stdout=PIPE, stderr=PIPE) |
| 431 | stdout, stderr = p.communicate() |
| 432 | return p.returncode, stdout, stderr |
| 433 | |
Chaoren Lin | e9bbabc | 2015-07-18 00:37:55 +0000 | [diff] [blame] | 434 | def append_android_envs(dictionary): |
| 435 | if dictionary is None: |
| 436 | dictionary = {} |
| 437 | dictionary["OS"] = "Android" |
| 438 | if android_device_api() >= 16: |
| 439 | dictionary["PIE"] = 1 |
| 440 | return dictionary |
| 441 | |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 442 | def target_is_android(): |
| 443 | if not hasattr(target_is_android, 'result'): |
| 444 | triple = lldb.DBG.GetSelectedPlatform().GetTriple() |
| 445 | match = re.match(".*-.*-.*-android", triple) |
| 446 | target_is_android.result = match is not None |
| 447 | return target_is_android.result |
| 448 | |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 449 | def android_device_api(): |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 450 | if not hasattr(android_device_api, 'result'): |
| 451 | assert lldb.platform_url is not None |
| 452 | device_id = None |
| 453 | parsed_url = urlparse.urlparse(lldb.platform_url) |
| 454 | if parsed_url.scheme == "adb": |
| 455 | device_id = parsed_url.netloc.split(":")[0] |
| 456 | retcode, stdout, stderr = run_adb_command( |
| 457 | ["shell", "getprop", "ro.build.version.sdk"], device_id) |
| 458 | if retcode == 0: |
| 459 | android_device_api.result = int(stdout) |
| 460 | else: |
| 461 | raise LookupError( |
| 462 | ">>> Unable to determine the API level of the Android device.\n" |
| 463 | ">>> stdout:\n%s\n" |
| 464 | ">>> stderr:\n%s\n" % (stdout, stderr)) |
| 465 | return android_device_api.result |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 466 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 467 | # |
| 468 | # Decorators for categorizing test cases. |
| 469 | # |
| 470 | |
| 471 | from functools import wraps |
| 472 | def python_api_test(func): |
| 473 | """Decorate the item as a Python API only test.""" |
| 474 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 475 | raise Exception("@python_api_test can only be used to decorate a test method") |
| 476 | @wraps(func) |
| 477 | def wrapper(self, *args, **kwargs): |
| 478 | try: |
| 479 | if lldb.dont_do_python_api_test: |
| 480 | self.skipTest("python api tests") |
| 481 | except AttributeError: |
| 482 | pass |
| 483 | return func(self, *args, **kwargs) |
| 484 | |
| 485 | # Mark this function as such to separate them from lldb command line tests. |
| 486 | wrapper.__python_api_test__ = True |
| 487 | return wrapper |
| 488 | |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 489 | def lldbmi_test(func): |
| 490 | """Decorate the item as a lldb-mi only test.""" |
| 491 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 492 | raise Exception("@lldbmi_test can only be used to decorate a test method") |
| 493 | @wraps(func) |
| 494 | def wrapper(self, *args, **kwargs): |
| 495 | try: |
| 496 | if lldb.dont_do_lldbmi_test: |
| 497 | self.skipTest("lldb-mi tests") |
| 498 | except AttributeError: |
| 499 | pass |
| 500 | return func(self, *args, **kwargs) |
| 501 | |
| 502 | # Mark this function as such to separate them from lldb command line tests. |
| 503 | wrapper.__lldbmi_test__ = True |
| 504 | return wrapper |
| 505 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 506 | def benchmarks_test(func): |
| 507 | """Decorate the item as a benchmarks test.""" |
| 508 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 509 | raise Exception("@benchmarks_test can only be used to decorate a test method") |
| 510 | @wraps(func) |
| 511 | def wrapper(self, *args, **kwargs): |
| 512 | try: |
| 513 | if not lldb.just_do_benchmarks_test: |
| 514 | self.skipTest("benchmarks tests") |
| 515 | except AttributeError: |
| 516 | pass |
| 517 | return func(self, *args, **kwargs) |
| 518 | |
| 519 | # Mark this function as such to separate them from the regular tests. |
| 520 | wrapper.__benchmarks_test__ = True |
| 521 | return wrapper |
| 522 | |
Johnny Chen | f1548d4 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 523 | def dsym_test(func): |
| 524 | """Decorate the item as a dsym test.""" |
| 525 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 526 | raise Exception("@dsym_test can only be used to decorate a test method") |
| 527 | @wraps(func) |
| 528 | def wrapper(self, *args, **kwargs): |
| 529 | try: |
| 530 | if lldb.dont_do_dsym_test: |
| 531 | self.skipTest("dsym tests") |
| 532 | except AttributeError: |
| 533 | pass |
| 534 | return func(self, *args, **kwargs) |
| 535 | |
| 536 | # Mark this function as such to separate them from the regular tests. |
| 537 | wrapper.__dsym_test__ = True |
| 538 | return wrapper |
| 539 | |
| 540 | def dwarf_test(func): |
| 541 | """Decorate the item as a dwarf test.""" |
| 542 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 543 | raise Exception("@dwarf_test can only be used to decorate a test method") |
| 544 | @wraps(func) |
| 545 | def wrapper(self, *args, **kwargs): |
| 546 | try: |
| 547 | if lldb.dont_do_dwarf_test: |
| 548 | self.skipTest("dwarf tests") |
| 549 | except AttributeError: |
| 550 | pass |
| 551 | return func(self, *args, **kwargs) |
| 552 | |
| 553 | # Mark this function as such to separate them from the regular tests. |
| 554 | wrapper.__dwarf_test__ = True |
| 555 | return wrapper |
| 556 | |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 557 | def debugserver_test(func): |
| 558 | """Decorate the item as a debugserver test.""" |
| 559 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 560 | raise Exception("@debugserver_test can only be used to decorate a test method") |
| 561 | @wraps(func) |
| 562 | def wrapper(self, *args, **kwargs): |
| 563 | try: |
| 564 | if lldb.dont_do_debugserver_test: |
| 565 | self.skipTest("debugserver tests") |
| 566 | except AttributeError: |
| 567 | pass |
| 568 | return func(self, *args, **kwargs) |
| 569 | |
| 570 | # Mark this function as such to separate them from the regular tests. |
| 571 | wrapper.__debugserver_test__ = True |
| 572 | return wrapper |
| 573 | |
| 574 | def llgs_test(func): |
Robert Flack | 8cc4cf1 | 2015-03-06 14:36:33 +0000 | [diff] [blame] | 575 | """Decorate the item as a lldb-server test.""" |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 576 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 577 | raise Exception("@llgs_test can only be used to decorate a test method") |
| 578 | @wraps(func) |
| 579 | def wrapper(self, *args, **kwargs): |
| 580 | try: |
| 581 | if lldb.dont_do_llgs_test: |
| 582 | self.skipTest("llgs tests") |
| 583 | except AttributeError: |
| 584 | pass |
| 585 | return func(self, *args, **kwargs) |
| 586 | |
| 587 | # Mark this function as such to separate them from the regular tests. |
| 588 | wrapper.__llgs_test__ = True |
| 589 | return wrapper |
| 590 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 591 | def not_remote_testsuite_ready(func): |
| 592 | """Decorate the item as a test which is not ready yet for remote testsuite.""" |
| 593 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 594 | raise Exception("@not_remote_testsuite_ready can only be used to decorate a test method") |
| 595 | @wraps(func) |
| 596 | def wrapper(self, *args, **kwargs): |
| 597 | try: |
Tamas Berghammer | 3e0ecb2 | 2015-03-25 15:13:28 +0000 | [diff] [blame] | 598 | if lldb.lldbtest_remote_sandbox or lldb.remote_platform: |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 599 | self.skipTest("not ready for remote testsuite") |
| 600 | except AttributeError: |
| 601 | pass |
| 602 | return func(self, *args, **kwargs) |
| 603 | |
| 604 | # Mark this function as such to separate them from the regular tests. |
| 605 | wrapper.__not_ready_for_remote_testsuite_test__ = True |
| 606 | return wrapper |
| 607 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 608 | def expectedFailure(expected_fn, bugnumber=None): |
| 609 | def expectedFailure_impl(func): |
| 610 | @wraps(func) |
| 611 | def wrapper(*args, **kwargs): |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 612 | from unittest2 import case |
| 613 | self = args[0] |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 614 | try: |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 615 | func(*args, **kwargs) |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 616 | except Exception: |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 617 | if expected_fn(self): |
| 618 | raise case._ExpectedFailure(sys.exc_info(), bugnumber) |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 619 | else: |
| 620 | raise |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 621 | if expected_fn(self): |
| 622 | raise case._UnexpectedSuccess(sys.exc_info(), bugnumber) |
| 623 | return wrapper |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 624 | # if bugnumber is not-callable(incluing None), that means decorator function is called with optional arguments |
| 625 | # return decorator in this case, so it will be used to decorating original method |
| 626 | if callable(bugnumber): |
| 627 | return expectedFailure_impl(bugnumber) |
| 628 | else: |
| 629 | return expectedFailure_impl |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 630 | |
| 631 | def expectedFailureCompiler(compiler, compiler_version=None, bugnumber=None): |
| 632 | if compiler_version is None: |
| 633 | compiler_version=['=', None] |
| 634 | def fn(self): |
| 635 | return compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version) |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 636 | return expectedFailure(fn, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 637 | |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 638 | # provide a function to xfail on defined oslist, compiler version, and archs |
| 639 | # if none is specified for any argument, that argument won't be checked and thus means for all |
| 640 | # for example, |
| 641 | # @expectedFailureAll, xfail for all platform/compiler/arch, |
| 642 | # @expectedFailureAll(compiler='gcc'), xfail for gcc on all platform/architecture |
| 643 | # @expectedFailureAll(bugnumber, ["linux"], "gcc", ['>=', '4.9'], ['i386']), xfail for gcc>=4.9 on linux with i386 |
| 644 | def expectedFailureAll(bugnumber=None, oslist=None, compiler=None, compiler_version=None, archs=None): |
| 645 | def fn(self): |
| 646 | return ((oslist is None or self.getPlatform() in oslist) and |
| 647 | (compiler is None or (compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version))) and |
| 648 | self.expectedArch(archs)) |
| 649 | return expectedFailure(fn, bugnumber) |
| 650 | |
Vince Harron | 8974ce2 | 2015-03-13 19:54:54 +0000 | [diff] [blame] | 651 | # to XFAIL a specific clang versions, try this |
| 652 | # @expectedFailureClang('bugnumber', ['<=', '3.4']) |
| 653 | def expectedFailureClang(bugnumber=None, compiler_version=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 654 | return expectedFailureCompiler('clang', compiler_version, bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 655 | |
| 656 | def expectedFailureGcc(bugnumber=None, compiler_version=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 657 | return expectedFailureCompiler('gcc', compiler_version, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 658 | |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 659 | def expectedFailureIcc(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 660 | return expectedFailureCompiler('icc', None, bugnumber) |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 661 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 662 | def expectedFailureArch(arch, bugnumber=None): |
| 663 | def fn(self): |
| 664 | return arch in self.getArchitecture() |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 665 | return expectedFailure(fn, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 666 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 667 | def expectedFailurei386(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 668 | return expectedFailureArch('i386', bugnumber) |
Johnny Chen | a33843f | 2011-12-22 21:14:31 +0000 | [diff] [blame] | 669 | |
Matt Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 670 | def expectedFailurex86_64(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 671 | return expectedFailureArch('x86_64', bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 672 | |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 673 | def expectedFailureOS(oslist, bugnumber=None, compilers=None): |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 674 | def fn(self): |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 675 | return (self.getPlatform() in oslist and |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 676 | self.expectedCompiler(compilers)) |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 677 | return expectedFailure(fn, bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 678 | |
Chaoren Lin | f7160f3 | 2015-06-09 17:39:27 +0000 | [diff] [blame] | 679 | def expectedFailureHostOS(oslist, bugnumber=None, compilers=None): |
| 680 | def fn(self): |
| 681 | return (getHostPlatform() in oslist and |
| 682 | self.expectedCompiler(compilers)) |
| 683 | return expectedFailure(fn, bugnumber) |
| 684 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 685 | def expectedFailureDarwin(bugnumber=None, compilers=None): |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 686 | # For legacy reasons, we support both "darwin" and "macosx" as OS X triples. |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 687 | return expectedFailureOS(getDarwinOSTriples(), bugnumber, compilers) |
Matt Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 688 | |
Ed Maste | 24a7f7d | 2013-07-24 19:47:08 +0000 | [diff] [blame] | 689 | def expectedFailureFreeBSD(bugnumber=None, compilers=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 690 | return expectedFailureOS(['freebsd'], bugnumber, compilers) |
Ed Maste | 24a7f7d | 2013-07-24 19:47:08 +0000 | [diff] [blame] | 691 | |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 692 | def expectedFailureLinux(bugnumber=None, compilers=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 693 | return expectedFailureOS(['linux'], bugnumber, compilers) |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 694 | |
Zachary Turner | 80c2c60 | 2014-12-09 19:28:00 +0000 | [diff] [blame] | 695 | def expectedFailureWindows(bugnumber=None, compilers=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 696 | return expectedFailureOS(['windows'], bugnumber, compilers) |
Zachary Turner | 80c2c60 | 2014-12-09 19:28:00 +0000 | [diff] [blame] | 697 | |
Chaoren Lin | f7160f3 | 2015-06-09 17:39:27 +0000 | [diff] [blame] | 698 | def expectedFailureHostWindows(bugnumber=None, compilers=None): |
| 699 | return expectedFailureHostOS(['windows'], bugnumber, compilers) |
| 700 | |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 701 | def matchAndroid(api_levels=None, archs=None): |
| 702 | def match(self): |
| 703 | if not target_is_android(): |
| 704 | return False |
| 705 | if archs is not None and self.getArchitecture() not in archs: |
| 706 | return False |
| 707 | if api_levels is not None and android_device_api() not in api_levels: |
| 708 | return False |
| 709 | return True |
| 710 | return match |
| 711 | |
| 712 | |
Tamas Berghammer | 050d1e8 | 2015-07-22 11:00:06 +0000 | [diff] [blame] | 713 | def expectedFailureAndroid(bugnumber=None, api_levels=None, archs=None): |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 714 | """ Mark a test as xfail for Android. |
| 715 | |
| 716 | Arguments: |
| 717 | bugnumber - The LLVM pr associated with the problem. |
| 718 | api_levels - A sequence of numbers specifying the Android API levels |
Tamas Berghammer | 050d1e8 | 2015-07-22 11:00:06 +0000 | [diff] [blame] | 719 | for which a test is expected to fail. None means all API level. |
| 720 | arch - A sequence of architecture names specifying the architectures |
| 721 | for which a test is expected to fail. None means all architectures. |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 722 | """ |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 723 | return expectedFailure(matchAndroid(api_levels, archs), bugnumber) |
Pavel Labath | 674bc7b | 2015-05-29 14:54:46 +0000 | [diff] [blame] | 724 | |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 725 | # if the test passes on the first try, we're done (success) |
| 726 | # if the test fails once, then passes on the second try, raise an ExpectedFailure |
| 727 | # if the test fails twice in a row, re-throw the exception from the second test run |
| 728 | def expectedFlakey(expected_fn, bugnumber=None): |
| 729 | def expectedFailure_impl(func): |
| 730 | @wraps(func) |
| 731 | def wrapper(*args, **kwargs): |
| 732 | from unittest2 import case |
| 733 | self = args[0] |
| 734 | try: |
| 735 | func(*args, **kwargs) |
Ying Chen | 0a7202b | 2015-07-01 22:44:27 +0000 | [diff] [blame] | 736 | # don't retry if the test case is already decorated with xfail or skip |
| 737 | except (case._ExpectedFailure, case.SkipTest, case._UnexpectedSuccess): |
| 738 | raise |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 739 | except Exception: |
| 740 | if expected_fn(self): |
Ying Chen | 0a7202b | 2015-07-01 22:44:27 +0000 | [diff] [blame] | 741 | # before retry, run tearDown for previous run and setup for next |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 742 | try: |
Ying Chen | 0a7202b | 2015-07-01 22:44:27 +0000 | [diff] [blame] | 743 | self.tearDown() |
| 744 | self.setUp() |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 745 | func(*args, **kwargs) |
| 746 | except Exception: |
| 747 | # oh snap! two failures in a row, record a failure/error |
| 748 | raise |
| 749 | # record the expected failure |
| 750 | raise case._ExpectedFailure(sys.exc_info(), bugnumber) |
| 751 | else: |
| 752 | raise |
| 753 | return wrapper |
| 754 | # if bugnumber is not-callable(incluing None), that means decorator function is called with optional arguments |
| 755 | # return decorator in this case, so it will be used to decorating original method |
| 756 | if callable(bugnumber): |
| 757 | return expectedFailure_impl(bugnumber) |
| 758 | else: |
| 759 | return expectedFailure_impl |
| 760 | |
| 761 | def expectedFlakeyOS(oslist, bugnumber=None, compilers=None): |
| 762 | def fn(self): |
| 763 | return (self.getPlatform() in oslist and |
| 764 | self.expectedCompiler(compilers)) |
| 765 | return expectedFlakey(fn, bugnumber) |
| 766 | |
| 767 | def expectedFlakeyDarwin(bugnumber=None, compilers=None): |
| 768 | # For legacy reasons, we support both "darwin" and "macosx" as OS X triples. |
| 769 | return expectedFlakeyOS(getDarwinOSTriples(), bugnumber, compilers) |
| 770 | |
| 771 | def expectedFlakeyLinux(bugnumber=None, compilers=None): |
| 772 | return expectedFlakeyOS(['linux'], bugnumber, compilers) |
| 773 | |
| 774 | def expectedFlakeyFreeBSD(bugnumber=None, compilers=None): |
| 775 | return expectedFlakeyOS(['freebsd'], bugnumber, compilers) |
| 776 | |
| 777 | def expectedFlakeyCompiler(compiler, compiler_version=None, bugnumber=None): |
| 778 | if compiler_version is None: |
| 779 | compiler_version=['=', None] |
| 780 | def fn(self): |
| 781 | return compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version) |
| 782 | return expectedFlakey(fn, bugnumber) |
| 783 | |
| 784 | # @expectedFlakeyClang('bugnumber', ['<=', '3.4']) |
| 785 | def expectedFlakeyClang(bugnumber=None, compiler_version=None): |
| 786 | return expectedFlakeyCompiler('clang', compiler_version, bugnumber) |
| 787 | |
| 788 | # @expectedFlakeyGcc('bugnumber', ['<=', '3.4']) |
| 789 | def expectedFlakeyGcc(bugnumber=None, compiler_version=None): |
| 790 | return expectedFlakeyCompiler('gcc', compiler_version, bugnumber) |
| 791 | |
Pavel Labath | 63a579c | 2015-09-07 12:15:27 +0000 | [diff] [blame] | 792 | def expectedFlakeyAndroid(bugnumber=None, api_levels=None, archs=None): |
| 793 | return expectedFlakey(matchAndroid(api_levels, archs), bugnumber) |
| 794 | |
Greg Clayton | 1251456 | 2013-12-05 22:22:32 +0000 | [diff] [blame] | 795 | def skipIfRemote(func): |
| 796 | """Decorate the item to skip tests if testing remotely.""" |
| 797 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 798 | raise Exception("@skipIfRemote can only be used to decorate a test method") |
| 799 | @wraps(func) |
| 800 | def wrapper(*args, **kwargs): |
| 801 | from unittest2 import case |
| 802 | if lldb.remote_platform: |
| 803 | self = args[0] |
| 804 | self.skipTest("skip on remote platform") |
| 805 | else: |
| 806 | func(*args, **kwargs) |
| 807 | return wrapper |
| 808 | |
Siva Chandra | 4470f38 | 2015-06-17 22:32:27 +0000 | [diff] [blame] | 809 | def skipUnlessListedRemote(remote_list=None): |
| 810 | def myImpl(func): |
| 811 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 812 | raise Exception("@skipIfRemote can only be used to decorate a " |
| 813 | "test method") |
| 814 | |
| 815 | @wraps(func) |
| 816 | def wrapper(*args, **kwargs): |
| 817 | if remote_list and lldb.remote_platform: |
| 818 | self = args[0] |
| 819 | triple = self.dbg.GetSelectedPlatform().GetTriple() |
| 820 | for r in remote_list: |
| 821 | if r in triple: |
| 822 | func(*args, **kwargs) |
| 823 | return |
| 824 | self.skipTest("skip on remote platform %s" % str(triple)) |
| 825 | else: |
| 826 | func(*args, **kwargs) |
| 827 | return wrapper |
| 828 | |
| 829 | return myImpl |
| 830 | |
Greg Clayton | 1251456 | 2013-12-05 22:22:32 +0000 | [diff] [blame] | 831 | def skipIfRemoteDueToDeadlock(func): |
| 832 | """Decorate the item to skip tests if testing remotely due to the test deadlocking.""" |
| 833 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 834 | raise Exception("@skipIfRemote can only be used to decorate a test method") |
| 835 | @wraps(func) |
| 836 | def wrapper(*args, **kwargs): |
| 837 | from unittest2 import case |
| 838 | if lldb.remote_platform: |
| 839 | self = args[0] |
| 840 | self.skipTest("skip on remote platform (deadlocks)") |
| 841 | else: |
| 842 | func(*args, **kwargs) |
| 843 | return wrapper |
| 844 | |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 845 | def skipIfNoSBHeaders(func): |
| 846 | """Decorate the item to mark tests that should be skipped when LLDB is built with no SB API headers.""" |
| 847 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
Ed Maste | 59cca5d | 2014-10-07 01:57:52 +0000 | [diff] [blame] | 848 | raise Exception("@skipIfNoSBHeaders can only be used to decorate a test method") |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 849 | @wraps(func) |
| 850 | def wrapper(*args, **kwargs): |
| 851 | from unittest2 import case |
| 852 | self = args[0] |
Shawn Best | 181b09b | 2014-11-08 00:04:04 +0000 | [diff] [blame] | 853 | if sys.platform.startswith("darwin"): |
| 854 | header = os.path.join(self.lib_dir, 'LLDB.framework', 'Versions','Current','Headers','LLDB.h') |
| 855 | else: |
| 856 | header = os.path.join(os.environ["LLDB_SRC"], "include", "lldb", "API", "LLDB.h") |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 857 | platform = sys.platform |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 858 | if not os.path.exists(header): |
| 859 | self.skipTest("skip because LLDB.h header not found") |
| 860 | else: |
| 861 | func(*args, **kwargs) |
| 862 | return wrapper |
| 863 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 864 | def skipIfFreeBSD(func): |
| 865 | """Decorate the item to skip tests that should be skipped on FreeBSD.""" |
| 866 | return skipIfPlatform(["freebsd"])(func) |
Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 867 | |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 868 | def getDarwinOSTriples(): |
| 869 | return ['darwin', 'macosx', 'ios'] |
| 870 | |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 871 | def skipIfDarwin(func): |
| 872 | """Decorate the item to skip tests that should be skipped on Darwin.""" |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 873 | return skipIfPlatform(getDarwinOSTriples())(func) |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 874 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 875 | def skipIfLinux(func): |
| 876 | """Decorate the item to skip tests that should be skipped on Linux.""" |
| 877 | return skipIfPlatform(["linux"])(func) |
| 878 | |
Oleksiy Vyalov | abb5a35 | 2015-07-29 22:18:16 +0000 | [diff] [blame] | 879 | def skipUnlessHostLinux(func): |
| 880 | """Decorate the item to skip tests that should be skipped on any non Linux host.""" |
| 881 | return skipUnlessHostPlatform(["linux"])(func) |
| 882 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 883 | def skipIfWindows(func): |
| 884 | """Decorate the item to skip tests that should be skipped on Windows.""" |
| 885 | return skipIfPlatform(["windows"])(func) |
| 886 | |
Chaoren Lin | e6eea5d | 2015-06-08 22:13:28 +0000 | [diff] [blame] | 887 | def skipIfHostWindows(func): |
| 888 | """Decorate the item to skip tests that should be skipped on Windows.""" |
| 889 | return skipIfHostPlatform(["windows"])(func) |
| 890 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 891 | def skipUnlessDarwin(func): |
| 892 | """Decorate the item to skip tests that should be skipped on any non Darwin platform.""" |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 893 | return skipUnlessPlatform(getDarwinOSTriples())(func) |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 894 | |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 895 | def getPlatform(): |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 896 | """Returns the target platform which the tests are running on.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 897 | platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2] |
| 898 | if platform.startswith('freebsd'): |
| 899 | platform = 'freebsd' |
| 900 | return platform |
| 901 | |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 902 | def getHostPlatform(): |
| 903 | """Returns the host platform running the test suite.""" |
| 904 | # Attempts to return a platform name matching a target Triple platform. |
| 905 | if sys.platform.startswith('linux'): |
| 906 | return 'linux' |
| 907 | elif sys.platform.startswith('win32'): |
| 908 | return 'windows' |
| 909 | elif sys.platform.startswith('darwin'): |
| 910 | return 'darwin' |
| 911 | elif sys.platform.startswith('freebsd'): |
| 912 | return 'freebsd' |
| 913 | else: |
| 914 | return sys.platform |
| 915 | |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 916 | def platformIsDarwin(): |
| 917 | """Returns true if the OS triple for the selected platform is any valid apple OS""" |
| 918 | return getPlatform() in getDarwinOSTriples() |
| 919 | |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 920 | def skipIfHostIncompatibleWithRemote(func): |
| 921 | """Decorate the item to skip tests if binaries built on this host are incompatible.""" |
| 922 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 923 | raise Exception("@skipIfHostIncompatibleWithRemote can only be used to decorate a test method") |
| 924 | @wraps(func) |
| 925 | def wrapper(*args, **kwargs): |
| 926 | from unittest2 import case |
| 927 | self = args[0] |
| 928 | host_arch = self.getLldbArchitecture() |
| 929 | host_platform = getHostPlatform() |
| 930 | target_arch = self.getArchitecture() |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 931 | target_platform = 'darwin' if self.platformIsDarwin() else self.getPlatform() |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 932 | if not (target_arch == 'x86_64' and host_arch == 'i386') and host_arch != target_arch: |
| 933 | self.skipTest("skipping because target %s is not compatible with host architecture %s" % (target_arch, host_arch)) |
| 934 | elif target_platform != host_platform: |
| 935 | self.skipTest("skipping because target is %s but host is %s" % (target_platform, host_platform)) |
| 936 | else: |
| 937 | func(*args, **kwargs) |
| 938 | return wrapper |
| 939 | |
Chaoren Lin | e6eea5d | 2015-06-08 22:13:28 +0000 | [diff] [blame] | 940 | def skipIfHostPlatform(oslist): |
| 941 | """Decorate the item to skip tests if running on one of the listed host platforms.""" |
| 942 | return unittest2.skipIf(getHostPlatform() in oslist, |
| 943 | "skip on %s" % (", ".join(oslist))) |
| 944 | |
| 945 | def skipUnlessHostPlatform(oslist): |
| 946 | """Decorate the item to skip tests unless running on one of the listed host platforms.""" |
| 947 | return unittest2.skipUnless(getHostPlatform() in oslist, |
| 948 | "requires on of %s" % (", ".join(oslist))) |
| 949 | |
Zachary Turner | 793d997 | 2015-08-14 23:29:24 +0000 | [diff] [blame] | 950 | def skipUnlessArch(archlist): |
| 951 | """Decorate the item to skip tests unless running on one of the listed architectures.""" |
| 952 | def myImpl(func): |
| 953 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 954 | raise Exception("@skipUnlessArch can only be used to decorate a test method") |
| 955 | |
| 956 | @wraps(func) |
| 957 | def wrapper(*args, **kwargs): |
| 958 | self = args[0] |
| 959 | if self.getArchitecture() not in archlist: |
| 960 | self.skipTest("skipping for architecture %s (requires one of %s)" % |
| 961 | (self.getArchitecture(), ", ".join(archlist))) |
| 962 | else: |
| 963 | func(*args, **kwargs) |
| 964 | return wrapper |
| 965 | |
| 966 | return myImpl |
| 967 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 968 | def skipIfPlatform(oslist): |
| 969 | """Decorate the item to skip tests if running on one of the listed platforms.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 970 | return unittest2.skipIf(getPlatform() in oslist, |
| 971 | "skip on %s" % (", ".join(oslist))) |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 972 | |
| 973 | def skipUnlessPlatform(oslist): |
| 974 | """Decorate the item to skip tests unless running on one of the listed platforms.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 975 | return unittest2.skipUnless(getPlatform() in oslist, |
| 976 | "requires on of %s" % (", ".join(oslist))) |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 977 | |
Daniel Malea | 4835990 | 2013-05-14 20:48:54 +0000 | [diff] [blame] | 978 | def skipIfLinuxClang(func): |
| 979 | """Decorate the item to skip tests that should be skipped if building on |
| 980 | Linux with clang. |
| 981 | """ |
| 982 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 983 | raise Exception("@skipIfLinuxClang can only be used to decorate a test method") |
| 984 | @wraps(func) |
| 985 | def wrapper(*args, **kwargs): |
| 986 | from unittest2 import case |
| 987 | self = args[0] |
| 988 | compiler = self.getCompiler() |
Vince Harron | c849267 | 2015-05-04 02:59:19 +0000 | [diff] [blame] | 989 | platform = self.getPlatform() |
| 990 | if "clang" in compiler and platform == "linux": |
Daniel Malea | 4835990 | 2013-05-14 20:48:54 +0000 | [diff] [blame] | 991 | self.skipTest("skipping because Clang is used on Linux") |
| 992 | else: |
| 993 | func(*args, **kwargs) |
| 994 | return wrapper |
| 995 | |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 996 | # provide a function to skip on defined oslist, compiler version, and archs |
| 997 | # if none is specified for any argument, that argument won't be checked and thus means for all |
| 998 | # for example, |
| 999 | # @skipIf, skip for all platform/compiler/arch, |
| 1000 | # @skipIf(compiler='gcc'), skip for gcc on all platform/architecture |
| 1001 | # @skipIf(bugnumber, ["linux"], "gcc", ['>=', '4.9'], ['i386']), skip for gcc>=4.9 on linux with i386 |
| 1002 | |
| 1003 | # TODO: refactor current code, to make skipIfxxx functions to call this function |
| 1004 | def skipIf(bugnumber=None, oslist=None, compiler=None, compiler_version=None, archs=None): |
| 1005 | def fn(self): |
| 1006 | return ((oslist is None or self.getPlatform() in oslist) and |
| 1007 | (compiler is None or (compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version))) and |
| 1008 | self.expectedArch(archs)) |
| 1009 | return skipTestIfFn(fn, bugnumber, skipReason="skipping because os:%s compiler: %s %s arch: %s"%(oslist, compiler, compiler_version, archs)) |
| 1010 | |
| 1011 | def skipTestIfFn(expected_fn, bugnumber=None, skipReason=None): |
| 1012 | def skipTestIfFn_impl(func): |
| 1013 | @wraps(func) |
| 1014 | def wrapper(*args, **kwargs): |
| 1015 | from unittest2 import case |
| 1016 | self = args[0] |
| 1017 | if expected_fn(self): |
| 1018 | self.skipTest(skipReason) |
| 1019 | else: |
| 1020 | func(*args, **kwargs) |
| 1021 | return wrapper |
| 1022 | if callable(bugnumber): |
| 1023 | return skipTestIfFn_impl(bugnumber) |
| 1024 | else: |
| 1025 | return skipTestIfFn_impl |
| 1026 | |
Daniel Malea | be23079 | 2013-01-24 23:52:09 +0000 | [diff] [blame] | 1027 | def skipIfGcc(func): |
| 1028 | """Decorate the item to skip tests that should be skipped if building with gcc .""" |
| 1029 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1030 | raise Exception("@skipIfGcc can only be used to decorate a test method") |
Daniel Malea | be23079 | 2013-01-24 23:52:09 +0000 | [diff] [blame] | 1031 | @wraps(func) |
| 1032 | def wrapper(*args, **kwargs): |
| 1033 | from unittest2 import case |
| 1034 | self = args[0] |
| 1035 | compiler = self.getCompiler() |
| 1036 | if "gcc" in compiler: |
| 1037 | self.skipTest("skipping because gcc is the test compiler") |
| 1038 | else: |
| 1039 | func(*args, **kwargs) |
| 1040 | return wrapper |
| 1041 | |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 1042 | def skipIfIcc(func): |
| 1043 | """Decorate the item to skip tests that should be skipped if building with icc .""" |
| 1044 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1045 | raise Exception("@skipIfIcc can only be used to decorate a test method") |
| 1046 | @wraps(func) |
| 1047 | def wrapper(*args, **kwargs): |
| 1048 | from unittest2 import case |
| 1049 | self = args[0] |
| 1050 | compiler = self.getCompiler() |
| 1051 | if "icc" in compiler: |
| 1052 | self.skipTest("skipping because icc is the test compiler") |
| 1053 | else: |
| 1054 | func(*args, **kwargs) |
| 1055 | return wrapper |
| 1056 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1057 | def skipIfi386(func): |
| 1058 | """Decorate the item to skip tests that should be skipped if building 32-bit.""" |
| 1059 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1060 | raise Exception("@skipIfi386 can only be used to decorate a test method") |
| 1061 | @wraps(func) |
| 1062 | def wrapper(*args, **kwargs): |
| 1063 | from unittest2 import case |
| 1064 | self = args[0] |
| 1065 | if "i386" == self.getArchitecture(): |
| 1066 | self.skipTest("skipping because i386 is not a supported architecture") |
| 1067 | else: |
| 1068 | func(*args, **kwargs) |
| 1069 | return wrapper |
| 1070 | |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 1071 | def skipIfTargetAndroid(api_levels=None, archs=None): |
Siva Chandra | 77f20fc | 2015-06-05 19:54:49 +0000 | [diff] [blame] | 1072 | """Decorator to skip tests when the target is Android. |
| 1073 | |
| 1074 | Arguments: |
| 1075 | api_levels - The API levels for which the test should be skipped. If |
| 1076 | it is None, then the test will be skipped for all API levels. |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 1077 | arch - A sequence of architecture names specifying the architectures |
| 1078 | for which a test is skipped. None means all architectures. |
Siva Chandra | 77f20fc | 2015-06-05 19:54:49 +0000 | [diff] [blame] | 1079 | """ |
| 1080 | def myImpl(func): |
| 1081 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1082 | raise Exception("@skipIfTargetAndroid can only be used to " |
| 1083 | "decorate a test method") |
| 1084 | @wraps(func) |
| 1085 | def wrapper(*args, **kwargs): |
| 1086 | from unittest2 import case |
| 1087 | self = args[0] |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 1088 | if matchAndroid(api_levels, archs)(self): |
| 1089 | self.skipTest("skiped on Android target with API %d and architecture %s" % |
| 1090 | (android_device_api(), self.getArchitecture())) |
Tamas Berghammer | 1253a81 | 2015-03-13 10:12:25 +0000 | [diff] [blame] | 1091 | func(*args, **kwargs) |
Siva Chandra | 77f20fc | 2015-06-05 19:54:49 +0000 | [diff] [blame] | 1092 | return wrapper |
| 1093 | return myImpl |
Tamas Berghammer | 1253a81 | 2015-03-13 10:12:25 +0000 | [diff] [blame] | 1094 | |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 1095 | def skipUnlessCompilerRt(func): |
| 1096 | """Decorate the item to skip tests if testing remotely.""" |
| 1097 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1098 | raise Exception("@skipUnless can only be used to decorate a test method") |
| 1099 | @wraps(func) |
| 1100 | def wrapper(*args, **kwargs): |
| 1101 | from unittest2 import case |
| 1102 | import os.path |
| 1103 | compilerRtPath = os.path.join(os.path.dirname(__file__), "..", "..", "..", "projects", "compiler-rt") |
| 1104 | if not os.path.exists(compilerRtPath): |
| 1105 | self = args[0] |
| 1106 | self.skipTest("skip if compiler-rt not found") |
| 1107 | else: |
| 1108 | func(*args, **kwargs) |
| 1109 | return wrapper |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1110 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1111 | class _PlatformContext(object): |
| 1112 | """Value object class which contains platform-specific options.""" |
| 1113 | |
| 1114 | def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension): |
| 1115 | self.shlib_environment_var = shlib_environment_var |
| 1116 | self.shlib_prefix = shlib_prefix |
| 1117 | self.shlib_extension = shlib_extension |
| 1118 | |
| 1119 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1120 | class Base(unittest2.TestCase): |
Johnny Chen | 8334dad | 2010-10-22 23:15:46 +0000 | [diff] [blame] | 1121 | """ |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1122 | Abstract base for performing lldb (see TestBase) or other generic tests (see |
| 1123 | BenchBase for one example). lldbtest.Base works with the test driver to |
| 1124 | accomplish things. |
| 1125 | |
Johnny Chen | 8334dad | 2010-10-22 23:15:46 +0000 | [diff] [blame] | 1126 | """ |
Enrico Granata | 5020f95 | 2012-10-24 21:42:49 +0000 | [diff] [blame] | 1127 | |
Enrico Granata | 1918627 | 2012-10-24 21:44:48 +0000 | [diff] [blame] | 1128 | # The concrete subclass should override this attribute. |
| 1129 | mydir = None |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1130 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1131 | # Keep track of the old current working directory. |
| 1132 | oldcwd = None |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1133 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 1134 | @staticmethod |
| 1135 | def compute_mydir(test_file): |
| 1136 | '''Subclasses should call this function to correctly calculate the required "mydir" attribute as follows: |
| 1137 | |
| 1138 | mydir = TestBase.compute_mydir(__file__)''' |
| 1139 | test_dir = os.path.dirname(test_file) |
| 1140 | return test_dir[len(os.environ["LLDB_TEST"])+1:] |
| 1141 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1142 | def TraceOn(self): |
| 1143 | """Returns True if we are in trace mode (tracing detailed test execution).""" |
| 1144 | return traceAlways |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 1145 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1146 | @classmethod |
| 1147 | def setUpClass(cls): |
Johnny Chen | da88434 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 1148 | """ |
| 1149 | Python unittest framework class setup fixture. |
| 1150 | Do current directory manipulation. |
| 1151 | """ |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 1152 | # Fail fast if 'mydir' attribute is not overridden. |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1153 | if not cls.mydir or len(cls.mydir) == 0: |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 1154 | raise Exception("Subclasses must override the 'mydir' attribute.") |
Enrico Granata | 7e137e3 | 2012-10-24 18:14:21 +0000 | [diff] [blame] | 1155 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1156 | # Save old working directory. |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1157 | cls.oldcwd = os.getcwd() |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1158 | |
| 1159 | # Change current working directory if ${LLDB_TEST} is defined. |
| 1160 | # See also dotest.py which sets up ${LLDB_TEST}. |
| 1161 | if ("LLDB_TEST" in os.environ): |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1162 | full_dir = os.path.join(os.environ["LLDB_TEST"], cls.mydir) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1163 | if traceAlways: |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1164 | print >> sys.stderr, "Change dir to:", full_dir |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1165 | os.chdir(os.path.join(os.environ["LLDB_TEST"], cls.mydir)) |
| 1166 | |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1167 | if debug_confirm_directory_exclusivity: |
Zachary Turner | b48b404 | 2015-05-21 20:16:02 +0000 | [diff] [blame] | 1168 | import lock |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1169 | cls.dir_lock = lock.Lock(os.path.join(full_dir, ".dirlock")) |
| 1170 | try: |
| 1171 | cls.dir_lock.try_acquire() |
| 1172 | # write the class that owns the lock into the lock file |
| 1173 | cls.dir_lock.handle.write(cls.__name__) |
| 1174 | except IOError as ioerror: |
| 1175 | # nothing else should have this directory lock |
| 1176 | # wait here until we get a lock |
| 1177 | cls.dir_lock.acquire() |
| 1178 | # read the previous owner from the lock file |
| 1179 | lock_id = cls.dir_lock.handle.read() |
| 1180 | print >> sys.stderr, "LOCK ERROR: {} wants to lock '{}' but it is already locked by '{}'".format(cls.__name__, full_dir, lock_id) |
| 1181 | raise ioerror |
| 1182 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1183 | # Set platform context. |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1184 | if platformIsDarwin(): |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1185 | cls.platformContext = _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib') |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1186 | elif getPlatform() == "linux" or getPlatform() == "freebsd": |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1187 | cls.platformContext = _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so') |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 1188 | else: |
| 1189 | cls.platformContext = None |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1190 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1191 | @classmethod |
| 1192 | def tearDownClass(cls): |
Johnny Chen | da88434 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 1193 | """ |
| 1194 | Python unittest framework class teardown fixture. |
| 1195 | Do class-wide cleanup. |
| 1196 | """ |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1197 | |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1198 | if doCleanup and not lldb.skip_build_and_cleanup: |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 1199 | # First, let's do the platform-specific cleanup. |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 1200 | module = builder_module() |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1201 | module.cleanup() |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1202 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 1203 | # Subclass might have specific cleanup function defined. |
| 1204 | if getattr(cls, "classCleanup", None): |
| 1205 | if traceAlways: |
| 1206 | print >> sys.stderr, "Call class-specific cleanup function for class:", cls |
| 1207 | try: |
| 1208 | cls.classCleanup() |
| 1209 | except: |
| 1210 | exc_type, exc_value, exc_tb = sys.exc_info() |
| 1211 | traceback.print_exception(exc_type, exc_value, exc_tb) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1212 | |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1213 | if debug_confirm_directory_exclusivity: |
| 1214 | cls.dir_lock.release() |
| 1215 | del cls.dir_lock |
| 1216 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1217 | # Restore old working directory. |
| 1218 | if traceAlways: |
Johnny Chen | 703dbd0 | 2010-09-30 17:06:24 +0000 | [diff] [blame] | 1219 | print >> sys.stderr, "Restore dir to:", cls.oldcwd |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1220 | os.chdir(cls.oldcwd) |
| 1221 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1222 | @classmethod |
| 1223 | def skipLongRunningTest(cls): |
| 1224 | """ |
| 1225 | By default, we skip long running test case. |
| 1226 | This can be overridden by passing '-l' to the test driver (dotest.py). |
| 1227 | """ |
| 1228 | if "LLDB_SKIP_LONG_RUNNING_TEST" in os.environ and "NO" == os.environ["LLDB_SKIP_LONG_RUNNING_TEST"]: |
| 1229 | return False |
| 1230 | else: |
| 1231 | return True |
Johnny Chen | ed49202 | 2011-06-21 00:53:00 +0000 | [diff] [blame] | 1232 | |
Vince Harron | 6d3d0f1 | 2015-05-10 22:01:59 +0000 | [diff] [blame] | 1233 | def enableLogChannelsForCurrentTest(self): |
| 1234 | if len(lldbtest_config.channels) == 0: |
| 1235 | return |
| 1236 | |
| 1237 | # if debug channels are specified in lldbtest_config.channels, |
| 1238 | # create a new set of log files for every test |
| 1239 | log_basename = self.getLogBasenameForCurrentTest() |
| 1240 | |
| 1241 | # confirm that the file is writeable |
| 1242 | host_log_path = "{}-host.log".format(log_basename) |
| 1243 | open(host_log_path, 'w').close() |
| 1244 | |
| 1245 | log_enable = "log enable -Tpn -f {} ".format(host_log_path) |
| 1246 | for channel_with_categories in lldbtest_config.channels: |
| 1247 | channel_then_categories = channel_with_categories.split(' ', 1) |
| 1248 | channel = channel_then_categories[0] |
| 1249 | if len(channel_then_categories) > 1: |
| 1250 | categories = channel_then_categories[1] |
| 1251 | else: |
| 1252 | categories = "default" |
| 1253 | |
| 1254 | if channel == "gdb-remote": |
| 1255 | # communicate gdb-remote categories to debugserver |
| 1256 | os.environ["LLDB_DEBUGSERVER_LOG_FLAGS"] = categories |
| 1257 | |
| 1258 | self.ci.HandleCommand(log_enable + channel_with_categories, self.res) |
| 1259 | if not self.res.Succeeded(): |
| 1260 | raise Exception('log enable failed (check LLDB_LOG_OPTION env variable)') |
| 1261 | |
| 1262 | # Communicate log path name to debugserver & lldb-server |
| 1263 | server_log_path = "{}-server.log".format(log_basename) |
| 1264 | open(server_log_path, 'w').close() |
| 1265 | os.environ["LLDB_DEBUGSERVER_LOG_FILE"] = server_log_path |
| 1266 | |
| 1267 | # Communicate channels to lldb-server |
| 1268 | os.environ["LLDB_SERVER_LOG_CHANNELS"] = ":".join(lldbtest_config.channels) |
| 1269 | |
| 1270 | if len(lldbtest_config.channels) == 0: |
| 1271 | return |
| 1272 | |
| 1273 | def disableLogChannelsForCurrentTest(self): |
| 1274 | # close all log files that we opened |
| 1275 | for channel_and_categories in lldbtest_config.channels: |
| 1276 | # channel format - <channel-name> [<category0> [<category1> ...]] |
| 1277 | channel = channel_and_categories.split(' ', 1)[0] |
| 1278 | self.ci.HandleCommand("log disable " + channel, self.res) |
| 1279 | if not self.res.Succeeded(): |
| 1280 | raise Exception('log disable failed (check LLDB_LOG_OPTION env variable)') |
| 1281 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1282 | def setUp(self): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1283 | """Fixture for unittest test case setup. |
| 1284 | |
| 1285 | It works with the test driver to conditionally skip tests and does other |
| 1286 | initializations.""" |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1287 | #import traceback |
| 1288 | #traceback.print_stack() |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1289 | |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 1290 | if "LIBCXX_PATH" in os.environ: |
| 1291 | self.libcxxPath = os.environ["LIBCXX_PATH"] |
| 1292 | else: |
| 1293 | self.libcxxPath = None |
| 1294 | |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 1295 | if "LLDBMI_EXEC" in os.environ: |
| 1296 | self.lldbMiExec = os.environ["LLDBMI_EXEC"] |
| 1297 | else: |
| 1298 | self.lldbMiExec = None |
| 1299 | self.dont_do_lldbmi_test = True |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 1300 | |
Johnny Chen | ebe5172 | 2011-10-07 19:21:09 +0000 | [diff] [blame] | 1301 | # If we spawn an lldb process for test (via pexpect), do not load the |
| 1302 | # init file unless told otherwise. |
| 1303 | if "NO_LLDBINIT" in os.environ and "NO" == os.environ["NO_LLDBINIT"]: |
| 1304 | self.lldbOption = "" |
| 1305 | else: |
| 1306 | self.lldbOption = "--no-lldbinit" |
Johnny Chen | aaa82ff | 2011-08-02 22:54:37 +0000 | [diff] [blame] | 1307 | |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1308 | # Assign the test method name to self.testMethodName. |
| 1309 | # |
| 1310 | # For an example of the use of this attribute, look at test/types dir. |
| 1311 | # There are a bunch of test cases under test/types and we don't want the |
| 1312 | # module cacheing subsystem to be confused with executable name "a.out" |
| 1313 | # used for all the test cases. |
| 1314 | self.testMethodName = self._testMethodName |
| 1315 | |
Johnny Chen | f3e22ac | 2010-12-10 18:52:10 +0000 | [diff] [blame] | 1316 | # Python API only test is decorated with @python_api_test, |
| 1317 | # which also sets the "__python_api_test__" attribute of the |
| 1318 | # function object to True. |
Johnny Chen | 4533dad | 2011-05-31 23:21:42 +0000 | [diff] [blame] | 1319 | try: |
| 1320 | if lldb.just_do_python_api_test: |
| 1321 | testMethod = getattr(self, self._testMethodName) |
| 1322 | if getattr(testMethod, "__python_api_test__", False): |
| 1323 | pass |
| 1324 | else: |
Johnny Chen | 5ccbccf | 2011-07-30 01:39:58 +0000 | [diff] [blame] | 1325 | self.skipTest("non python api test") |
| 1326 | except AttributeError: |
| 1327 | pass |
| 1328 | |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 1329 | # lldb-mi only test is decorated with @lldbmi_test, |
| 1330 | # which also sets the "__lldbmi_test__" attribute of the |
| 1331 | # function object to True. |
| 1332 | try: |
| 1333 | if lldb.just_do_lldbmi_test: |
| 1334 | testMethod = getattr(self, self._testMethodName) |
| 1335 | if getattr(testMethod, "__lldbmi_test__", False): |
| 1336 | pass |
| 1337 | else: |
| 1338 | self.skipTest("non lldb-mi test") |
| 1339 | except AttributeError: |
| 1340 | pass |
| 1341 | |
Johnny Chen | 5ccbccf | 2011-07-30 01:39:58 +0000 | [diff] [blame] | 1342 | # Benchmarks test is decorated with @benchmarks_test, |
| 1343 | # which also sets the "__benchmarks_test__" attribute of the |
| 1344 | # function object to True. |
| 1345 | try: |
| 1346 | if lldb.just_do_benchmarks_test: |
| 1347 | testMethod = getattr(self, self._testMethodName) |
| 1348 | if getattr(testMethod, "__benchmarks_test__", False): |
| 1349 | pass |
| 1350 | else: |
| 1351 | self.skipTest("non benchmarks test") |
Johnny Chen | 4533dad | 2011-05-31 23:21:42 +0000 | [diff] [blame] | 1352 | except AttributeError: |
| 1353 | pass |
Johnny Chen | f3e22ac | 2010-12-10 18:52:10 +0000 | [diff] [blame] | 1354 | |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1355 | # This is for the case of directly spawning 'lldb'/'gdb' and interacting |
| 1356 | # with it using pexpect. |
| 1357 | self.child = None |
| 1358 | self.child_prompt = "(lldb) " |
| 1359 | # If the child is interacting with the embedded script interpreter, |
| 1360 | # there are two exits required during tear down, first to quit the |
| 1361 | # embedded script interpreter and second to quit the lldb command |
| 1362 | # interpreter. |
| 1363 | self.child_in_script_interpreter = False |
| 1364 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1365 | # These are for customized teardown cleanup. |
| 1366 | self.dict = None |
| 1367 | self.doTearDownCleanup = False |
| 1368 | # And in rare cases where there are multiple teardown cleanups. |
| 1369 | self.dicts = [] |
| 1370 | self.doTearDownCleanups = False |
| 1371 | |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1372 | # List of spawned subproces.Popen objects |
| 1373 | self.subprocesses = [] |
| 1374 | |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1375 | # List of forked process PIDs |
| 1376 | self.forkedProcessPids = [] |
| 1377 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1378 | # Create a string buffer to record the session info, to be dumped into a |
| 1379 | # test case specific file if test failure is encountered. |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1380 | self.log_basename = self.getLogBasenameForCurrentTest() |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1381 | |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1382 | session_file = "{}.log".format(self.log_basename) |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1383 | unbuffered = 0 # 0 is the constant for unbuffered |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1384 | self.session = open(session_file, "w", unbuffered) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1385 | |
| 1386 | # Optimistically set __errored__, __failed__, __expected__ to False |
| 1387 | # initially. If the test errored/failed, the session info |
| 1388 | # (self.session) is then dumped into a session specific file for |
| 1389 | # diagnosis. |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1390 | self.__cleanup_errored__ = False |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1391 | self.__errored__ = False |
| 1392 | self.__failed__ = False |
| 1393 | self.__expected__ = False |
| 1394 | # We are also interested in unexpected success. |
| 1395 | self.__unexpected__ = False |
Johnny Chen | f79b076 | 2011-08-16 00:48:58 +0000 | [diff] [blame] | 1396 | # And skipped tests. |
| 1397 | self.__skipped__ = False |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1398 | |
| 1399 | # See addTearDownHook(self, hook) which allows the client to add a hook |
| 1400 | # function to be run during tearDown() time. |
| 1401 | self.hooks = [] |
| 1402 | |
| 1403 | # See HideStdout(self). |
| 1404 | self.sys_stdout_hidden = False |
| 1405 | |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 1406 | if self.platformContext: |
| 1407 | # set environment variable names for finding shared libraries |
| 1408 | self.dylibPath = self.platformContext.shlib_environment_var |
Daniel Malea | 179ff29 | 2012-11-26 21:21:11 +0000 | [diff] [blame] | 1409 | |
Vince Harron | 6d3d0f1 | 2015-05-10 22:01:59 +0000 | [diff] [blame] | 1410 | # Create the debugger instance if necessary. |
| 1411 | try: |
| 1412 | self.dbg = lldb.DBG |
| 1413 | except AttributeError: |
| 1414 | self.dbg = lldb.SBDebugger.Create() |
| 1415 | |
| 1416 | if not self.dbg: |
| 1417 | raise Exception('Invalid debugger instance') |
| 1418 | |
| 1419 | # Retrieve the associated command interpreter instance. |
| 1420 | self.ci = self.dbg.GetCommandInterpreter() |
| 1421 | if not self.ci: |
| 1422 | raise Exception('Could not get the command interpreter') |
| 1423 | |
| 1424 | # And the result object. |
| 1425 | self.res = lldb.SBCommandReturnObject() |
| 1426 | |
| 1427 | self.enableLogChannelsForCurrentTest() |
| 1428 | |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1429 | def runHooks(self, child=None, child_prompt=None, use_cmd_api=False): |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1430 | """Perform the run hooks to bring lldb debugger to the desired state. |
| 1431 | |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1432 | By default, expect a pexpect spawned child and child prompt to be |
| 1433 | supplied (use_cmd_api=False). If use_cmd_api is true, ignore the child |
| 1434 | and child prompt and use self.runCmd() to run the hooks one by one. |
| 1435 | |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1436 | Note that child is a process spawned by pexpect.spawn(). If not, your |
| 1437 | test case is mostly likely going to fail. |
| 1438 | |
| 1439 | See also dotest.py where lldb.runHooks are processed/populated. |
| 1440 | """ |
| 1441 | if not lldb.runHooks: |
| 1442 | self.skipTest("No runhooks specified for lldb, skip the test") |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1443 | if use_cmd_api: |
| 1444 | for hook in lldb.runhooks: |
| 1445 | self.runCmd(hook) |
| 1446 | else: |
| 1447 | if not child or not child_prompt: |
| 1448 | self.fail("Both child and child_prompt need to be defined.") |
| 1449 | for hook in lldb.runHooks: |
| 1450 | child.sendline(hook) |
| 1451 | child.expect_exact(child_prompt) |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1452 | |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 1453 | def setAsync(self, value): |
| 1454 | """ Sets async mode to True/False and ensures it is reset after the testcase completes.""" |
| 1455 | old_async = self.dbg.GetAsync() |
| 1456 | self.dbg.SetAsync(value) |
| 1457 | self.addTearDownHook(lambda: self.dbg.SetAsync(old_async)) |
| 1458 | |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1459 | def cleanupSubprocesses(self): |
| 1460 | # Ensure any subprocesses are cleaned up |
| 1461 | for p in self.subprocesses: |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1462 | p.terminate() |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1463 | del p |
| 1464 | del self.subprocesses[:] |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1465 | # Ensure any forked processes are cleaned up |
| 1466 | for pid in self.forkedProcessPids: |
| 1467 | if os.path.exists("/proc/" + str(pid)): |
| 1468 | os.kill(pid, signal.SIGTERM) |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1469 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 1470 | def spawnSubprocess(self, executable, args=[], install_remote=True): |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1471 | """ Creates a subprocess.Popen object with the specified executable and arguments, |
| 1472 | saves it in self.subprocesses, and returns the object. |
| 1473 | NOTE: if using this function, ensure you also call: |
| 1474 | |
| 1475 | self.addTearDownHook(self.cleanupSubprocesses) |
| 1476 | |
| 1477 | otherwise the test suite will leak processes. |
| 1478 | """ |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 1479 | proc = _RemoteProcess(install_remote) if lldb.remote_platform else _LocalProcess(self.TraceOn()) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1480 | proc.launch(executable, args) |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1481 | self.subprocesses.append(proc) |
| 1482 | return proc |
| 1483 | |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1484 | def forkSubprocess(self, executable, args=[]): |
| 1485 | """ Fork a subprocess with its own group ID. |
| 1486 | NOTE: if using this function, ensure you also call: |
| 1487 | |
| 1488 | self.addTearDownHook(self.cleanupSubprocesses) |
| 1489 | |
| 1490 | otherwise the test suite will leak processes. |
| 1491 | """ |
| 1492 | child_pid = os.fork() |
| 1493 | if child_pid == 0: |
| 1494 | # If more I/O support is required, this can be beefed up. |
| 1495 | fd = os.open(os.devnull, os.O_RDWR) |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1496 | os.dup2(fd, 1) |
| 1497 | os.dup2(fd, 2) |
| 1498 | # This call causes the child to have its of group ID |
| 1499 | os.setpgid(0,0) |
| 1500 | os.execvp(executable, [executable] + args) |
| 1501 | # Give the child time to get through the execvp() call |
| 1502 | time.sleep(0.1) |
| 1503 | self.forkedProcessPids.append(child_pid) |
| 1504 | return child_pid |
| 1505 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1506 | def HideStdout(self): |
| 1507 | """Hide output to stdout from the user. |
| 1508 | |
| 1509 | During test execution, there might be cases where we don't want to show the |
| 1510 | standard output to the user. For example, |
| 1511 | |
| 1512 | self.runCmd(r'''sc print "\n\n\tHello!\n"''') |
| 1513 | |
| 1514 | tests whether command abbreviation for 'script' works or not. There is no |
| 1515 | need to show the 'Hello' output to the user as long as the 'script' command |
| 1516 | succeeds and we are not in TraceOn() mode (see the '-t' option). |
| 1517 | |
| 1518 | In this case, the test method calls self.HideStdout(self) to redirect the |
| 1519 | sys.stdout to a null device, and restores the sys.stdout upon teardown. |
| 1520 | |
| 1521 | Note that you should only call this method at most once during a test case |
| 1522 | execution. Any subsequent call has no effect at all.""" |
| 1523 | if self.sys_stdout_hidden: |
| 1524 | return |
| 1525 | |
| 1526 | self.sys_stdout_hidden = True |
| 1527 | old_stdout = sys.stdout |
| 1528 | sys.stdout = open(os.devnull, 'w') |
| 1529 | def restore_stdout(): |
| 1530 | sys.stdout = old_stdout |
| 1531 | self.addTearDownHook(restore_stdout) |
| 1532 | |
| 1533 | # ======================================================================= |
| 1534 | # Methods for customized teardown cleanups as well as execution of hooks. |
| 1535 | # ======================================================================= |
| 1536 | |
| 1537 | def setTearDownCleanup(self, dictionary=None): |
| 1538 | """Register a cleanup action at tearDown() time with a dictinary""" |
| 1539 | self.dict = dictionary |
| 1540 | self.doTearDownCleanup = True |
| 1541 | |
| 1542 | def addTearDownCleanup(self, dictionary): |
| 1543 | """Add a cleanup action at tearDown() time with a dictinary""" |
| 1544 | self.dicts.append(dictionary) |
| 1545 | self.doTearDownCleanups = True |
| 1546 | |
| 1547 | def addTearDownHook(self, hook): |
| 1548 | """ |
| 1549 | Add a function to be run during tearDown() time. |
| 1550 | |
| 1551 | Hooks are executed in a first come first serve manner. |
| 1552 | """ |
| 1553 | if callable(hook): |
| 1554 | with recording(self, traceAlways) as sbuf: |
| 1555 | print >> sbuf, "Adding tearDown hook:", getsource_if_available(hook) |
| 1556 | self.hooks.append(hook) |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1557 | |
| 1558 | return self |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1559 | |
Jim Ingham | da3a386 | 2014-10-16 23:02:14 +0000 | [diff] [blame] | 1560 | def deletePexpectChild(self): |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1561 | # This is for the case of directly spawning 'lldb' and interacting with it |
| 1562 | # using pexpect. |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1563 | if self.child and self.child.isalive(): |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 1564 | import pexpect |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1565 | with recording(self, traceAlways) as sbuf: |
| 1566 | print >> sbuf, "tearing down the child process...." |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1567 | try: |
Daniel Malea | c9a0ec3 | 2013-02-22 00:41:26 +0000 | [diff] [blame] | 1568 | if self.child_in_script_interpreter: |
| 1569 | self.child.sendline('quit()') |
| 1570 | self.child.expect_exact(self.child_prompt) |
| 1571 | self.child.sendline('settings set interpreter.prompt-on-quit false') |
| 1572 | self.child.sendline('quit') |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1573 | self.child.expect(pexpect.EOF) |
Ilia K | 47448c2 | 2015-02-11 21:41:58 +0000 | [diff] [blame] | 1574 | except (ValueError, pexpect.ExceptionPexpect): |
| 1575 | # child is already terminated |
| 1576 | pass |
| 1577 | except OSError as exception: |
| 1578 | import errno |
| 1579 | if exception.errno != errno.EIO: |
| 1580 | # unexpected error |
| 1581 | raise |
Daniel Malea | c9a0ec3 | 2013-02-22 00:41:26 +0000 | [diff] [blame] | 1582 | # child is already terminated |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1583 | pass |
Shawn Best | eb3e905 | 2014-11-06 17:52:15 +0000 | [diff] [blame] | 1584 | finally: |
| 1585 | # Give it one final blow to make sure the child is terminated. |
| 1586 | self.child.close() |
Jim Ingham | da3a386 | 2014-10-16 23:02:14 +0000 | [diff] [blame] | 1587 | |
| 1588 | def tearDown(self): |
| 1589 | """Fixture for unittest test case teardown.""" |
| 1590 | #import traceback |
| 1591 | #traceback.print_stack() |
| 1592 | |
| 1593 | self.deletePexpectChild() |
| 1594 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1595 | # Check and run any hook functions. |
| 1596 | for hook in reversed(self.hooks): |
| 1597 | with recording(self, traceAlways) as sbuf: |
| 1598 | print >> sbuf, "Executing tearDown hook:", getsource_if_available(hook) |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1599 | import inspect |
| 1600 | hook_argc = len(inspect.getargspec(hook).args) |
Enrico Granata | 6e0566c | 2014-11-17 19:00:20 +0000 | [diff] [blame] | 1601 | if hook_argc == 0 or getattr(hook,'im_self',None): |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1602 | hook() |
| 1603 | elif hook_argc == 1: |
| 1604 | hook(self) |
| 1605 | else: |
| 1606 | hook() # try the plain call and hope it works |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1607 | |
| 1608 | del self.hooks |
| 1609 | |
| 1610 | # Perform registered teardown cleanup. |
| 1611 | if doCleanup and self.doTearDownCleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1612 | self.cleanup(dictionary=self.dict) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1613 | |
| 1614 | # In rare cases where there are multiple teardown cleanups added. |
| 1615 | if doCleanup and self.doTearDownCleanups: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1616 | if self.dicts: |
| 1617 | for dict in reversed(self.dicts): |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1618 | self.cleanup(dictionary=dict) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1619 | |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1620 | self.disableLogChannelsForCurrentTest() |
| 1621 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1622 | # ========================================================= |
| 1623 | # Various callbacks to allow introspection of test progress |
| 1624 | # ========================================================= |
| 1625 | |
| 1626 | def markError(self): |
| 1627 | """Callback invoked when an error (unexpected exception) errored.""" |
| 1628 | self.__errored__ = True |
| 1629 | with recording(self, False) as sbuf: |
| 1630 | # False because there's no need to write "ERROR" to the stderr twice. |
| 1631 | # Once by the Python unittest framework, and a second time by us. |
| 1632 | print >> sbuf, "ERROR" |
| 1633 | |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1634 | def markCleanupError(self): |
| 1635 | """Callback invoked when an error occurs while a test is cleaning up.""" |
| 1636 | self.__cleanup_errored__ = True |
| 1637 | with recording(self, False) as sbuf: |
| 1638 | # False because there's no need to write "CLEANUP_ERROR" to the stderr twice. |
| 1639 | # Once by the Python unittest framework, and a second time by us. |
| 1640 | print >> sbuf, "CLEANUP_ERROR" |
| 1641 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1642 | def markFailure(self): |
| 1643 | """Callback invoked when a failure (test assertion failure) occurred.""" |
| 1644 | self.__failed__ = True |
| 1645 | with recording(self, False) as sbuf: |
| 1646 | # False because there's no need to write "FAIL" to the stderr twice. |
| 1647 | # Once by the Python unittest framework, and a second time by us. |
| 1648 | print >> sbuf, "FAIL" |
| 1649 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1650 | def markExpectedFailure(self,err,bugnumber): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1651 | """Callback invoked when an expected failure/error occurred.""" |
| 1652 | self.__expected__ = True |
| 1653 | with recording(self, False) as sbuf: |
| 1654 | # False because there's no need to write "expected failure" to the |
| 1655 | # stderr twice. |
| 1656 | # Once by the Python unittest framework, and a second time by us. |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1657 | if bugnumber == None: |
| 1658 | print >> sbuf, "expected failure" |
| 1659 | else: |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 1660 | print >> sbuf, "expected failure (problem id:" + str(bugnumber) + ")" |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1661 | |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1662 | def markSkippedTest(self): |
| 1663 | """Callback invoked when a test is skipped.""" |
| 1664 | self.__skipped__ = True |
| 1665 | with recording(self, False) as sbuf: |
| 1666 | # False because there's no need to write "skipped test" to the |
| 1667 | # stderr twice. |
| 1668 | # Once by the Python unittest framework, and a second time by us. |
| 1669 | print >> sbuf, "skipped test" |
| 1670 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1671 | def markUnexpectedSuccess(self, bugnumber): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1672 | """Callback invoked when an unexpected success occurred.""" |
| 1673 | self.__unexpected__ = True |
| 1674 | with recording(self, False) as sbuf: |
| 1675 | # False because there's no need to write "unexpected success" to the |
| 1676 | # stderr twice. |
| 1677 | # Once by the Python unittest framework, and a second time by us. |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1678 | if bugnumber == None: |
| 1679 | print >> sbuf, "unexpected success" |
| 1680 | else: |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 1681 | print >> sbuf, "unexpected success (problem id:" + str(bugnumber) + ")" |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1682 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 1683 | def getRerunArgs(self): |
| 1684 | return " -f %s.%s" % (self.__class__.__name__, self._testMethodName) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1685 | |
| 1686 | def getLogBasenameForCurrentTest(self, prefix=None): |
| 1687 | """ |
| 1688 | returns a partial path that can be used as the beginning of the name of multiple |
| 1689 | log files pertaining to this test |
| 1690 | |
| 1691 | <session-dir>/<arch>-<compiler>-<test-file>.<test-class>.<test-method> |
| 1692 | """ |
| 1693 | dname = os.path.join(os.environ["LLDB_TEST"], |
| 1694 | os.environ["LLDB_SESSION_DIRNAME"]) |
| 1695 | if not os.path.isdir(dname): |
| 1696 | os.mkdir(dname) |
| 1697 | |
| 1698 | compiler = self.getCompiler() |
| 1699 | |
| 1700 | if compiler[1] == ':': |
| 1701 | compiler = compiler[2:] |
Chaoren Lin | 636a0e3 | 2015-07-17 21:40:11 +0000 | [diff] [blame] | 1702 | if os.path.altsep is not None: |
| 1703 | compiler = compiler.replace(os.path.altsep, os.path.sep) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1704 | |
Vince Harron | 19e300f | 2015-05-12 00:50:54 +0000 | [diff] [blame] | 1705 | fname = "{}-{}-{}".format(self.id(), self.getArchitecture(), "_".join(compiler.split(os.path.sep))) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1706 | if len(fname) > 200: |
Vince Harron | 19e300f | 2015-05-12 00:50:54 +0000 | [diff] [blame] | 1707 | fname = "{}-{}-{}".format(self.id(), self.getArchitecture(), compiler.split(os.path.sep)[-1]) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1708 | |
| 1709 | if prefix is not None: |
| 1710 | fname = "{}-{}".format(prefix, fname) |
| 1711 | |
| 1712 | return os.path.join(dname, fname) |
| 1713 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1714 | def dumpSessionInfo(self): |
| 1715 | """ |
| 1716 | Dump the debugger interactions leading to a test error/failure. This |
| 1717 | allows for more convenient postmortem analysis. |
| 1718 | |
| 1719 | See also LLDBTestResult (dotest.py) which is a singlton class derived |
| 1720 | from TextTestResult and overwrites addError, addFailure, and |
| 1721 | addExpectedFailure methods to allow us to to mark the test instance as |
| 1722 | such. |
| 1723 | """ |
| 1724 | |
| 1725 | # We are here because self.tearDown() detected that this test instance |
| 1726 | # either errored or failed. The lldb.test_result singleton contains |
| 1727 | # two lists (erros and failures) which get populated by the unittest |
| 1728 | # framework. Look over there for stack trace information. |
| 1729 | # |
| 1730 | # The lists contain 2-tuples of TestCase instances and strings holding |
| 1731 | # formatted tracebacks. |
| 1732 | # |
| 1733 | # See http://docs.python.org/library/unittest.html#unittest.TestResult. |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1734 | |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1735 | # output tracebacks into session |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1736 | pairs = [] |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1737 | if self.__errored__: |
| 1738 | pairs = lldb.test_result.errors |
| 1739 | prefix = 'Error' |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1740 | if self.__cleanup_errored__: |
| 1741 | pairs = lldb.test_result.cleanup_errors |
| 1742 | prefix = 'CleanupError' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1743 | elif self.__failed__: |
| 1744 | pairs = lldb.test_result.failures |
| 1745 | prefix = 'Failure' |
| 1746 | elif self.__expected__: |
| 1747 | pairs = lldb.test_result.expectedFailures |
| 1748 | prefix = 'ExpectedFailure' |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1749 | elif self.__skipped__: |
| 1750 | prefix = 'SkippedTest' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1751 | elif self.__unexpected__: |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1752 | prefix = 'UnexpectedSuccess' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1753 | else: |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1754 | prefix = 'Success' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1755 | |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1756 | if not self.__unexpected__ and not self.__skipped__: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1757 | for test, traceback in pairs: |
| 1758 | if test is self: |
| 1759 | print >> self.session, traceback |
| 1760 | |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1761 | # put footer (timestamp/rerun instructions) into session |
Johnny Chen | 8082a00 | 2011-08-11 00:16:28 +0000 | [diff] [blame] | 1762 | testMethod = getattr(self, self._testMethodName) |
| 1763 | if getattr(testMethod, "__benchmarks_test__", False): |
| 1764 | benchmarks = True |
| 1765 | else: |
| 1766 | benchmarks = False |
| 1767 | |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1768 | import datetime |
| 1769 | print >> self.session, "Session info generated @", datetime.datetime.now().ctime() |
| 1770 | print >> self.session, "To rerun this test, issue the following command from the 'test' directory:\n" |
| 1771 | print >> self.session, "./dotest.py %s -v %s %s" % (self.getRunOptions(), |
| 1772 | ('+b' if benchmarks else '-t'), |
| 1773 | self.getRerunArgs()) |
| 1774 | self.session.close() |
| 1775 | del self.session |
| 1776 | |
| 1777 | # process the log files |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1778 | log_files_for_this_test = glob.glob(self.log_basename + "*") |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1779 | |
| 1780 | if prefix != 'Success' or lldbtest_config.log_success: |
| 1781 | # keep all log files, rename them to include prefix |
| 1782 | dst_log_basename = self.getLogBasenameForCurrentTest(prefix) |
| 1783 | for src in log_files_for_this_test: |
Zachary Turner | 306278f | 2015-05-26 20:26:29 +0000 | [diff] [blame] | 1784 | if os.path.isfile(src): |
| 1785 | dst = src.replace(self.log_basename, dst_log_basename) |
| 1786 | if os.name == "nt" and os.path.isfile(dst): |
| 1787 | # On Windows, renaming a -> b will throw an exception if b exists. On non-Windows platforms |
| 1788 | # it silently replaces the destination. Ultimately this means that atomic renames are not |
| 1789 | # guaranteed to be possible on Windows, but we need this to work anyway, so just remove the |
| 1790 | # destination first if it already exists. |
| 1791 | os.remove(dst) |
Zachary Turner | 5de068b | 2015-05-26 19:52:24 +0000 | [diff] [blame] | 1792 | |
Zachary Turner | 306278f | 2015-05-26 20:26:29 +0000 | [diff] [blame] | 1793 | os.rename(src, dst) |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1794 | else: |
| 1795 | # success! (and we don't want log files) delete log files |
| 1796 | for log_file in log_files_for_this_test: |
Adrian McCarthy | a729204 | 2015-09-04 20:48:48 +0000 | [diff] [blame] | 1797 | try: |
| 1798 | os.unlink(log_file) |
| 1799 | except: |
| 1800 | # We've seen consistent unlink failures on Windows, perhaps because the |
| 1801 | # just-created log file is being scanned by anti-virus. Empirically, this |
| 1802 | # sleep-and-retry approach allows tests to succeed much more reliably. |
| 1803 | # Attempts to figure out exactly what process was still holding a file handle |
| 1804 | # have failed because running instrumentation like Process Monitor seems to |
| 1805 | # slow things down enough that the problem becomes much less consistent. |
| 1806 | time.sleep(0.5) |
| 1807 | os.unlink(log_file) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1808 | |
| 1809 | # ==================================================== |
| 1810 | # Config. methods supported through a plugin interface |
| 1811 | # (enables reading of the current test configuration) |
| 1812 | # ==================================================== |
| 1813 | |
| 1814 | def getArchitecture(self): |
| 1815 | """Returns the architecture in effect the test suite is running with.""" |
| 1816 | module = builder_module() |
Ed Maste | 0f434e6 | 2015-04-06 15:50:48 +0000 | [diff] [blame] | 1817 | arch = module.getArchitecture() |
| 1818 | if arch == 'amd64': |
| 1819 | arch = 'x86_64' |
| 1820 | return arch |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1821 | |
Vince Harron | 0261376 | 2015-05-04 00:17:53 +0000 | [diff] [blame] | 1822 | def getLldbArchitecture(self): |
| 1823 | """Returns the architecture of the lldb binary.""" |
| 1824 | if not hasattr(self, 'lldbArchitecture'): |
| 1825 | |
| 1826 | # spawn local process |
| 1827 | command = [ |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 1828 | lldbtest_config.lldbExec, |
Vince Harron | 0261376 | 2015-05-04 00:17:53 +0000 | [diff] [blame] | 1829 | "-o", |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 1830 | "file " + lldbtest_config.lldbExec, |
Vince Harron | 0261376 | 2015-05-04 00:17:53 +0000 | [diff] [blame] | 1831 | "-o", |
| 1832 | "quit" |
| 1833 | ] |
| 1834 | |
| 1835 | output = check_output(command) |
| 1836 | str = output.decode("utf-8"); |
| 1837 | |
| 1838 | for line in str.splitlines(): |
| 1839 | m = re.search("Current executable set to '.*' \\((.*)\\)\\.", line) |
| 1840 | if m: |
| 1841 | self.lldbArchitecture = m.group(1) |
| 1842 | break |
| 1843 | |
| 1844 | return self.lldbArchitecture |
| 1845 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1846 | def getCompiler(self): |
| 1847 | """Returns the compiler in effect the test suite is running with.""" |
| 1848 | module = builder_module() |
| 1849 | return module.getCompiler() |
| 1850 | |
Oleksiy Vyalov | dc4067c | 2014-11-26 18:30:04 +0000 | [diff] [blame] | 1851 | def getCompilerBinary(self): |
| 1852 | """Returns the compiler binary the test suite is running with.""" |
| 1853 | return self.getCompiler().split()[0] |
| 1854 | |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1855 | def getCompilerVersion(self): |
| 1856 | """ Returns a string that represents the compiler version. |
| 1857 | Supports: llvm, clang. |
| 1858 | """ |
| 1859 | from lldbutil import which |
| 1860 | version = 'unknown' |
| 1861 | |
Oleksiy Vyalov | dc4067c | 2014-11-26 18:30:04 +0000 | [diff] [blame] | 1862 | compiler = self.getCompilerBinary() |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 1863 | version_output = system([[which(compiler), "-v"]])[1] |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1864 | for line in version_output.split(os.linesep): |
Greg Clayton | 2a844b7 | 2013-03-06 02:34:51 +0000 | [diff] [blame] | 1865 | m = re.search('version ([0-9\.]+)', line) |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1866 | if m: |
| 1867 | version = m.group(1) |
| 1868 | return version |
| 1869 | |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 1870 | def platformIsDarwin(self): |
| 1871 | """Returns true if the OS triple for the selected platform is any valid apple OS""" |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1872 | return platformIsDarwin() |
Vince Harron | 20952cc | 2015-04-03 01:00:06 +0000 | [diff] [blame] | 1873 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1874 | def getPlatform(self): |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1875 | """Returns the target platform the test suite is running on.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1876 | return getPlatform() |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1877 | |
Daniel Malea | adaaec9 | 2013-08-06 20:51:41 +0000 | [diff] [blame] | 1878 | def isIntelCompiler(self): |
| 1879 | """ Returns true if using an Intel (ICC) compiler, false otherwise. """ |
| 1880 | return any([x in self.getCompiler() for x in ["icc", "icpc", "icl"]]) |
| 1881 | |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 1882 | def expectedCompilerVersion(self, compiler_version): |
| 1883 | """Returns True iff compiler_version[1] matches the current compiler version. |
| 1884 | Use compiler_version[0] to specify the operator used to determine if a match has occurred. |
| 1885 | Any operator other than the following defaults to an equality test: |
| 1886 | '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not' |
| 1887 | """ |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 1888 | if (compiler_version == None): |
| 1889 | return True |
| 1890 | operator = str(compiler_version[0]) |
| 1891 | version = compiler_version[1] |
| 1892 | |
| 1893 | if (version == None): |
| 1894 | return True |
| 1895 | if (operator == '>'): |
| 1896 | return self.getCompilerVersion() > version |
| 1897 | if (operator == '>=' or operator == '=>'): |
| 1898 | return self.getCompilerVersion() >= version |
| 1899 | if (operator == '<'): |
| 1900 | return self.getCompilerVersion() < version |
| 1901 | if (operator == '<=' or operator == '=<'): |
| 1902 | return self.getCompilerVersion() <= version |
| 1903 | if (operator == '!=' or operator == '!' or operator == 'not'): |
| 1904 | return str(version) not in str(self.getCompilerVersion()) |
| 1905 | return str(version) in str(self.getCompilerVersion()) |
| 1906 | |
| 1907 | def expectedCompiler(self, compilers): |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 1908 | """Returns True iff any element of compilers is a sub-string of the current compiler.""" |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 1909 | if (compilers == None): |
| 1910 | return True |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 1911 | |
| 1912 | for compiler in compilers: |
| 1913 | if compiler in self.getCompiler(): |
| 1914 | return True |
| 1915 | |
| 1916 | return False |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 1917 | |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 1918 | def expectedArch(self, archs): |
| 1919 | """Returns True iff any element of archs is a sub-string of the current architecture.""" |
| 1920 | if (archs == None): |
| 1921 | return True |
| 1922 | |
| 1923 | for arch in archs: |
| 1924 | if arch in self.getArchitecture(): |
| 1925 | return True |
| 1926 | |
| 1927 | return False |
| 1928 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1929 | def getRunOptions(self): |
| 1930 | """Command line option for -A and -C to run this test again, called from |
| 1931 | self.dumpSessionInfo().""" |
| 1932 | arch = self.getArchitecture() |
| 1933 | comp = self.getCompiler() |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 1934 | if arch: |
| 1935 | option_str = "-A " + arch |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1936 | else: |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 1937 | option_str = "" |
| 1938 | if comp: |
Johnny Chen | 531c085 | 2012-03-16 20:44:00 +0000 | [diff] [blame] | 1939 | option_str += " -C " + comp |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 1940 | return option_str |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1941 | |
| 1942 | # ================================================== |
| 1943 | # Build methods supported through a plugin interface |
| 1944 | # ================================================== |
| 1945 | |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1946 | def getstdlibFlag(self): |
| 1947 | """ Returns the proper -stdlib flag, or empty if not required.""" |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 1948 | if self.platformIsDarwin() or self.getPlatform() == "freebsd": |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1949 | stdlibflag = "-stdlib=libc++" |
| 1950 | else: |
| 1951 | stdlibflag = "" |
| 1952 | return stdlibflag |
| 1953 | |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1954 | def getstdFlag(self): |
| 1955 | """ Returns the proper stdflag. """ |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1956 | if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): |
Daniel Malea | 0b7c611 | 2013-05-06 19:31:31 +0000 | [diff] [blame] | 1957 | stdflag = "-std=c++0x" |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1958 | else: |
| 1959 | stdflag = "-std=c++11" |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1960 | return stdflag |
| 1961 | |
| 1962 | def buildDriver(self, sources, exe_name): |
| 1963 | """ Platform-specific way to build a program that links with LLDB (via the liblldb.so |
| 1964 | or LLDB.framework). |
| 1965 | """ |
| 1966 | |
| 1967 | stdflag = self.getstdFlag() |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1968 | stdlibflag = self.getstdlibFlag() |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1969 | |
| 1970 | if sys.platform.startswith("darwin"): |
| 1971 | dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB') |
| 1972 | d = {'CXX_SOURCES' : sources, |
| 1973 | 'EXE' : exe_name, |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1974 | 'CFLAGS_EXTRAS' : "%s %s" % (stdflag, stdlibflag), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1975 | 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir, |
Stefanus Du Toit | 0400444 | 2013-07-30 19:19:49 +0000 | [diff] [blame] | 1976 | 'LD_EXTRAS' : "%s -Wl,-rpath,%s" % (dsym, self.lib_dir), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1977 | } |
Ed Maste | 372c24d | 2013-07-25 21:02:34 +0000 | [diff] [blame] | 1978 | elif sys.platform.startswith('freebsd') or sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile': |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 1979 | d = {'CXX_SOURCES' : sources, |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1980 | 'EXE' : exe_name, |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1981 | 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1982 | 'LD_EXTRAS' : "-L%s -llldb" % self.lib_dir} |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 1983 | elif sys.platform.startswith('win'): |
| 1984 | d = {'CXX_SOURCES' : sources, |
| 1985 | 'EXE' : exe_name, |
| 1986 | 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
| 1987 | 'LD_EXTRAS' : "-L%s -lliblldb" % self.implib_dir} |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1988 | if self.TraceOn(): |
| 1989 | print "Building LLDB Driver (%s) from sources %s" % (exe_name, sources) |
| 1990 | |
| 1991 | self.buildDefault(dictionary=d) |
| 1992 | |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1993 | def buildLibrary(self, sources, lib_name): |
| 1994 | """Platform specific way to build a default library. """ |
| 1995 | |
| 1996 | stdflag = self.getstdFlag() |
| 1997 | |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 1998 | if self.platformIsDarwin(): |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1999 | dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB') |
| 2000 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 2001 | 'DYLIB_NAME' : lib_name, |
| 2002 | 'CFLAGS_EXTRAS' : "%s -stdlib=libc++" % stdflag, |
| 2003 | 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir, |
| 2004 | 'LD_EXTRAS' : "%s -Wl,-rpath,%s -dynamiclib" % (dsym, self.lib_dir), |
| 2005 | } |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2006 | elif self.getPlatform() == 'freebsd' or self.getPlatform() == 'linux' or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile': |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2007 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 2008 | 'DYLIB_NAME' : lib_name, |
| 2009 | 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
| 2010 | 'LD_EXTRAS' : "-shared -L%s -llldb" % self.lib_dir} |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2011 | elif self.getPlatform() == 'windows': |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 2012 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 2013 | 'DYLIB_NAME' : lib_name, |
| 2014 | 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
| 2015 | 'LD_EXTRAS' : "-shared -l%s\liblldb.lib" % self.implib_dir} |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2016 | if self.TraceOn(): |
| 2017 | print "Building LLDB Library (%s) from sources %s" % (lib_name, sources) |
| 2018 | |
| 2019 | self.buildDefault(dictionary=d) |
| 2020 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2021 | def buildProgram(self, sources, exe_name): |
| 2022 | """ Platform specific way to build an executable from C/C++ sources. """ |
| 2023 | d = {'CXX_SOURCES' : sources, |
| 2024 | 'EXE' : exe_name} |
| 2025 | self.buildDefault(dictionary=d) |
| 2026 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2027 | def buildDefault(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2028 | """Platform specific way to build the default binaries.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2029 | if lldb.skip_build_and_cleanup: |
| 2030 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2031 | module = builder_module() |
Chaoren Lin | e9bbabc | 2015-07-18 00:37:55 +0000 | [diff] [blame] | 2032 | if target_is_android(): |
| 2033 | dictionary = append_android_envs(dictionary) |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2034 | if not module.buildDefault(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2035 | raise Exception("Don't know how to build default binary") |
| 2036 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2037 | def buildDsym(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2038 | """Platform specific way to build binaries with dsym info.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2039 | if lldb.skip_build_and_cleanup: |
| 2040 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2041 | module = builder_module() |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2042 | if not module.buildDsym(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2043 | raise Exception("Don't know how to build binary with dsym") |
| 2044 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2045 | def buildDwarf(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2046 | """Platform specific way to build binaries with dwarf maps.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2047 | if lldb.skip_build_and_cleanup: |
| 2048 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2049 | module = builder_module() |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 2050 | if target_is_android(): |
Chaoren Lin | e9bbabc | 2015-07-18 00:37:55 +0000 | [diff] [blame] | 2051 | dictionary = append_android_envs(dictionary) |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2052 | if not module.buildDwarf(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2053 | raise Exception("Don't know how to build binary with dwarf") |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2054 | |
Oleksiy Vyalov | 49b71c6 | 2015-01-22 20:03:21 +0000 | [diff] [blame] | 2055 | def signBinary(self, binary_path): |
| 2056 | if sys.platform.startswith("darwin"): |
| 2057 | codesign_cmd = "codesign --force --sign lldb_codesign %s" % (binary_path) |
| 2058 | call(codesign_cmd, shell=True) |
| 2059 | |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 2060 | def findBuiltClang(self): |
| 2061 | """Tries to find and use Clang from the build directory as the compiler (instead of the system compiler).""" |
| 2062 | paths_to_try = [ |
| 2063 | "llvm-build/Release+Asserts/x86_64/Release+Asserts/bin/clang", |
| 2064 | "llvm-build/Debug+Asserts/x86_64/Debug+Asserts/bin/clang", |
| 2065 | "llvm-build/Release/x86_64/Release/bin/clang", |
| 2066 | "llvm-build/Debug/x86_64/Debug/bin/clang", |
| 2067 | ] |
| 2068 | lldb_root_path = os.path.join(os.path.dirname(__file__), "..") |
| 2069 | for p in paths_to_try: |
| 2070 | path = os.path.join(lldb_root_path, p) |
| 2071 | if os.path.exists(path): |
| 2072 | return path |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 2073 | |
| 2074 | # Tries to find clang at the same folder as the lldb |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 2075 | path = os.path.join(os.path.dirname(lldbtest_config.lldbExec), "clang") |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 2076 | if os.path.exists(path): |
| 2077 | return path |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 2078 | |
| 2079 | return os.environ["CC"] |
| 2080 | |
Tamas Berghammer | 765b5e5 | 2015-02-25 13:26:28 +0000 | [diff] [blame] | 2081 | def getBuildFlags(self, use_cpp11=True, use_libcxx=False, use_libstdcxx=False): |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2082 | """ Returns a dictionary (which can be provided to build* functions above) which |
| 2083 | contains OS-specific build flags. |
| 2084 | """ |
| 2085 | cflags = "" |
Tamas Berghammer | 765b5e5 | 2015-02-25 13:26:28 +0000 | [diff] [blame] | 2086 | ldflags = "" |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 2087 | |
| 2088 | # On Mac OS X, unless specifically requested to use libstdc++, use libc++ |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2089 | if not use_libstdcxx and self.platformIsDarwin(): |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 2090 | use_libcxx = True |
| 2091 | |
| 2092 | if use_libcxx and self.libcxxPath: |
| 2093 | cflags += "-stdlib=libc++ " |
| 2094 | if self.libcxxPath: |
| 2095 | libcxxInclude = os.path.join(self.libcxxPath, "include") |
| 2096 | libcxxLib = os.path.join(self.libcxxPath, "lib") |
| 2097 | if os.path.isdir(libcxxInclude) and os.path.isdir(libcxxLib): |
| 2098 | cflags += "-nostdinc++ -I%s -L%s -Wl,-rpath,%s " % (libcxxInclude, libcxxLib, libcxxLib) |
| 2099 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2100 | if use_cpp11: |
| 2101 | cflags += "-std=" |
| 2102 | if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): |
| 2103 | cflags += "c++0x" |
| 2104 | else: |
| 2105 | cflags += "c++11" |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2106 | if self.platformIsDarwin() or self.getPlatform() == "freebsd": |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2107 | cflags += " -stdlib=libc++" |
| 2108 | elif "clang" in self.getCompiler(): |
| 2109 | cflags += " -stdlib=libstdc++" |
| 2110 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2111 | return {'CFLAGS_EXTRAS' : cflags, |
| 2112 | 'LD_EXTRAS' : ldflags, |
| 2113 | } |
| 2114 | |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 2115 | def cleanup(self, dictionary=None): |
| 2116 | """Platform specific way to do cleanup after build.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2117 | if lldb.skip_build_and_cleanup: |
| 2118 | return |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 2119 | module = builder_module() |
| 2120 | if not module.cleanup(self, dictionary): |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2121 | raise Exception("Don't know how to do cleanup with dictionary: "+dictionary) |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 2122 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2123 | def getLLDBLibraryEnvVal(self): |
| 2124 | """ Returns the path that the OS-specific library search environment variable |
| 2125 | (self.dylibPath) should be set to in order for a program to find the LLDB |
| 2126 | library. If an environment variable named self.dylibPath is already set, |
| 2127 | the new path is appended to it and returned. |
| 2128 | """ |
| 2129 | existing_library_path = os.environ[self.dylibPath] if self.dylibPath in os.environ else None |
| 2130 | if existing_library_path: |
| 2131 | return "%s:%s" % (existing_library_path, self.lib_dir) |
| 2132 | elif sys.platform.startswith("darwin"): |
| 2133 | return os.path.join(self.lib_dir, 'LLDB.framework') |
| 2134 | else: |
| 2135 | return self.lib_dir |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2136 | |
Ed Maste | 437f8f6 | 2013-09-09 14:04:04 +0000 | [diff] [blame] | 2137 | def getLibcPlusPlusLibs(self): |
Robert Flack | fa5ad65 | 2015-05-13 20:17:34 +0000 | [diff] [blame] | 2138 | if self.getPlatform() == 'freebsd' or self.getPlatform() == 'linux': |
Ed Maste | 437f8f6 | 2013-09-09 14:04:04 +0000 | [diff] [blame] | 2139 | return ['libc++.so.1'] |
| 2140 | else: |
| 2141 | return ['libc++.1.dylib','libc++abi.dylib'] |
| 2142 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2143 | class TestBase(Base): |
| 2144 | """ |
| 2145 | This abstract base class is meant to be subclassed. It provides default |
| 2146 | implementations for setUpClass(), tearDownClass(), setUp(), and tearDown(), |
| 2147 | among other things. |
| 2148 | |
| 2149 | Important things for test class writers: |
| 2150 | |
| 2151 | - Overwrite the mydir class attribute, otherwise your test class won't |
| 2152 | run. It specifies the relative directory to the top level 'test' so |
| 2153 | the test harness can change to the correct working directory before |
| 2154 | running your test. |
| 2155 | |
| 2156 | - The setUp method sets up things to facilitate subsequent interactions |
| 2157 | with the debugger as part of the test. These include: |
| 2158 | - populate the test method name |
| 2159 | - create/get a debugger set with synchronous mode (self.dbg) |
| 2160 | - get the command interpreter from with the debugger (self.ci) |
| 2161 | - create a result object for use with the command interpreter |
| 2162 | (self.res) |
| 2163 | - plus other stuffs |
| 2164 | |
| 2165 | - The tearDown method tries to perform some necessary cleanup on behalf |
| 2166 | of the test to return the debugger to a good state for the next test. |
| 2167 | These include: |
| 2168 | - execute any tearDown hooks registered by the test method with |
| 2169 | TestBase.addTearDownHook(); examples can be found in |
| 2170 | settings/TestSettings.py |
| 2171 | - kill the inferior process associated with each target, if any, |
| 2172 | and, then delete the target from the debugger's target list |
| 2173 | - perform build cleanup before running the next test method in the |
| 2174 | same test class; examples of registering for this service can be |
| 2175 | found in types/TestIntegerTypes.py with the call: |
| 2176 | - self.setTearDownCleanup(dictionary=d) |
| 2177 | |
| 2178 | - Similarly setUpClass and tearDownClass perform classwise setup and |
| 2179 | teardown fixtures. The tearDownClass method invokes a default build |
| 2180 | cleanup for the entire test class; also, subclasses can implement the |
| 2181 | classmethod classCleanup(cls) to perform special class cleanup action. |
| 2182 | |
| 2183 | - The instance methods runCmd and expect are used heavily by existing |
| 2184 | test cases to send a command to the command interpreter and to perform |
| 2185 | string/pattern matching on the output of such command execution. The |
| 2186 | expect method also provides a mode to peform string/pattern matching |
| 2187 | without running a command. |
| 2188 | |
| 2189 | - The build methods buildDefault, buildDsym, and buildDwarf are used to |
| 2190 | build the binaries used during a particular test scenario. A plugin |
| 2191 | should be provided for the sys.platform running the test suite. The |
| 2192 | Mac OS X implementation is located in plugins/darwin.py. |
| 2193 | """ |
| 2194 | |
| 2195 | # Maximum allowed attempts when launching the inferior process. |
| 2196 | # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable. |
| 2197 | maxLaunchCount = 3; |
| 2198 | |
| 2199 | # Time to wait before the next launching attempt in second(s). |
| 2200 | # Can be overridden by the LLDB_TIME_WAIT_NEXT_LAUNCH environment variable. |
| 2201 | timeWaitNextLaunch = 1.0; |
| 2202 | |
| 2203 | def doDelay(self): |
| 2204 | """See option -w of dotest.py.""" |
| 2205 | if ("LLDB_WAIT_BETWEEN_TEST_CASES" in os.environ and |
| 2206 | os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] == 'YES'): |
| 2207 | waitTime = 1.0 |
| 2208 | if "LLDB_TIME_WAIT_BETWEEN_TEST_CASES" in os.environ: |
| 2209 | waitTime = float(os.environ["LLDB_TIME_WAIT_BETWEEN_TEST_CASES"]) |
| 2210 | time.sleep(waitTime) |
| 2211 | |
Enrico Granata | 165f8af | 2012-09-21 19:10:53 +0000 | [diff] [blame] | 2212 | # Returns the list of categories to which this test case belongs |
| 2213 | # by default, look for a ".categories" file, and read its contents |
| 2214 | # if no such file exists, traverse the hierarchy - we guarantee |
| 2215 | # a .categories to exist at the top level directory so we do not end up |
| 2216 | # looping endlessly - subclasses are free to define their own categories |
| 2217 | # in whatever way makes sense to them |
| 2218 | def getCategories(self): |
| 2219 | import inspect |
| 2220 | import os.path |
| 2221 | folder = inspect.getfile(self.__class__) |
| 2222 | folder = os.path.dirname(folder) |
| 2223 | while folder != '/': |
| 2224 | categories_file_name = os.path.join(folder,".categories") |
| 2225 | if os.path.exists(categories_file_name): |
| 2226 | categories_file = open(categories_file_name,'r') |
| 2227 | categories = categories_file.readline() |
| 2228 | categories_file.close() |
| 2229 | categories = str.replace(categories,'\n','') |
| 2230 | categories = str.replace(categories,'\r','') |
| 2231 | return categories.split(',') |
| 2232 | else: |
| 2233 | folder = os.path.dirname(folder) |
| 2234 | continue |
| 2235 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2236 | def setUp(self): |
| 2237 | #import traceback |
| 2238 | #traceback.print_stack() |
| 2239 | |
| 2240 | # Works with the test driver to conditionally skip tests via decorators. |
| 2241 | Base.setUp(self) |
| 2242 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2243 | try: |
| 2244 | if lldb.blacklist: |
| 2245 | className = self.__class__.__name__ |
| 2246 | classAndMethodName = "%s.%s" % (className, self._testMethodName) |
| 2247 | if className in lldb.blacklist: |
| 2248 | self.skipTest(lldb.blacklist.get(className)) |
| 2249 | elif classAndMethodName in lldb.blacklist: |
| 2250 | self.skipTest(lldb.blacklist.get(classAndMethodName)) |
| 2251 | except AttributeError: |
| 2252 | pass |
| 2253 | |
Johnny Chen | ed49202 | 2011-06-21 00:53:00 +0000 | [diff] [blame] | 2254 | # Insert some delay between successive test cases if specified. |
| 2255 | self.doDelay() |
Johnny Chen | 0ed37c9 | 2010-10-07 02:04:14 +0000 | [diff] [blame] | 2256 | |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2257 | if "LLDB_MAX_LAUNCH_COUNT" in os.environ: |
| 2258 | self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"]) |
| 2259 | |
Johnny Chen | 430eb76 | 2010-10-19 16:00:42 +0000 | [diff] [blame] | 2260 | if "LLDB_TIME_WAIT_NEXT_LAUNCH" in os.environ: |
Johnny Chen | 4921b11 | 2010-11-29 20:20:34 +0000 | [diff] [blame] | 2261 | self.timeWaitNextLaunch = float(os.environ["LLDB_TIME_WAIT_NEXT_LAUNCH"]) |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2262 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2263 | # |
| 2264 | # Warning: MAJOR HACK AHEAD! |
| 2265 | # If we are running testsuite remotely (by checking lldb.lldbtest_remote_sandbox), |
| 2266 | # redefine the self.dbg.CreateTarget(filename) method to execute a "file filename" |
| 2267 | # command, instead. See also runCmd() where it decorates the "file filename" call |
| 2268 | # with additional functionality when running testsuite remotely. |
| 2269 | # |
| 2270 | if lldb.lldbtest_remote_sandbox: |
| 2271 | def DecoratedCreateTarget(arg): |
| 2272 | self.runCmd("file %s" % arg) |
| 2273 | target = self.dbg.GetSelectedTarget() |
| 2274 | # |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 2275 | # SBtarget.LaunchSimple () currently not working for remote platform? |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2276 | # johnny @ 04/23/2012 |
| 2277 | # |
| 2278 | def DecoratedLaunchSimple(argv, envp, wd): |
| 2279 | self.runCmd("run") |
| 2280 | return target.GetProcess() |
| 2281 | target.LaunchSimple = DecoratedLaunchSimple |
| 2282 | |
| 2283 | return target |
| 2284 | self.dbg.CreateTarget = DecoratedCreateTarget |
| 2285 | if self.TraceOn(): |
| 2286 | print "self.dbg.Create is redefined to:\n%s" % getsource_if_available(DecoratedCreateTarget) |
| 2287 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 2288 | # We want our debugger to be synchronous. |
| 2289 | self.dbg.SetAsync(False) |
| 2290 | |
| 2291 | # Retrieve the associated command interpreter instance. |
| 2292 | self.ci = self.dbg.GetCommandInterpreter() |
| 2293 | if not self.ci: |
| 2294 | raise Exception('Could not get the command interpreter') |
| 2295 | |
| 2296 | # And the result object. |
| 2297 | self.res = lldb.SBCommandReturnObject() |
| 2298 | |
Johnny Chen | 44d2497 | 2012-04-16 18:55:15 +0000 | [diff] [blame] | 2299 | # Run global pre-flight code, if defined via the config file. |
| 2300 | if lldb.pre_flight: |
| 2301 | lldb.pre_flight(self) |
| 2302 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2303 | if lldb.remote_platform: |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 2304 | remote_test_dir = lldbutil.join_remote_paths( |
| 2305 | lldb.remote_platform_working_dir, |
| 2306 | self.getArchitecture(), |
| 2307 | str(self.test_number), |
| 2308 | self.mydir) |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2309 | error = lldb.remote_platform.MakeDirectory(remote_test_dir, 0700) |
| 2310 | if error.Success(): |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2311 | lldb.remote_platform.SetWorkingDirectory(remote_test_dir) |
| 2312 | else: |
| 2313 | print "error: making remote directory '%s': %s" % (remote_test_dir, error) |
| 2314 | |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2315 | def registerSharedLibrariesWithTarget(self, target, shlibs): |
| 2316 | '''If we are remotely running the test suite, register the shared libraries with the target so they get uploaded, otherwise do nothing |
| 2317 | |
| 2318 | Any modules in the target that have their remote install file specification set will |
| 2319 | get uploaded to the remote host. This function registers the local copies of the |
| 2320 | shared libraries with the target and sets their remote install locations so they will |
| 2321 | be uploaded when the target is run. |
| 2322 | ''' |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 2323 | if not shlibs or not self.platformContext: |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2324 | return None |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2325 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2326 | shlib_environment_var = self.platformContext.shlib_environment_var |
| 2327 | shlib_prefix = self.platformContext.shlib_prefix |
| 2328 | shlib_extension = '.' + self.platformContext.shlib_extension |
| 2329 | |
| 2330 | working_dir = self.get_process_working_directory() |
| 2331 | environment = ['%s=%s' % (shlib_environment_var, working_dir)] |
| 2332 | # Add any shared libraries to our target if remote so they get |
| 2333 | # uploaded into the working directory on the remote side |
| 2334 | for name in shlibs: |
| 2335 | # The path can be a full path to a shared library, or a make file name like "Foo" for |
| 2336 | # "libFoo.dylib" or "libFoo.so", or "Foo.so" for "Foo.so" or "libFoo.so", or just a |
| 2337 | # basename like "libFoo.so". So figure out which one it is and resolve the local copy |
| 2338 | # of the shared library accordingly |
| 2339 | if os.path.exists(name): |
| 2340 | local_shlib_path = name # name is the full path to the local shared library |
| 2341 | else: |
| 2342 | # Check relative names |
| 2343 | local_shlib_path = os.path.join(os.getcwd(), shlib_prefix + name + shlib_extension) |
| 2344 | if not os.path.exists(local_shlib_path): |
| 2345 | local_shlib_path = os.path.join(os.getcwd(), name + shlib_extension) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2346 | if not os.path.exists(local_shlib_path): |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2347 | local_shlib_path = os.path.join(os.getcwd(), name) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2348 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2349 | # Make sure we found the local shared library in the above code |
| 2350 | self.assertTrue(os.path.exists(local_shlib_path)) |
| 2351 | |
| 2352 | # Add the shared library to our target |
| 2353 | shlib_module = target.AddModule(local_shlib_path, None, None, None) |
| 2354 | if lldb.remote_platform: |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2355 | # We must set the remote install location if we want the shared library |
| 2356 | # to get uploaded to the remote target |
Chaoren Lin | 5d76b1b | 2015-06-06 00:25:50 +0000 | [diff] [blame] | 2357 | remote_shlib_path = lldbutil.append_to_process_working_directory(os.path.basename(local_shlib_path)) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2358 | shlib_module.SetRemoteInstallFileSpec(lldb.SBFileSpec(remote_shlib_path, False)) |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2359 | |
| 2360 | return environment |
| 2361 | |
Enrico Granata | 4481816 | 2012-10-24 01:23:57 +0000 | [diff] [blame] | 2362 | # utility methods that tests can use to access the current objects |
| 2363 | def target(self): |
| 2364 | if not self.dbg: |
| 2365 | raise Exception('Invalid debugger instance') |
| 2366 | return self.dbg.GetSelectedTarget() |
| 2367 | |
| 2368 | def process(self): |
| 2369 | if not self.dbg: |
| 2370 | raise Exception('Invalid debugger instance') |
| 2371 | return self.dbg.GetSelectedTarget().GetProcess() |
| 2372 | |
| 2373 | def thread(self): |
| 2374 | if not self.dbg: |
| 2375 | raise Exception('Invalid debugger instance') |
| 2376 | return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread() |
| 2377 | |
| 2378 | def frame(self): |
| 2379 | if not self.dbg: |
| 2380 | raise Exception('Invalid debugger instance') |
| 2381 | return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() |
| 2382 | |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 2383 | def get_process_working_directory(self): |
| 2384 | '''Get the working directory that should be used when launching processes for local or remote processes.''' |
| 2385 | if lldb.remote_platform: |
| 2386 | # Remote tests set the platform working directory up in TestBase.setUp() |
| 2387 | return lldb.remote_platform.GetWorkingDirectory() |
| 2388 | else: |
| 2389 | # local tests change directory into each test subdirectory |
| 2390 | return os.getcwd() |
| 2391 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 2392 | def tearDown(self): |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 2393 | #import traceback |
| 2394 | #traceback.print_stack() |
| 2395 | |
Johnny Chen | 3794ad9 | 2011-06-15 21:24:24 +0000 | [diff] [blame] | 2396 | # Delete the target(s) from the debugger as a general cleanup step. |
| 2397 | # This includes terminating the process for each target, if any. |
| 2398 | # We'd like to reuse the debugger for our next test without incurring |
| 2399 | # the initialization overhead. |
| 2400 | targets = [] |
| 2401 | for target in self.dbg: |
| 2402 | if target: |
| 2403 | targets.append(target) |
| 2404 | process = target.GetProcess() |
| 2405 | if process: |
| 2406 | rc = self.invoke(process, "Kill") |
| 2407 | self.assertTrue(rc.Success(), PROCESS_KILLED) |
| 2408 | for target in targets: |
| 2409 | self.dbg.DeleteTarget(target) |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 2410 | |
Johnny Chen | 44d2497 | 2012-04-16 18:55:15 +0000 | [diff] [blame] | 2411 | # Run global post-flight code, if defined via the config file. |
| 2412 | if lldb.post_flight: |
| 2413 | lldb.post_flight(self) |
| 2414 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 2415 | # Do this last, to make sure it's in reverse order from how we setup. |
| 2416 | Base.tearDown(self) |
| 2417 | |
Zachary Turner | 9581204 | 2015-03-26 18:54:21 +0000 | [diff] [blame] | 2418 | # This must be the last statement, otherwise teardown hooks or other |
| 2419 | # lines might depend on this still being active. |
| 2420 | del self.dbg |
| 2421 | |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2422 | def switch_to_thread_with_stop_reason(self, stop_reason): |
| 2423 | """ |
| 2424 | Run the 'thread list' command, and select the thread with stop reason as |
| 2425 | 'stop_reason'. If no such thread exists, no select action is done. |
| 2426 | """ |
| 2427 | from lldbutil import stop_reason_to_str |
| 2428 | self.runCmd('thread list') |
| 2429 | output = self.res.GetOutput() |
| 2430 | thread_line_pattern = re.compile("^[ *] thread #([0-9]+):.*stop reason = %s" % |
| 2431 | stop_reason_to_str(stop_reason)) |
| 2432 | for line in output.splitlines(): |
| 2433 | matched = thread_line_pattern.match(line) |
| 2434 | if matched: |
| 2435 | self.runCmd('thread select %s' % matched.group(1)) |
| 2436 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2437 | def runCmd(self, cmd, msg=None, check=True, trace=False, inHistory=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2438 | """ |
| 2439 | Ask the command interpreter to handle the command and then check its |
| 2440 | return status. |
| 2441 | """ |
| 2442 | # Fail fast if 'cmd' is not meaningful. |
| 2443 | if not cmd or len(cmd) == 0: |
| 2444 | raise Exception("Bad 'cmd' parameter encountered") |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2445 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2446 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 2447 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2448 | # This is an opportunity to insert the 'platform target-install' command if we are told so |
| 2449 | # via the settig of lldb.lldbtest_remote_sandbox. |
| 2450 | if cmd.startswith("target create "): |
| 2451 | cmd = cmd.replace("target create ", "file ") |
| 2452 | if cmd.startswith("file ") and lldb.lldbtest_remote_sandbox: |
| 2453 | with recording(self, trace) as sbuf: |
| 2454 | the_rest = cmd.split("file ")[1] |
| 2455 | # Split the rest of the command line. |
| 2456 | atoms = the_rest.split() |
| 2457 | # |
| 2458 | # NOTE: This assumes that the options, if any, follow the file command, |
| 2459 | # instead of follow the specified target. |
| 2460 | # |
| 2461 | target = atoms[-1] |
| 2462 | # Now let's get the absolute pathname of our target. |
| 2463 | abs_target = os.path.abspath(target) |
| 2464 | print >> sbuf, "Found a file command, target (with absolute pathname)=%s" % abs_target |
| 2465 | fpath, fname = os.path.split(abs_target) |
| 2466 | parent_dir = os.path.split(fpath)[0] |
| 2467 | platform_target_install_command = 'platform target-install %s %s' % (fpath, lldb.lldbtest_remote_sandbox) |
| 2468 | print >> sbuf, "Insert this command to be run first: %s" % platform_target_install_command |
| 2469 | self.ci.HandleCommand(platform_target_install_command, self.res) |
| 2470 | # And this is the file command we want to execute, instead. |
| 2471 | # |
| 2472 | # Warning: SIDE EFFECT AHEAD!!! |
| 2473 | # Populate the remote executable pathname into the lldb namespace, |
| 2474 | # so that test cases can grab this thing out of the namespace. |
| 2475 | # |
| 2476 | lldb.lldbtest_remote_sandboxed_executable = abs_target.replace(parent_dir, lldb.lldbtest_remote_sandbox) |
| 2477 | cmd = "file -P %s %s %s" % (lldb.lldbtest_remote_sandboxed_executable, the_rest.replace(target, ''), abs_target) |
| 2478 | print >> sbuf, "And this is the replaced file command: %s" % cmd |
| 2479 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 2480 | running = (cmd.startswith("run") or cmd.startswith("process launch")) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2481 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 2482 | for i in range(self.maxLaunchCount if running else 1): |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2483 | self.ci.HandleCommand(cmd, self.res, inHistory) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2484 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2485 | with recording(self, trace) as sbuf: |
| 2486 | print >> sbuf, "runCmd:", cmd |
Johnny Chen | ab254f5 | 2010-10-15 16:13:00 +0000 | [diff] [blame] | 2487 | if not check: |
Johnny Chen | 27b107b | 2010-10-15 18:52:22 +0000 | [diff] [blame] | 2488 | print >> sbuf, "check of return status not required" |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2489 | if self.res.Succeeded(): |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2490 | print >> sbuf, "output:", self.res.GetOutput() |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2491 | else: |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2492 | print >> sbuf, "runCmd failed!" |
| 2493 | print >> sbuf, self.res.GetError() |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2494 | |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 2495 | if self.res.Succeeded(): |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2496 | break |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2497 | elif running: |
Johnny Chen | cf7f74e | 2011-01-19 02:02:08 +0000 | [diff] [blame] | 2498 | # For process launch, wait some time before possible next try. |
| 2499 | time.sleep(self.timeWaitNextLaunch) |
Johnny Chen | 552d671 | 2012-08-01 19:56:04 +0000 | [diff] [blame] | 2500 | with recording(self, trace) as sbuf: |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2501 | print >> sbuf, "Command '" + cmd + "' failed!" |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2502 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2503 | if check: |
Sean Callanan | 05834cd | 2015-07-01 23:56:30 +0000 | [diff] [blame] | 2504 | self.assertTrue(self.res.Succeeded(), |
| 2505 | msg if msg else CMD_MSG(cmd)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2506 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 2507 | def match (self, str, patterns, msg=None, trace=False, error=False, matching=True, exe=True): |
| 2508 | """run command in str, and match the result against regexp in patterns returning the match object for the first matching pattern |
| 2509 | |
| 2510 | Otherwise, all the arguments have the same meanings as for the expect function""" |
| 2511 | |
| 2512 | trace = (True if traceAlways else trace) |
| 2513 | |
| 2514 | if exe: |
| 2515 | # First run the command. If we are expecting error, set check=False. |
| 2516 | # Pass the assert message along since it provides more semantic info. |
| 2517 | self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error) |
| 2518 | |
| 2519 | # Then compare the output against expected strings. |
| 2520 | output = self.res.GetError() if error else self.res.GetOutput() |
| 2521 | |
| 2522 | # If error is True, the API client expects the command to fail! |
| 2523 | if error: |
| 2524 | self.assertFalse(self.res.Succeeded(), |
| 2525 | "Command '" + str + "' is expected to fail!") |
| 2526 | else: |
| 2527 | # No execution required, just compare str against the golden input. |
| 2528 | output = str |
| 2529 | with recording(self, trace) as sbuf: |
| 2530 | print >> sbuf, "looking at:", output |
| 2531 | |
| 2532 | # The heading says either "Expecting" or "Not expecting". |
| 2533 | heading = "Expecting" if matching else "Not expecting" |
| 2534 | |
| 2535 | for pattern in patterns: |
| 2536 | # Match Objects always have a boolean value of True. |
| 2537 | match_object = re.search(pattern, output) |
| 2538 | matched = bool(match_object) |
| 2539 | with recording(self, trace) as sbuf: |
| 2540 | print >> sbuf, "%s pattern: %s" % (heading, pattern) |
| 2541 | print >> sbuf, "Matched" if matched else "Not matched" |
| 2542 | if matched: |
| 2543 | break |
| 2544 | |
| 2545 | self.assertTrue(matched if matching else not matched, |
| 2546 | msg if msg else EXP_MSG(str, exe)) |
| 2547 | |
| 2548 | return match_object |
| 2549 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2550 | def expect(self, str, msg=None, patterns=None, startstr=None, endstr=None, substrs=None, trace=False, error=False, matching=True, exe=True, inHistory=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2551 | """ |
| 2552 | Similar to runCmd; with additional expect style output matching ability. |
| 2553 | |
| 2554 | Ask the command interpreter to handle the command and then check its |
| 2555 | return status. The 'msg' parameter specifies an informational assert |
| 2556 | message. We expect the output from running the command to start with |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2557 | 'startstr', matches the substrings contained in 'substrs', and regexp |
| 2558 | matches the patterns contained in 'patterns'. |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2559 | |
| 2560 | If the keyword argument error is set to True, it signifies that the API |
| 2561 | client is expecting the command to fail. In this case, the error stream |
Johnny Chen | aa90292 | 2010-09-17 22:45:27 +0000 | [diff] [blame] | 2562 | from running the command is retrieved and compared against the golden |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2563 | input, instead. |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2564 | |
| 2565 | If the keyword argument matching is set to False, it signifies that the API |
| 2566 | client is expecting the output of the command not to match the golden |
| 2567 | input. |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2568 | |
| 2569 | Finally, the required argument 'str' represents the lldb command to be |
| 2570 | sent to the command interpreter. In case the keyword argument 'exe' is |
| 2571 | set to False, the 'str' is treated as a string to be matched/not-matched |
| 2572 | against the golden input. |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2573 | """ |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2574 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 2575 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2576 | if exe: |
| 2577 | # First run the command. If we are expecting error, set check=False. |
Johnny Chen | 62d4f86 | 2010-10-28 21:10:32 +0000 | [diff] [blame] | 2578 | # Pass the assert message along since it provides more semantic info. |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2579 | self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error, inHistory=inHistory) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2580 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2581 | # Then compare the output against expected strings. |
| 2582 | output = self.res.GetError() if error else self.res.GetOutput() |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2583 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2584 | # If error is True, the API client expects the command to fail! |
| 2585 | if error: |
| 2586 | self.assertFalse(self.res.Succeeded(), |
| 2587 | "Command '" + str + "' is expected to fail!") |
| 2588 | else: |
| 2589 | # No execution required, just compare str against the golden input. |
Enrico Granata | bc08ab4 | 2012-10-23 00:09:02 +0000 | [diff] [blame] | 2590 | if isinstance(str,lldb.SBCommandReturnObject): |
| 2591 | output = str.GetOutput() |
| 2592 | else: |
| 2593 | output = str |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2594 | with recording(self, trace) as sbuf: |
| 2595 | print >> sbuf, "looking at:", output |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2596 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2597 | # The heading says either "Expecting" or "Not expecting". |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2598 | heading = "Expecting" if matching else "Not expecting" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2599 | |
| 2600 | # Start from the startstr, if specified. |
| 2601 | # If there's no startstr, set the initial state appropriately. |
| 2602 | matched = output.startswith(startstr) if startstr else (True if matching else False) |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 2603 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2604 | if startstr: |
| 2605 | with recording(self, trace) as sbuf: |
| 2606 | print >> sbuf, "%s start string: %s" % (heading, startstr) |
| 2607 | print >> sbuf, "Matched" if matched else "Not matched" |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 2608 | |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2609 | # Look for endstr, if specified. |
| 2610 | keepgoing = matched if matching else not matched |
| 2611 | if endstr: |
| 2612 | matched = output.endswith(endstr) |
| 2613 | with recording(self, trace) as sbuf: |
| 2614 | print >> sbuf, "%s end string: %s" % (heading, endstr) |
| 2615 | print >> sbuf, "Matched" if matched else "Not matched" |
| 2616 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2617 | # Look for sub strings, if specified. |
| 2618 | keepgoing = matched if matching else not matched |
| 2619 | if substrs and keepgoing: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2620 | for str in substrs: |
Johnny Chen | b052f6c | 2010-09-23 23:35:28 +0000 | [diff] [blame] | 2621 | matched = output.find(str) != -1 |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2622 | with recording(self, trace) as sbuf: |
| 2623 | print >> sbuf, "%s sub string: %s" % (heading, str) |
| 2624 | print >> sbuf, "Matched" if matched else "Not matched" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2625 | keepgoing = matched if matching else not matched |
| 2626 | if not keepgoing: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2627 | break |
| 2628 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2629 | # Search for regular expression patterns, if specified. |
| 2630 | keepgoing = matched if matching else not matched |
| 2631 | if patterns and keepgoing: |
| 2632 | for pattern in patterns: |
| 2633 | # Match Objects always have a boolean value of True. |
| 2634 | matched = bool(re.search(pattern, output)) |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2635 | with recording(self, trace) as sbuf: |
| 2636 | print >> sbuf, "%s pattern: %s" % (heading, pattern) |
| 2637 | print >> sbuf, "Matched" if matched else "Not matched" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2638 | keepgoing = matched if matching else not matched |
| 2639 | if not keepgoing: |
| 2640 | break |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2641 | |
| 2642 | self.assertTrue(matched if matching else not matched, |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 2643 | msg if msg else EXP_MSG(str, exe)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2644 | |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2645 | def invoke(self, obj, name, trace=False): |
Johnny Chen | 61703c9 | 2010-08-25 22:56:10 +0000 | [diff] [blame] | 2646 | """Use reflection to call a method dynamically with no argument.""" |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2647 | trace = (True if traceAlways else trace) |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2648 | |
| 2649 | method = getattr(obj, name) |
| 2650 | import inspect |
| 2651 | self.assertTrue(inspect.ismethod(method), |
| 2652 | name + "is a method name of object: " + str(obj)) |
| 2653 | result = method() |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2654 | with recording(self, trace) as sbuf: |
| 2655 | print >> sbuf, str(method) + ":", result |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2656 | return result |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2657 | |
Johnny Chen | f359cf2 | 2011-05-27 23:36:52 +0000 | [diff] [blame] | 2658 | # ================================================= |
| 2659 | # Misc. helper methods for debugging test execution |
| 2660 | # ================================================= |
| 2661 | |
Johnny Chen | 56b92a7 | 2011-07-11 19:15:11 +0000 | [diff] [blame] | 2662 | def DebugSBValue(self, val): |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2663 | """Debug print a SBValue object, if traceAlways is True.""" |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 2664 | from lldbutil import value_type_to_str |
Johnny Chen | 87bb589 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 2665 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2666 | if not traceAlways: |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2667 | return |
| 2668 | |
| 2669 | err = sys.stderr |
| 2670 | err.write(val.GetName() + ":\n") |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2671 | err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n') |
| 2672 | err.write('\t' + "ByteSize -> " + str(val.GetByteSize()) + '\n') |
| 2673 | err.write('\t' + "NumChildren -> " + str(val.GetNumChildren()) + '\n') |
| 2674 | err.write('\t' + "Value -> " + str(val.GetValue()) + '\n') |
| 2675 | err.write('\t' + "ValueAsUnsigned -> " + str(val.GetValueAsUnsigned())+ '\n') |
| 2676 | err.write('\t' + "ValueType -> " + value_type_to_str(val.GetValueType()) + '\n') |
| 2677 | err.write('\t' + "Summary -> " + str(val.GetSummary()) + '\n') |
| 2678 | err.write('\t' + "IsPointerType -> " + str(val.TypeIsPointerType()) + '\n') |
| 2679 | err.write('\t' + "Location -> " + val.GetLocation() + '\n') |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2680 | |
Johnny Chen | 36c5eb1 | 2011-08-05 20:17:27 +0000 | [diff] [blame] | 2681 | def DebugSBType(self, type): |
| 2682 | """Debug print a SBType object, if traceAlways is True.""" |
| 2683 | if not traceAlways: |
| 2684 | return |
| 2685 | |
| 2686 | err = sys.stderr |
| 2687 | err.write(type.GetName() + ":\n") |
| 2688 | err.write('\t' + "ByteSize -> " + str(type.GetByteSize()) + '\n') |
| 2689 | err.write('\t' + "IsPointerType -> " + str(type.IsPointerType()) + '\n') |
| 2690 | err.write('\t' + "IsReferenceType -> " + str(type.IsReferenceType()) + '\n') |
| 2691 | |
Johnny Chen | b877f1e | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 2692 | def DebugPExpect(self, child): |
| 2693 | """Debug the spwaned pexpect object.""" |
| 2694 | if not traceAlways: |
| 2695 | return |
| 2696 | |
| 2697 | print child |
Filipe Cabecinhas | 0eec15a | 2012-06-20 10:13:40 +0000 | [diff] [blame] | 2698 | |
| 2699 | @classmethod |
| 2700 | def RemoveTempFile(cls, file): |
| 2701 | if os.path.exists(file): |
| 2702 | os.remove(file) |