Skip to content

Commit

Permalink
Avoid dirtying the source tree in breakpoint command tests
Browse files Browse the repository at this point in the history
Summary:
The paralelization patch exposed a bunch of cases where we were still
touching the source tree (as these tests were now stepping on each
others toes and being flaky).

This patch removes such issues from breakpoint command tests. Since the
only reason they were creating files was to indirectly test whether the
breakpoint commands got executed (and plumbing the full build tree path
to all places that needed it would be messy) I decided to modify the
tests to check for a different side effect instead: modification of a
global variable. This also makes the code simpler as checking the value
of the global variable is easier, and there is nothing to clean up.

As the tests aren't really doing anything debug-info related, I took the
opportunity to also mark them as NO_DEBUG_INFO_TESTCASEs.

Reviewers: jingham, aprantl

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D43464

llvm-svn: 325570
  • Loading branch information
labath committed Feb 20, 2018
1 parent da4f43a commit a9f8b0d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import side_effect


class BreakpointCommandTestCase(TestBase):

NO_DEBUG_INFO_TESTCASE = True
mydir = TestBase.compute_mydir(__file__)

@classmethod
def classCleanup(cls):
"""Cleanup the test byproduct of breakpoint_command_sequence(self)."""
cls.RemoveTempFile("output.txt")
cls.RemoveTempFile("output2.txt")

@expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24528")
def test_breakpoint_command_sequence(self):
"""Test a sequence of breakpoint command add, list, and delete."""
Expand Down Expand Up @@ -71,7 +67,7 @@ def breakpoint_command_sequence(self):
self.runCmd(
"breakpoint command add -s command -o 'frame variable --show-types --scope' 1 4")
self.runCmd(
"breakpoint command add -s python -o 'here = open(\"output.txt\", \"w\"); here.write(\"lldb\\n\"); here.close()' 2")
"breakpoint command add -s python -o 'import side_effect; side_effect.one_liner = \"one liner was here\"' 2")
self.runCmd(
"breakpoint command add --python-function bktptcmd.function 3")

Expand Down Expand Up @@ -104,9 +100,8 @@ def breakpoint_command_sequence(self):
"frame variable --show-types --scope"])
self.expect("breakpoint command list 2", "Breakpoint 2 command ok",
substrs=["Breakpoint commands (Python):",
"here = open",
"here.write",
"here.close()"])
"import side_effect",
"side_effect.one_liner"])
self.expect("breakpoint command list 3", "Breakpoint 3 command ok",
substrs=["Breakpoint commands (Python):",
"bktptcmd.function(frame, bp_loc, internal_dict)"])
Expand Down Expand Up @@ -151,40 +146,14 @@ def breakpoint_command_sequence(self):
extra_options="-f a.c",
num_expected_locations=1)

# Run the program. Remove 'output.txt' if it exists.
self.RemoveTempFile("output.txt")
self.RemoveTempFile("output2.txt")
# Reset our canary variables and run the program.
side_effect.one_liner = None
side_effect.bktptcmd = None
self.runCmd("run", RUN_SUCCEEDED)

# Check that the file 'output.txt' exists and contains the string
# "lldb".

# The 'output.txt' file should now exist.
self.assertTrue(
os.path.isfile("output.txt"),
"'output.txt' exists due to breakpoint command for breakpoint 2.")
self.assertTrue(
os.path.isfile("output2.txt"),
"'output2.txt' exists due to breakpoint command for breakpoint 3.")

# Read the output file produced by running the program.
with open('output.txt', 'r') as f:
output = f.read()

self.expect(
output,
"File 'output.txt' and the content matches",
exe=False,
startstr="lldb")

with open('output2.txt', 'r') as f:
output = f.read()

self.expect(
output,
"File 'output2.txt' and the content matches",
exe=False,
startstr="lldb")
# Check the value of canary variables.
self.assertEquals("one liner was here", side_effect.one_liner)
self.assertEquals("function was here", side_effect.bktptcmd)

# Finish the program.
self.runCmd("process continue")
Expand Down Expand Up @@ -245,38 +214,16 @@ def breakpoint_command_script_parameters(self):
self, "main.c", self.line, num_expected_locations=1, loc_exact=True)

# Now add callbacks for the breakpoints just created.
self.runCmd("breakpoint command add -s python -o 'here = open(\"output-2.txt\", \"w\"); here.write(str(frame) + \"\\n\"); here.write(str(bp_loc) + \"\\n\"); here.close()' 1")

# Remove 'output-2.txt' if it already exists.
self.runCmd("breakpoint command add -s python -o 'import side_effect; side_effect.frame = str(frame); side_effect.bp_loc = str(bp_loc)' 1")

if (os.path.exists('output-2.txt')):
os.remove('output-2.txt')

# Run program, hit breakpoint, and hopefully write out new version of
# 'output-2.txt'
# Reset canary variables and run.
side_effect.frame = None
side_effect.bp_loc = None
self.runCmd("run", RUN_SUCCEEDED)

# Check that the file 'output.txt' exists and contains the string
# "lldb".

# The 'output-2.txt' file should now exist.
self.assertTrue(
os.path.isfile("output-2.txt"),
"'output-2.txt' exists due to breakpoint command for breakpoint 1.")

# Read the output file produced by running the program.
with open('output-2.txt', 'r') as f:
output = f.read()

self.expect(
output,
"File 'output-2.txt' and the content matches",
exe=False,
startstr="frame #0:",
patterns=["1.* where = .*main .* resolved, hit count = 1"])

# Now remove 'output-2.txt'
os.remove('output-2.txt')
self.expect(side_effect.frame, exe=False, startstr="frame #0:")
self.expect(side_effect.bp_loc, exe=False,
patterns=["1.* where = .*main .* resolved, hit count = 1"])

def breakpoint_commands_on_creation(self):
"""Test that setting breakpoint commands when creating the breakpoint works"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import side_effect


class PythonBreakpointCommandSettingTestCase(TestBase):

mydir = TestBase.compute_mydir(__file__)
my_var = 10
NO_DEBUG_INFO_TESTCASE = True

@add_test_categories(['pyapi'])
def test_step_out_python(self):
Expand Down Expand Up @@ -69,12 +70,9 @@ def do_set_python_command_from_python(self):
self.assertTrue(got_one_in_B, "Failed to match the pattern in B")
self.target.BreakpointDelete(no_files_bkpt.GetID())

PythonBreakpointCommandSettingTestCase.my_var = 10
error = lldb.SBError()
error = body_bkpt.SetScriptCallbackBody("\
import TestBreakpointCommandsFromPython\n\
TestBreakpointCommandsFromPython.PythonBreakpointCommandSettingTestCase.my_var = 20\n\
print('Hit breakpoint')")
error = body_bkpt.SetScriptCallbackBody(
"import side_effect; side_effect.callback = 'callback was here'")
self.assertTrue(
error.Success(),
"Failed to set the script callback body: %s." %
Expand All @@ -84,9 +82,9 @@ def do_set_python_command_from_python(self):
"command script import --allow-reload ./bktptcmd.py")
func_bkpt.SetScriptCallbackFunction("bktptcmd.function")

# We will use the function that touches a text file, so remove it
# first:
self.RemoveTempFile("output2.txt")
# Clear out canary variables
side_effect.bktptcmd = None
side_effect.callback = None

# Now launch the process, and do not stop at entry point.
self.process = self.target.LaunchSimple(
Expand All @@ -100,11 +98,5 @@ def do_set_python_command_from_python(self):
self.assertTrue(len(threads) == 1, "Stopped at inner breakpoint.")
self.thread = threads[0]

self.assertTrue(PythonBreakpointCommandSettingTestCase.my_var == 20)

# Check for the function version as well, which produced this file:
# Remember to clean up after ourselves...
self.assertTrue(
os.path.isfile("output2.txt"),
"'output2.txt' exists due to breakpoint command for breakpoint function.")
self.RemoveTempFile("output2.txt")
self.assertEquals("callback was here", side_effect.callback)
self.assertEquals("function was here", side_effect.bktptcmd)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import print_function

import side_effect

def function(frame, bp_loc, dict):
there = open("output2.txt", "w")
print("lldb", file=there)
there.close()
side_effect.bktptcmd = "function was here"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
A dummy module for testing the execution of various breakpoint commands. A
command will modify a global variable in this module and test will check its
value.
"""

0 comments on commit a9f8b0d

Please sign in to comment.