Johnny Chen | a1affab | 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 |
| 12 | entire test suite. Users who want to run a test case on its own can specify the |
| 13 | LLDB_TEST and PYTHONPATH environment variables, for example: |
| 14 | |
| 15 | $ export LLDB_TEST=$PWD |
| 16 | $ export PYTHONPATH=/Volumes/data/lldb/svn/trunk/build/Debug/LLDB.framework/Resources/Python:$LLDB_TEST |
| 17 | $ echo $LLDB_TEST |
| 18 | /Volumes/data/lldb/svn/trunk/test |
| 19 | $ echo $PYTHONPATH |
| 20 | /Volumes/data/lldb/svn/trunk/build/Debug/LLDB.framework/Resources/Python:/Volumes/data/lldb/svn/trunk/test |
| 21 | $ python function_types/TestFunctionTypes.py |
| 22 | . |
| 23 | ---------------------------------------------------------------------- |
| 24 | Ran 1 test in 0.363s |
| 25 | |
| 26 | OK |
| 27 | $ |
| 28 | """ |
| 29 | |
| 30 | import os |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 31 | import unittest2 |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 32 | import lldb |
| 33 | |
Johnny Chen | 96f08d5 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 34 | # |
| 35 | # Some commonly used assert messages. |
| 36 | # |
| 37 | |
| 38 | CURRENT_EXECUTABLE_SET = "Current executable set successfully" |
| 39 | |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 40 | RUN_STOPPED = "Process is stopped successfully" |
Johnny Chen | 96f08d5 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 41 | |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 42 | RUN_COMPLETED = "Process exited successfully" |
Johnny Chen | 96f08d5 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 43 | |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 44 | BREAKPOINT_CREATED = "Breakpoint created successfully" |
| 45 | |
Johnny Chen | 9b92c6e | 2010-08-17 21:33:31 +0000 | [diff] [blame^] | 46 | BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" |
| 47 | |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 48 | BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1" |
Johnny Chen | 96f08d5 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 49 | |
| 50 | STOPPED_DUE_TO_BREAKPOINT = "Process state is stopped due to breakpoint" |
| 51 | |
| 52 | STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" |
| 53 | |
| 54 | VARIABLES_DISPLAYED_CORRECTLY = "Show specified variable(s) correctly" |
| 55 | |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 56 | # |
| 57 | # And a generic "Command '%s' returns successfully" message generator. |
| 58 | # |
| 59 | def CMD_MSG(command): |
| 60 | return "Command '%s' returns successfully" % (command) |
| 61 | |
| 62 | |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 63 | class TestBase(unittest2.TestCase): |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 64 | """This LLDB abstract base class is meant to be subclassed.""" |
| 65 | |
| 66 | # The concrete subclass should override this attribute. |
Johnny Chen | f8c723b | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 67 | mydir = None |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 68 | |
Johnny Chen | ffde4fc | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 69 | # State pertaining to the inferior process, if any. |
| 70 | runStarted = False |
| 71 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 72 | def setUp(self): |
Johnny Chen | 6ead27f | 2010-08-07 01:13:18 +0000 | [diff] [blame] | 73 | #import traceback |
Johnny Chen | 88f8304 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 74 | #traceback.print_stack() |
| 75 | |
Johnny Chen | f8c723b | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 76 | # Fail fast if 'mydir' attribute is not overridden. |
| 77 | if not self.mydir or len(self.mydir) == 0: |
| 78 | raise Exception("Subclasses must override the 'mydir' attribute.") |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 79 | # Save old working directory. |
| 80 | self.oldcwd = os.getcwd() |
| 81 | |
| 82 | # Change current working directory if ${LLDB_TEST} is defined. |
| 83 | # See also dotest.py which sets up ${LLDB_TEST}. |
| 84 | if ("LLDB_TEST" in os.environ): |
| 85 | os.chdir(os.path.join(os.environ["LLDB_TEST"], self.mydir)); |
| 86 | |
| 87 | # Create the debugger instance if necessary. |
| 88 | try: |
| 89 | self.dbg = lldb.DBG |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 90 | except AttributeError: |
| 91 | self.dbg = lldb.SBDebugger.Create() |
Johnny Chen | f8c723b | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 92 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 93 | if not self.dbg.IsValid(): |
| 94 | raise Exception('Invalid debugger instance') |
| 95 | |
| 96 | # We want our debugger to be synchronous. |
| 97 | self.dbg.SetAsync(False) |
| 98 | |
| 99 | # Retrieve the associated command interpreter instance. |
| 100 | self.ci = self.dbg.GetCommandInterpreter() |
| 101 | if not self.ci: |
| 102 | raise Exception('Could not get the command interpreter') |
| 103 | |
| 104 | # And the result object. |
| 105 | self.res = lldb.SBCommandReturnObject() |
| 106 | |
| 107 | |
| 108 | def tearDown(self): |
Johnny Chen | ffde4fc | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 109 | # Finish the inferior process, if it was "run" previously. |
| 110 | if self.runStarted: |
| 111 | self.ci.HandleCommand("continue", self.res) |
| 112 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 113 | del self.dbg |
| 114 | |
| 115 | # Restore old working directory. |
| 116 | os.chdir(self.oldcwd) |