This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG54c2687292da: [lldb] Introduce createTestTarget for creating 
a valid target in API tests (authored by teemperor).
Herald added subscribers: lldb-commits, wenlei.

Changed prior to commit:
  https://reviews.llvm.org/D102771?vs=346444&id=347384#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102771/new/

https://reviews.llvm.org/D102771

Files:
  lldb/packages/Python/lldbsuite/test/lldbtest.py
  lldb/test/API/android/platform/TestDefaultCacheLineSize.py
  lldb/test/API/api/listeners/TestListener.py
  lldb/test/API/assert_messages_test/TestAssertMessages.py
  lldb/test/API/commands/disassemble/basic/TestFrameDisassemble.py
  lldb/test/API/commands/expression/call-function/TestCallBuiltinFunction.py
  
lldb/test/API/commands/expression/completion-crash-invalid-iterator/TestInvalidIteratorCompletionCrash.py
  lldb/test/API/commands/expression/completion/TestExprCompletion.py
  lldb/test/API/commands/expression/error-limit/TestExprErrorLimit.py
  lldb/test/API/commands/expression/expr-in-syscall/TestExpressionInSyscall.py
  lldb/test/API/commands/expression/no-deadlock/TestExprDoesntBlock.py
  lldb/test/API/commands/expression/test/TestExprs.py
  lldb/test/API/commands/frame/language/TestGuessLanguage.py
  lldb/test/API/commands/frame/var/TestFrameVar.py
  
lldb/test/API/commands/process/launch-with-shellexpand/TestLaunchWithShellExpand.py
  lldb/test/API/commands/process/launch/TestProcessLaunch.py
  lldb/test/API/commands/register/register/register_command/TestRegisters.py
  
lldb/test/API/commands/trace/multiple-threads/TestTraceStartStopMultipleThreads.py
  lldb/test/API/commands/watchpoints/multiple_hits/TestMultipleHits.py
  
lldb/test/API/commands/watchpoints/step_over_watchpoint/TestStepOverWatchpoint.py
  lldb/test/API/commands/watchpoints/watchpoint_disable/TestWatchpointDisable.py
  lldb/test/API/commands/watchpoints/watchpoint_events/TestWatchpointEvents.py
  lldb/test/API/functionalities/asan/TestMemoryHistory.py
  lldb/test/API/functionalities/asan/TestReportData.py
  
lldb/test/API/functionalities/breakpoint/address_breakpoints/TestAddressBreakpoints.py
  
lldb/test/API/functionalities/breakpoint/auto_continue/TestBreakpointAutoContinue.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_by_file_colon_line/TestBreakpointByFileColonLine.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_by_line_and_column/TestBreakpointByLineAndColumn.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommand.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_conditions/TestBreakpointConditions.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_hit_count/TestBreakpointHitCount.py
  
lldb/test/API/functionalities/breakpoint/breakpoint_ignore_count/TestBreakpointIgnoreCount.py
  lldb/test/API/sample_test/TestSampleTest.py
  lldb/test/API/sample_test/main.c

Index: lldb/test/API/sample_test/main.c
===================================================================
--- lldb/test/API/sample_test/main.c
+++ lldb/test/API/sample_test/main.c
@@ -1,9 +1,10 @@
 #include <stdio.h>
 
+int test_var = 10;
+
 int
 main()
 {
-  int test_var = 10;
   printf ("Set a breakpoint here: %d.\n", test_var);
   //% test_var = self.frame().FindVariable("test_var")
   //% test_value = test_var.GetValueAsUnsigned()
Index: lldb/test/API/sample_test/TestSampleTest.py
===================================================================
--- lldb/test/API/sample_test/TestSampleTest.py
+++ lldb/test/API/sample_test/TestSampleTest.py
@@ -46,3 +46,8 @@
         test_value = test_var.GetValueAsUnsigned()
         self.assertEqual(test_value, 10, "Got the right value for test_var")
 
+    def sample_test_no_launch(self):
+        """ Same as above but doesn't launch a process."""
+
+        target = self.createTestTarget()
+        self.expect_expr("test_var", result_value="10")
Index: lldb/test/API/functionalities/breakpoint/breakpoint_ignore_count/TestBreakpointIgnoreCount.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_ignore_count/TestBreakpointIgnoreCount.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_ignore_count/TestBreakpointIgnoreCount.py
@@ -99,11 +99,7 @@
 
     def breakpoint_ignore_count_python(self):
         """Use Python APIs to set breakpoint ignore count."""
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint on main.c by name 'c'.
         breakpoint = target.BreakpointCreateByName('c', 'a.out')
Index: lldb/test/API/functionalities/breakpoint/breakpoint_hit_count/TestBreakpointHitCount.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_hit_count/TestBreakpointHitCount.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_hit_count/TestBreakpointHitCount.py
@@ -24,10 +24,7 @@
     def test_breakpoint_one_shot(self):
         """Check that one-shot breakpoints trigger only once."""
         self.build()
-
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         self.runCmd("tb a")
         process = target.LaunchSimple(
@@ -56,10 +53,7 @@
 
     def do_test_breakpoint_location_hit_count(self):
         """Use Python APIs to check breakpoint hit count."""
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Create a breakpoint in main.cpp by name 'a',
         # there should be two locations.
Index: lldb/test/API/functionalities/breakpoint/breakpoint_conditions/TestBreakpointConditions.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_conditions/TestBreakpointConditions.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_conditions/TestBreakpointConditions.py
@@ -118,11 +118,7 @@
 
     def breakpoint_conditions_python(self):
         """Use Python APIs to set breakpoint conditions."""
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint on main.c by name 'c'.
         breakpoint = target.BreakpointCreateByName('c', 'a.out')
Index: lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
@@ -36,11 +36,9 @@
         self.main_source_spec = lldb.SBFileSpec(self.main_source)
 
     def do_set_python_command_from_python(self):
-        exe = self.getBuildArtifact("a.out")
         error = lldb.SBError()
 
-        self.target = self.dbg.CreateTarget(exe)
-        self.assertTrue(self.target, VALID_TARGET)
+        self.target = self.createTestTarget()
 
         body_bkpt = self.target.BreakpointCreateBySourceRegex(
             "Set break point at this line.", self.main_source_spec)
@@ -144,12 +142,9 @@
         self.assertEquals("Not so fancy", side_effect.not_so_fancy)
 
     def do_bad_args_to_python_command(self):
-        exe = self.getBuildArtifact("a.out")
         error = lldb.SBError()
 
-        self.target = self.dbg.CreateTarget(exe)
-        self.assertTrue(self.target, VALID_TARGET)
-
+        self.target = self.createTestTarget()
 
         self.expect("command script import --allow-reload ./bktptcmd.py")
 
Index: lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommand.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommand.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_command/TestBreakpointCommand.py
@@ -270,9 +270,7 @@
 
     def breakpoint_commands_on_creation(self):
         """Test that setting breakpoint commands when creating the breakpoint works"""
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), "Created an invalid target.")
+        target = self.createTestTarget()
 
         # Add a breakpoint.
         lldbutil.run_break_set_by_file_and_line(
@@ -291,9 +289,7 @@
     def test_breakpoint_delete_disabled(self):
         """Test 'break delete --disabled' works"""
         self.build()
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), "Created an invalid target.")
+        target = self.createTestTarget()
 
         bp_1 = target.BreakpointCreateByName("main")
         bp_2 = target.BreakpointCreateByName("not_here")
Index: lldb/test/API/functionalities/breakpoint/breakpoint_by_line_and_column/TestBreakpointByLineAndColumn.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_by_line_and_column/TestBreakpointByLineAndColumn.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_by_line_and_column/TestBreakpointByLineAndColumn.py
@@ -52,7 +52,6 @@
     @skipIf(compiler="gcc", compiler_version=['<', '7.1'])
     def testBreakpointByLineAndColumnNearestCode(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
 
         patterns = [
             "In the middle of a function name (col:42)",
@@ -67,10 +66,7 @@
             column = int(re.search('\(col:([0-9]+)\)', pattern).group(1))
             source_loc.append({'line':line, 'column':column})
 
-        # Create a target from the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
-
+        target = self.createTestTarget()
 
         for loc in source_loc:
             src_file = lldb.SBFileSpec("main.cpp")
Index: lldb/test/API/functionalities/breakpoint/breakpoint_by_file_colon_line/TestBreakpointByFileColonLine.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/breakpoint_by_file_colon_line/TestBreakpointByFileColonLine.py
+++ lldb/test/API/functionalities/breakpoint/breakpoint_by_file_colon_line/TestBreakpointByFileColonLine.py
@@ -16,10 +16,7 @@
 
     def testBreakpointSpecWithLine(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
         
         # This one should work:
         lldbutil.run_break_set_by_file_colon_line(self, "main.c:11", "main.c", 11, num_expected_locations = 1)
@@ -32,10 +29,7 @@
     @skipIf(compiler="gcc", compiler_version=['<', '7.1'])
     def testBreakpointByLine(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         main_c = lldb.SBFileSpec("main.c")
         lldbutil.run_break_set_by_file_colon_line(self, "main.c:11:50", "main.c", 11, num_expected_locations = 1)
Index: lldb/test/API/functionalities/breakpoint/auto_continue/TestBreakpointAutoContinue.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/auto_continue/TestBreakpointAutoContinue.py
+++ lldb/test/API/functionalities/breakpoint/auto_continue/TestBreakpointAutoContinue.py
@@ -32,9 +32,7 @@
 
     def make_target_and_bkpt(self, additional_options=None, num_expected_loc=1,
                              pattern="Set a breakpoint here"):
-        exe = self.getBuildArtifact("a.out")
-        self.target = self.dbg.CreateTarget(exe)
-        self.assertTrue(self.target.IsValid(), "Target is not valid")
+        self.target = self.createTestTarget()
 
         extra_options_txt = "--auto-continue 1 "
         if additional_options:
Index: lldb/test/API/functionalities/breakpoint/address_breakpoints/TestAddressBreakpoints.py
===================================================================
--- lldb/test/API/functionalities/breakpoint/address_breakpoints/TestAddressBreakpoints.py
+++ lldb/test/API/functionalities/breakpoint/address_breakpoints/TestAddressBreakpoints.py
@@ -22,11 +22,7 @@
 
     def address_breakpoints(self):
         """Test address breakpoints set with shared library of SBAddress work correctly."""
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint on main.c by name 'c'.
         breakpoint = target.BreakpointCreateBySourceRegex(
Index: lldb/test/API/functionalities/asan/TestReportData.py
===================================================================
--- lldb/test/API/functionalities/asan/TestReportData.py
+++ lldb/test/API/functionalities/asan/TestReportData.py
@@ -34,9 +34,7 @@
         self.col_crash = 16
 
     def asan_tests(self):
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         self.registerSanitizerLibrariesWithTarget(target)
 
Index: lldb/test/API/functionalities/asan/TestMemoryHistory.py
===================================================================
--- lldb/test/API/functionalities/asan/TestMemoryHistory.py
+++ lldb/test/API/functionalities/asan/TestMemoryHistory.py
@@ -31,9 +31,7 @@
         self.line_breakpoint = line_number('main.c', '// break line')
 
     def asan_tests(self):
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         self.registerSanitizerLibrariesWithTarget(target)
 
Index: lldb/test/API/commands/watchpoints/watchpoint_events/TestWatchpointEvents.py
===================================================================
--- lldb/test/API/commands/watchpoints/watchpoint_events/TestWatchpointEvents.py
+++ lldb/test/API/commands/watchpoints/watchpoint_events/TestWatchpointEvents.py
@@ -24,11 +24,7 @@
     def test_with_python_api(self):
         """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
         self.build()
-
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         self.main_source_spec = lldb.SBFileSpec(self.main_source)
 
Index: lldb/test/API/commands/watchpoints/watchpoint_disable/TestWatchpointDisable.py
===================================================================
--- lldb/test/API/commands/watchpoints/watchpoint_disable/TestWatchpointDisable.py
+++ lldb/test/API/commands/watchpoints/watchpoint_disable/TestWatchpointDisable.py
@@ -25,12 +25,9 @@
     def do_test(self, test_enable):
         """Set a watchpoint, disable it and make sure it doesn't get hit."""
 
-        exe = self.getBuildArtifact("a.out")
         main_file_spec = lldb.SBFileSpec("main.c")
 
-        # Create a target by the debugger.
-        self.target = self.dbg.CreateTarget(exe)
-        self.assertTrue(self.target, VALID_TARGET)
+        self.target = self.createTestTarget()
 
         bkpt_before = self.target.BreakpointCreateBySourceRegex("Set a breakpoint here", main_file_spec)
         self.assertEqual(bkpt_before.GetNumLocations(),  1, "Failed setting the before breakpoint.")
Index: lldb/test/API/commands/watchpoints/step_over_watchpoint/TestStepOverWatchpoint.py
===================================================================
--- lldb/test/API/commands/watchpoints/step_over_watchpoint/TestStepOverWatchpoint.py
+++ lldb/test/API/commands/watchpoints/step_over_watchpoint/TestStepOverWatchpoint.py
@@ -26,10 +26,7 @@
     def test(self):
         """Test stepping over watchpoints."""
         self.build()
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(self.target, VALID_TARGET)
+        target = self.createTestTarget()
 
         lldbutil.run_break_set_by_symbol(self, 'main')
 
Index: lldb/test/API/commands/watchpoints/multiple_hits/TestMultipleHits.py
===================================================================
--- lldb/test/API/commands/watchpoints/multiple_hits/TestMultipleHits.py
+++ lldb/test/API/commands/watchpoints/multiple_hits/TestMultipleHits.py
@@ -19,9 +19,7 @@
     @skipIfwatchOS
     def test(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target and target.IsValid(), VALID_TARGET)
+        target = self.createTestTarget()
 
         bp = target.BreakpointCreateByName("main")
         self.assertTrue(bp and bp.IsValid(), "Breakpoint is valid")
Index: lldb/test/API/commands/trace/multiple-threads/TestTraceStartStopMultipleThreads.py
===================================================================
--- lldb/test/API/commands/trace/multiple-threads/TestTraceStartStopMultipleThreads.py
+++ lldb/test/API/commands/trace/multiple-threads/TestTraceStartStopMultipleThreads.py
@@ -18,8 +18,7 @@
     @skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
     def testStartMultipleLiveThreads(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
 
         self.expect("b main")
         self.expect("b 6")
@@ -40,8 +39,7 @@
     @skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
     def testStartMultipleLiveThreadsWithStops(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
 
         self.expect("b main")
         self.expect("b 6")
Index: lldb/test/API/commands/register/register/register_command/TestRegisters.py
===================================================================
--- lldb/test/API/commands/register/register/register_command/TestRegisters.py
+++ lldb/test/API/commands/register/register/register_command/TestRegisters.py
@@ -197,11 +197,7 @@
     # lldb/test/Shell/Register/x86*-fp-read.test.
     @skipUnlessDarwin
     def fp_special_purpose_register_read(self):
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Launch the process and stop.
         self.expect("run", PROCESS_STOPPED, substrs=['stopped'])
@@ -278,11 +274,7 @@
                 1 << fstat_top_pointer_initial)
 
     def fp_register_write(self):
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Launch the process, stop at the entry point.
         error = lldb.SBError()
Index: lldb/test/API/commands/process/launch/TestProcessLaunch.py
===================================================================
--- lldb/test/API/commands/process/launch/TestProcessLaunch.py
+++ lldb/test/API/commands/process/launch/TestProcessLaunch.py
@@ -180,11 +180,10 @@
         d = {'CXX_SOURCES': source}
         self.build(dictionary=d)
         self.setTearDownCleanup(d)
-        exe = self.getBuildArtifact("a.out")
 
         evil_var = 'INIT*MIDDLE}TAIL'
 
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
         main_source_spec = lldb.SBFileSpec(source)
         breakpoint = target.BreakpointCreateBySourceRegex(
             '// Set breakpoint here.', main_source_spec)
Index: lldb/test/API/commands/process/launch-with-shellexpand/TestLaunchWithShellExpand.py
===================================================================
--- lldb/test/API/commands/process/launch-with-shellexpand/TestLaunchWithShellExpand.py
+++ lldb/test/API/commands/process/launch-with-shellexpand/TestLaunchWithShellExpand.py
@@ -25,12 +25,7 @@
     @expectedFailureNetBSD
     def test(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-
-        self.runCmd("target create %s" % exe)
-
-        # Create the target
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
 
         # Create any breakpoints we need
         breakpoint = target.BreakpointCreateBySourceRegex(
Index: lldb/test/API/commands/frame/var/TestFrameVar.py
===================================================================
--- lldb/test/API/commands/frame/var/TestFrameVar.py
+++ lldb/test/API/commands/frame/var/TestFrameVar.py
@@ -23,11 +23,7 @@
         self.do_test()
 
     def do_test(self):
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint in main.c at the source matching
         # "Set a breakpoint here"
Index: lldb/test/API/commands/frame/language/TestGuessLanguage.py
===================================================================
--- lldb/test/API/commands/frame/language/TestGuessLanguage.py
+++ lldb/test/API/commands/frame/language/TestGuessLanguage.py
@@ -34,11 +34,7 @@
 
     def do_test(self):
         """Test GuessLanguage for C & C++."""
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint in main.c at the source matching
         # "Set a breakpoint here"
Index: lldb/test/API/commands/expression/test/TestExprs.py
===================================================================
--- lldb/test/API/commands/expression/test/TestExprs.py
+++ lldb/test/API/commands/expression/test/TestExprs.py
@@ -80,11 +80,7 @@
     def test_evaluate_expression_python(self):
         """Test SBFrame.EvaluateExpression() API for evaluating an expression."""
         self.build()
-
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Create the breakpoint.
         filespec = lldb.SBFileSpec("main.cpp", False)
Index: lldb/test/API/commands/expression/no-deadlock/TestExprDoesntBlock.py
===================================================================
--- lldb/test/API/commands/expression/no-deadlock/TestExprDoesntBlock.py
+++ lldb/test/API/commands/expression/no-deadlock/TestExprDoesntBlock.py
@@ -20,11 +20,7 @@
     def test_with_run_command(self):
         """Test that expr will time out and allow other threads to run if it blocks."""
         self.build()
-        exe = self.getBuildArtifact("a.out")
-
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint at source line before call_me_to_get_lock
         # gets called.
Index: lldb/test/API/commands/expression/expr-in-syscall/TestExpressionInSyscall.py
===================================================================
--- lldb/test/API/commands/expression/expr-in-syscall/TestExpressionInSyscall.py
+++ lldb/test/API/commands/expression/expr-in-syscall/TestExpressionInSyscall.py
@@ -20,11 +20,8 @@
         self.expr_syscall()
 
     def expr_syscall(self):
-        exe = self.getBuildArtifact("a.out")
-
         # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         listener = lldb.SBListener("my listener")
 
Index: lldb/test/API/commands/expression/error-limit/TestExprErrorLimit.py
===================================================================
--- lldb/test/API/commands/expression/error-limit/TestExprErrorLimit.py
+++ lldb/test/API/commands/expression/error-limit/TestExprErrorLimit.py
@@ -17,7 +17,7 @@
         # FIXME: The only reason this test needs to create a real target is because
         # the settings of the dummy target can't be changed with `settings set`.
         self.build()
-        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+        target = self.createTestTarget()
 
         # Our test expression that is just several lines of malformed
         # integer literals (with a 'yerror' integer suffix). Every error
Index: lldb/test/API/commands/expression/completion/TestExprCompletion.py
===================================================================
--- lldb/test/API/commands/expression/completion/TestExprCompletion.py
+++ lldb/test/API/commands/expression/completion/TestExprCompletion.py
@@ -19,7 +19,7 @@
         self.build()
         self.main_source = "main.cpp"
         self.main_source_spec = lldb.SBFileSpec(self.main_source)
-        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+        self.createTestTarget()
 
         # Try the completion before we have a context to complete on.
         self.assume_no_completions('expr some_expr')
@@ -195,7 +195,7 @@
         self.build()
         self.main_source = "main.cpp"
         self.main_source_spec = lldb.SBFileSpec(self.main_source)
-        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+        self.createTestTarget()
 
         (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
                                           '// Break here', self.main_source_spec)
Index: lldb/test/API/commands/expression/completion-crash-invalid-iterator/TestInvalidIteratorCompletionCrash.py
===================================================================
--- lldb/test/API/commands/expression/completion-crash-invalid-iterator/TestInvalidIteratorCompletionCrash.py
+++ lldb/test/API/commands/expression/completion-crash-invalid-iterator/TestInvalidIteratorCompletionCrash.py
@@ -10,8 +10,7 @@
     @skipIf # rdar://problem/53931074
     def test(self):
         self.build()
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
         callee_break = target.BreakpointCreateByName(
             "SomeClass::SomeClass(ParamClass)", None)
         self.assertTrue(callee_break.GetNumLocations() > 0)
Index: lldb/test/API/commands/expression/call-function/TestCallBuiltinFunction.py
===================================================================
--- lldb/test/API/commands/expression/call-function/TestCallBuiltinFunction.py
+++ lldb/test/API/commands/expression/call-function/TestCallBuiltinFunction.py
@@ -20,7 +20,7 @@
     def test(self):
         self.build()
 
-        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+        target = self.createTestTarget()
 
         self.expect_expr("__builtin_isinf(0.0f)", result_type="int", result_value="0")
         self.expect_expr("__builtin_isnormal(0.0f)", result_type="int", result_value="0")
Index: lldb/test/API/commands/disassemble/basic/TestFrameDisassemble.py
===================================================================
--- lldb/test/API/commands/disassemble/basic/TestFrameDisassemble.py
+++ lldb/test/API/commands/disassemble/basic/TestFrameDisassemble.py
@@ -22,11 +22,8 @@
 
     def frame_disassemble_test(self):
         """Sample test to ensure SBFrame::Disassemble produces SOME output"""
-        exe = self.getBuildArtifact("a.out")
-
         # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        target = self.createTestTarget()
 
         # Now create a breakpoint in main.c at the source matching
         # "Set a breakpoint here"
Index: lldb/test/API/assert_messages_test/TestAssertMessages.py
===================================================================
--- lldb/test/API/assert_messages_test/TestAssertMessages.py
+++ lldb/test/API/assert_messages_test/TestAssertMessages.py
@@ -24,6 +24,15 @@
         else:
             self.fail("Initial expect should have raised AssertionError!")
 
+    def test_createTestTarget(self):
+        try:
+           self.createTestTarget("doesnt_exist")
+        except AssertionError as e:
+           self.assertIn("Couldn't create target for path 'doesnt_exist': "
+                         "error: unable to find executable for 'doesnt_exist'",
+                         str(e))
+
+
     def test_expect(self):
         """Test format of messages produced by expect(...)"""
 
Index: lldb/test/API/api/listeners/TestListener.py
===================================================================
--- lldb/test/API/api/listeners/TestListener.py
+++ lldb/test/API/api/listeners/TestListener.py
@@ -35,11 +35,9 @@
             lldb.SBTarget.GetBroadcasterClassName(),
             lldb.SBTarget.eBroadcastBitModulesUnloaded)
 
-        exe = self.getBuildArtifact("a.out")
-
         my_listener.Clear()
 
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
 
         bkpt = target.BreakpointCreateByName("main")
 
@@ -58,9 +56,7 @@
             lldb.SBTarget.GetBroadcasterClassName(),
             lldb.SBTarget.eBroadcastBitBreakpointChanged)
 
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
 
         bkpt = target.BreakpointCreateByName("main")
 
@@ -100,9 +96,7 @@
             lldb.SBTarget.GetBroadcasterClassName(),
             lldb.SBTarget.eBroadcastBitBreakpointChanged)
 
-        exe = self.getBuildArtifact("a.out")
-
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
         result = target.GetBroadcaster().AddListener(my_listener,
                                                      lldb.SBTarget.eBroadcastBitBreakpointChanged)
         self.assertEqual(result, lldb.SBTarget.eBroadcastBitBreakpointChanged,"Got our bit")
Index: lldb/test/API/android/platform/TestDefaultCacheLineSize.py
===================================================================
--- lldb/test/API/android/platform/TestDefaultCacheLineSize.py
+++ lldb/test/API/android/platform/TestDefaultCacheLineSize.py
@@ -17,8 +17,7 @@
     @skipUnlessTargetAndroid
     def test_cache_line_size(self):
         self.build(dictionary=self.getBuildFlags())
-        exe = self.getBuildArtifact("a.out")
-        target = self.dbg.CreateTarget(exe)
+        target = self.createTestTarget()
         self.assertTrue(target and target.IsValid(), "Target is valid")
 
         breakpoint = target.BreakpointCreateByName("main")
Index: lldb/packages/Python/lldbsuite/test/lldbtest.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/lldbtest.py
+++ lldb/packages/Python/lldbsuite/test/lldbtest.py
@@ -2654,6 +2654,31 @@
             self.fail(self._formatMessage(msg,
                 "'{}' is not success".format(error)))
 
+    def createTestTarget(self, file_path=None, msg=None):
+        """
+        Creates a target from the file found at the given file path.
+        Asserts that the resulting target is valid.
+        :param file_path: The file path that should be used to create the target.
+                          The default argument opens the current default test
+                          executable in the current test directory.
+        :param msg: A custom error message.
+        """
+        if file_path is None:
+            file_path = self.getBuildArtifact("a.out")
+        error = lldb.SBError()
+        triple = ""
+        platform = ""
+        load_dependent_modules = True
+        target = self.dbg.CreateTarget(file_path, triple, platform,
+                                       load_dependent_modules, error)
+        if error.Fail():
+            err = "Couldn't create target for path '{}': {}".format(file_path,
+                                                                    str(error))
+            self.fail(self._formatMessage(msg, err))
+
+        self.assertTrue(target.IsValid(), "Got invalid target without error")
+        return target
+
     # =================================================
     # Misc. helper methods for debugging test execution
     # =================================================
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to