[lldb] Add assertState function to the API test suite

Add a function to make it easier to debug a test failure caused by an
unexpected state.

Currently, tests are using assertEqual which results in a cryptic error
message: "AssertionError: 5 != 10". Even when a test provides a message
to make it clear why a particular state is expected, you still have to
figure out which of the two was the expected state, and what the other
value corresponds to.

We have a function in lldbutil that helps you convert the state number
into a user readable string. This patch adds a wrapper around
assertEqual specifically for comparing states and reporting better error
messages.

The aforementioned error message now looks like this: "AssertionError:
stopped (5) != exited (10)". If the user provided a message, that
continues to get printed as well.

Differential revision: https://reviews.llvm.org/D127355
This commit is contained in:
Jonas Devlieghere 2022-06-08 15:57:41 -07:00
parent 5ead1f13a2
commit ce825e4674
No known key found for this signature in database
GPG Key ID: 49CC0BD90FDEED4D
24 changed files with 71 additions and 63 deletions

View File

@ -519,9 +519,9 @@ class Base(unittest2.TestCase):
# /abs/path/to/packages/group/subdir/mytest.py -> group/subdir
lldb_test_src = configuration.test_src_root
if not test_file.startswith(lldb_test_src):
raise Exception(
"Test file '%s' must reside within lldb_test_src "
"(which is '%s')." % (test_file, lldb_test_src))
raise Exception(
"Test file '%s' must reside within lldb_test_src "
"(which is '%s')." % (test_file, lldb_test_src))
return os.path.dirname(os.path.relpath(test_file, start=lldb_test_src))
def TraceOn(self):
@ -2474,6 +2474,14 @@ FileCheck output:
self.fail(self._formatMessage(msg,
"'{}' is not success".format(error)))
"""Assert two states are equal"""
def assertState(self, first, second, msg=None):
if first != second:
error = "{} ({}) != {} ({})".format(
lldbutil.state_type_to_str(first), first,
lldbutil.state_type_to_str(second), second)
self.fail(self._formatMessage(msg, error))
def createTestTarget(self, file_path=None, msg=None,
load_dependent_modules=True):
"""

View File

@ -41,8 +41,8 @@ class LaunchWithShellExpandTestCase(TestBase):
process = self.process()
self.assertEquals(process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
thread = process.GetThreadAtIndex(0)
@ -72,8 +72,8 @@ class LaunchWithShellExpandTestCase(TestBase):
process = self.process()
self.assertEquals(process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
thread = process.GetThreadAtIndex(0)
@ -95,8 +95,8 @@ class LaunchWithShellExpandTestCase(TestBase):
process = self.process()
self.assertEquals(process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
thread = process.GetThreadAtIndex(0)

View File

@ -36,8 +36,8 @@ class TestStepOverWatchpoint(TestBase):
process = target.LaunchSimple(None, None,
self.get_process_working_directory())
self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
thread = lldbutil.get_stopped_thread(process,
lldb.eStopReasonBreakpoint)
@ -63,8 +63,8 @@ class TestStepOverWatchpoint(TestBase):
self.assertEquals(thread.GetStopDescription(20), 'watchpoint 1')
process.Continue()
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertEquals(thread.GetStopDescription(20), 'step over')
self.step_inst_for_watchpoint(1)
@ -90,8 +90,8 @@ class TestStepOverWatchpoint(TestBase):
self.assertEquals(thread.GetStopDescription(20), 'watchpoint 2')
process.Continue()
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertEquals(thread.GetStopDescription(20), 'step over')
self.step_inst_for_watchpoint(2)

View File

@ -37,7 +37,7 @@ class ConsecutiveBreakpointsTestCase(TestBase):
def finish_test(self):
# Run the process until termination
self.process.Continue()
self.assertEquals(self.process.GetState(), lldb.eStateExited)
self.assertState(self.process.GetState(), lldb.eStateExited)
@no_debug_info_test
def test_continue(self):
@ -45,7 +45,7 @@ class ConsecutiveBreakpointsTestCase(TestBase):
self.prepare_test()
self.process.Continue()
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
# We should be stopped at the second breakpoint
self.thread = lldbutil.get_one_thread_stopped_at_breakpoint(
self.process, self.breakpoint2)
@ -63,7 +63,7 @@ class ConsecutiveBreakpointsTestCase(TestBase):
step_over = False
self.thread.StepInstruction(step_over)
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(
self.thread.GetFrameAtIndex(0).GetPCAddress().GetLoadAddress(
self.target), self.bkpt_address.GetLoadAddress(
@ -90,7 +90,7 @@ class ConsecutiveBreakpointsTestCase(TestBase):
step_over = False
self.thread.StepInstruction(step_over)
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(
self.thread.GetFrameAtIndex(0).GetPCAddress().GetLoadAddress(
self.target), self.bkpt_address.GetLoadAddress(

View File

@ -32,7 +32,7 @@ class TestMoveNearest(TestBase):
lldbutil.run_break_set_by_symbol(self, 'main', sym_exact=True)
environment = self.registerSharedLibrariesWithTarget(target, ["foo"])
process = target.LaunchSimple(None, environment, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
# Regardless of the -m value the breakpoint should have exactly one
# location on the foo functions

View File

@ -71,7 +71,7 @@ class StepOverBreakpointsTestCase(TestBase):
while True:
self.thread.StepInstruction(True)
step_count = step_count + 1
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertTrue(self.thread.GetStopReason() == lldb.eStopReasonPlanComplete or
self.thread.GetStopReason() == lldb.eStopReasonBreakpoint)
if (self.thread.GetStopReason() == lldb.eStopReasonBreakpoint) :
@ -83,24 +83,24 @@ class StepOverBreakpointsTestCase(TestBase):
# Run the process until termination
self.process.Continue()
self.assertEquals(self.process.GetState(), lldb.eStateExited)
self.assertState(self.process.GetState(), lldb.eStateExited)
@skipIf(bugnumber="llvm.org/pr31972", hostoslist=["windows"])
def test_step_over(self):
self.thread.StepOver()
# We should be stopped at the breakpoint_2 line with stop plan complete reason
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(self.thread.GetStopReason(), lldb.eStopReasonPlanComplete)
self.thread.StepOver()
# We should be stopped at the breakpoint_3 line with stop plan complete reason
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(self.thread.GetStopReason(), lldb.eStopReasonPlanComplete)
self.thread.StepOver()
# We should be stopped at the breakpoint_4
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(self.thread.GetStopReason(), lldb.eStopReasonBreakpoint)
thread1 = lldbutil.get_one_thread_stopped_at_breakpoint(self.process, self.breakpoint4)
self.assertEquals(self.thread, thread1, "Didn't stop at breakpoint 4.")
@ -113,5 +113,5 @@ class StepOverBreakpointsTestCase(TestBase):
# Run the process until termination
self.process.Continue()
self.assertEquals(self.process.GetState(), lldb.eStateExited)
self.assertState(self.process.GetState(), lldb.eStateExited)

View File

@ -43,7 +43,7 @@ class CommandLineCompletionTestCase(TestBase):
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
'// Break here', self.main_source_spec)
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
# Since CommandInterpreter has been corrected to update the current execution
# context at the beginning of HandleCompletion, we're here explicitly testing

View File

@ -65,8 +65,8 @@ class DynamicValueChildCountTestCase(TestBase):
process = target.LaunchSimple(
None, None, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
b = self.frame().FindVariable("b").GetDynamicValue(lldb.eDynamicCanRunTarget)
self.assertEquals(b.GetNumChildren(), 0, "b has 0 children")

View File

@ -63,7 +63,7 @@ class TestStopAtEntry(TestBase):
def test_stop_remote_platform_async(self):
self.do_test_stop_at_entry(False, True)
def do_test_stop_at_entry(self, synchronous, remote):
def do_test_stop_at_entry(self, synchronous, remote):
"""Test the normal launch path in either sync or async mode"""
self.build()
@ -98,7 +98,7 @@ class TestStopAtEntry(TestBase):
result = listener.WaitForEvent(30, event)
self.assertTrue(result, "Timed out waiting for event from process")
state = lldb.SBProcess.GetStateFromEvent(event)
self.assertEqual(state, lldb.eStateStopped, "Didn't get a stopped state after launch")
self.assertState(state, lldb.eStateStopped, "Didn't get a stopped state after launch")
# Okay, we should be stopped. Make sure we are indeed at the
# entry point. I only know how to do this on darwin:
@ -127,13 +127,13 @@ class TestStopAtEntry(TestBase):
self.assertTrue(result, "Timed out waiting for running")
state = lldb.SBProcess.GetStateFromEvent(event)
if num_running == 1:
self.assertEqual(state, lldb.eStateRunning, "Got running event")
self.assertState(state, lldb.eStateRunning, "Got running event")
# The last event we should get is the exited event
self.assertEqual(state, lldb.eStateExited, "Got running event")
self.assertState(state, lldb.eStateExited, "Got exit event")
else:
# Make sure that the process has indeed exited
state = process.GetState()
self.assertEqual(state, lldb.eStateExited);
self.assertState(state, lldb.eStateExited);
def setup_remote_platform(self):
return

View File

@ -62,7 +62,7 @@ class ReturnValueTestCase(TestBase):
thread.StepOut()
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
frame = thread.GetFrameAtIndex(0)
@ -94,7 +94,7 @@ class ReturnValueTestCase(TestBase):
thread.StepOutOfFrame(frame)
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
frame = thread.GetFrameAtIndex(0)
fun_name = frame.GetFunctionName()
@ -123,7 +123,7 @@ class ReturnValueTestCase(TestBase):
in_float = float(in_value.GetValue())
thread.StepOut()
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
frame = thread.GetFrameAtIndex(0)
@ -263,7 +263,7 @@ class ReturnValueTestCase(TestBase):
thread.StepOut()
self.assertEquals(self.process.GetState(), lldb.eStateStopped)
self.assertState(self.process.GetState(), lldb.eStateStopped)
self.assertEquals(thread.GetStopReason(), lldb.eStopReasonPlanComplete)
# Assuming all these functions step out to main. Could figure out the caller dynamically

View File

@ -61,8 +61,8 @@ class DynamicValueTestCase(TestBase):
process = target.LaunchSimple(
None, None, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
threads = lldbutil.get_threads_stopped_at_breakpoint(
process, first_call_bpt)

View File

@ -38,7 +38,7 @@ class TestWithGmodulesDebugInfo(TestBase):
self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
# Get the thread of the process
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(

View File

@ -34,8 +34,8 @@ class ObjCiVarIMPTestCase(TestBase):
process = target.LaunchSimple(
None, None, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.expect(
'frame variable --ptr-depth=1 --show-types -d run -- object',

View File

@ -41,8 +41,8 @@ class ObjCDynamicValueTestCase(TestBase):
process = target.LaunchSimple(
None, None, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
var = self.frame().FindVariable("foo")
var_ptr_type = var.GetType()

View File

@ -63,8 +63,8 @@ class ObjCDynamicValueTestCase(TestBase):
process = target.LaunchSimple(
None, None, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
threads = lldbutil.get_threads_stopped_at_breakpoint(
process, main_before_setProperty_bkpt)

View File

@ -47,8 +47,8 @@ class ObjCPropertyTestCase(TestBase):
process = target.LaunchSimple(
None, None, self.get_process_working_directory())
self.assertEquals(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
self.assertState(process.GetState(), lldb.eStateStopped,
PROCESS_STOPPED)
threads = lldbutil.get_threads_stopped_at_breakpoint(
process, main_bkpt)

View File

@ -33,8 +33,8 @@ class TargetSymbolsAddCommand(TestBase):
self.assertTrue(self.process, PROCESS_IS_VALID)
# The stop reason of the thread should be breakpoint.
self.assertEquals(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
exe_module = self.target.GetModuleAtIndex(0)

View File

@ -30,8 +30,8 @@ class TestMixedDwarfBinary(TestBase):
self.assertTrue(self.process, PROCESS_IS_VALID)
# The stop reason of the thread should be breakpoint.
self.assertEquals(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
frame = self.process.GetThreadAtIndex(0).GetFrameAtIndex(0)
x = frame.FindVariable("x")

View File

@ -79,8 +79,8 @@ class AddDsymDownload(TestBase):
self.assertTrue(self.process, PROCESS_IS_VALID)
# The stop reason of the thread should be breakpoint.
self.assertEquals(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.runCmd(command)
self.expect("frame select", substrs=['a.out`main at main.c'])

View File

@ -36,8 +36,8 @@ class AddDsymMidExecutionCommandCase(TestBase):
self.assertTrue(self.process, PROCESS_IS_VALID)
# The stop reason of the thread should be breakpoint.
self.assertEquals(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.assertState(self.process.GetState(), lldb.eStateStopped,
STOPPED_DUE_TO_BREAKPOINT)
self.runCmd("add-dsym " +
self.getBuildArtifact("hide.app/Contents/a.out.dSYM"))

View File

@ -45,7 +45,7 @@ class SBTypeMemberFunctionsTest(TestBase):
self.assertTrue(process, PROCESS_IS_VALID)
# Get Frame #0.
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(

View File

@ -39,7 +39,7 @@ class ObjCSBTypeTestCase(TestBase):
self.assertTrue(process, PROCESS_IS_VALID)
# Get Frame #0.
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(

View File

@ -59,7 +59,7 @@ class ChangeValueAPITestCase(TestBase):
self.assertTrue(process, PROCESS_IS_VALID)
# Get Frame #0.
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(
@ -136,7 +136,7 @@ class ChangeValueAPITestCase(TestBase):
# Now continue.
process.Continue()
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(
@ -169,7 +169,7 @@ class ChangeValueAPITestCase(TestBase):
process.Continue()
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(

View File

@ -27,7 +27,7 @@ class ValueAPIEmptyClassTestCase(TestBase):
self.assertTrue(process, PROCESS_IS_VALID)
# Get Frame #0.
self.assertEquals(process.GetState(), lldb.eStateStopped)
self.assertState(process.GetState(), lldb.eStateStopped)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
self.assertTrue(