blob: 75f7e518da45506b78091e450be1f28fa6d79c70 [file] [log] [blame]
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001"""
2LLDB module which provides the abstract base class of lldb test case.
3
4The concrete subclass can override lldbtest.TesBase in order to inherit the
5common behavior for unitest.TestCase.setUp/tearDown implemented in this file.
6
7The subclass should override the attribute mydir in order for the python runtime
8to locate the individual test cases when running as part of a large test suite
9or 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
12entire test suite. Users who want to run a test case on its own can specify the
13LLDB_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----------------------------------------------------------------------
24Ran 1 test in 0.363s
25
26OK
27$
28"""
29
30import os
Johnny Chen73258832010-08-05 23:42:46 +000031import unittest2
Johnny Chenbf6ffa32010-07-03 03:41:59 +000032import lldb
33
Johnny Chen00778092010-08-09 22:01:17 +000034#
35# Some commonly used assert messages.
36#
37
38CURRENT_EXECUTABLE_SET = "Current executable set successfully"
39
Johnny Chen17941842010-08-09 23:44:24 +000040RUN_STOPPED = "Process is stopped successfully"
Johnny Chen00778092010-08-09 22:01:17 +000041
Johnny Chen17941842010-08-09 23:44:24 +000042RUN_COMPLETED = "Process exited successfully"
Johnny Chen00778092010-08-09 22:01:17 +000043
Johnny Chen17941842010-08-09 23:44:24 +000044BREAKPOINT_CREATED = "Breakpoint created successfully"
45
46BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1"
Johnny Chen00778092010-08-09 22:01:17 +000047
48STOPPED_DUE_TO_BREAKPOINT = "Process state is stopped due to breakpoint"
49
50STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in"
51
52VARIABLES_DISPLAYED_CORRECTLY = "Show specified variable(s) correctly"
53
Johnny Chen17941842010-08-09 23:44:24 +000054#
55# And a generic "Command '%s' returns successfully" message generator.
56#
57def CMD_MSG(command):
58 return "Command '%s' returns successfully" % (command)
59
60
Johnny Chen73258832010-08-05 23:42:46 +000061class TestBase(unittest2.TestCase):
Johnny Chenbf6ffa32010-07-03 03:41:59 +000062 """This LLDB abstract base class is meant to be subclassed."""
63
64 # The concrete subclass should override this attribute.
Johnny Chenf02ec122010-07-03 20:41:42 +000065 mydir = None
Johnny Chenbf6ffa32010-07-03 03:41:59 +000066
Johnny Chen6ca006c2010-08-16 21:28:10 +000067 # State pertaining to the inferior process, if any.
68 runStarted = False
69
Johnny Chenbf6ffa32010-07-03 03:41:59 +000070 def setUp(self):
Johnny Chen9289a652010-08-07 01:13:18 +000071 #import traceback
Johnny Chena2124952010-08-05 21:23:45 +000072 #traceback.print_stack()
73
Johnny Chenf02ec122010-07-03 20:41:42 +000074 # Fail fast if 'mydir' attribute is not overridden.
75 if not self.mydir or len(self.mydir) == 0:
76 raise Exception("Subclasses must override the 'mydir' attribute.")
Johnny Chenbf6ffa32010-07-03 03:41:59 +000077 # Save old working directory.
78 self.oldcwd = os.getcwd()
79
80 # Change current working directory if ${LLDB_TEST} is defined.
81 # See also dotest.py which sets up ${LLDB_TEST}.
82 if ("LLDB_TEST" in os.environ):
83 os.chdir(os.path.join(os.environ["LLDB_TEST"], self.mydir));
84
85 # Create the debugger instance if necessary.
86 try:
87 self.dbg = lldb.DBG
Johnny Chenbf6ffa32010-07-03 03:41:59 +000088 except AttributeError:
89 self.dbg = lldb.SBDebugger.Create()
Johnny Chenf02ec122010-07-03 20:41:42 +000090
Johnny Chenbf6ffa32010-07-03 03:41:59 +000091 if not self.dbg.IsValid():
92 raise Exception('Invalid debugger instance')
93
94 # We want our debugger to be synchronous.
95 self.dbg.SetAsync(False)
96
97 # Retrieve the associated command interpreter instance.
98 self.ci = self.dbg.GetCommandInterpreter()
99 if not self.ci:
100 raise Exception('Could not get the command interpreter')
101
102 # And the result object.
103 self.res = lldb.SBCommandReturnObject()
104
105
106 def tearDown(self):
Johnny Chen6ca006c2010-08-16 21:28:10 +0000107 # Finish the inferior process, if it was "run" previously.
108 if self.runStarted:
109 self.ci.HandleCommand("continue", self.res)
110
Johnny Chenbf6ffa32010-07-03 03:41:59 +0000111 del self.dbg
112
113 # Restore old working directory.
114 os.chdir(self.oldcwd)