Skip to content

Commit

Permalink
Next batch of test-tree-cleaning changes
Browse files Browse the repository at this point in the history
Summary:
The changes here fall into several categories.

- some tests were redirecting inferior stdout/err to a file. For these I
  make sure we use an absolute path for the file. I also create a
  lldbutil.read_file_on_target helper function to encapsulate the
  differences between reading a file locally and remotely.
- some tests were redirecting the pexpect I/O into a file. For these I
  use a python StringIO object to avoid creating a file altogether.
- the TestSettings inferior was creating a file. Here, I make sure the
  inferior is launched with pwd=build-dir so that the files end up
  created there.
- lldb-mi --log (used by some tests) creates a log file in PWD without
  the ability say differently. To make this work I make sure to run
  lldb-mi with PWD=build_dir. This in turn necessitated a couple of
  changes in other lldb-mi tests, which were using relative paths to
  access the source tree.

Reviewers: aprantl

Subscribers: ki.stfu, mehdi_amini, lldb-commits

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

llvm-svn: 327625
  • Loading branch information
labath committed Mar 15, 2018
1 parent dfc7eb4 commit 107052f
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class ProcessLaunchTestCase(TestBase):

mydir = TestBase.compute_mydir(__file__)
NO_DEBUG_INFO_TESTCASE = True

def setUp(self):
# Call super's setUp().
Expand All @@ -38,8 +39,8 @@ def test_io(self):
patterns=["Current executable set to .*a.out"])

in_file = "input-file.txt"
out_file = "output-test.out"
err_file = "output-test.err"
out_file = lldbutil.append_to_process_working_directory(self, "output-test.out")
err_file = lldbutil.append_to_process_working_directory(self, "output-test.err")

# Make sure the output files do not exist before launching the process
try:
Expand All @@ -52,8 +53,8 @@ def test_io(self):
except OSError:
pass

launch_command = "process launch -i " + \
in_file + " -o " + out_file + " -e " + err_file
launch_command = "process launch -i '{0}' -o '{1}' -e '{2}' -w '{3}'".format(
in_file, out_file, err_file, self.get_process_working_directory())

if lldb.remote_platform:
self.runCmd('platform put-file "{local}" "{remote}"'.format(
Expand All @@ -62,55 +63,19 @@ def test_io(self):
self.expect(launch_command,
patterns=["Process .* launched: .*a.out"])

if lldb.remote_platform:
self.runCmd('platform get-file "{remote}" "{local}"'.format(
remote=out_file, local=out_file))
self.runCmd('platform get-file "{remote}" "{local}"'.format(
remote=err_file, local=err_file))

success = True
err_msg = ""

# Check to see if the 'stdout' file was created
try:
out_f = open(out_file)
except IOError:
out = lldbutil.read_file_on_target(self, out_file)
if out != "This should go to stdout.\n":
success = False
err_msg = err_msg + " ERROR: stdout file was not created.\n"
else:
# Check to see if the 'stdout' file contains the right output
line = out_f.readline()
if line != "This should go to stdout.\n":
success = False
err_msg = err_msg + " ERROR: stdout file does not contain correct output.\n"
out_f.close()
err_msg = err_msg + " ERROR: stdout file does not contain correct output.\n"

# Try to delete the 'stdout' file
try:
os.remove(out_file)
except OSError:
pass

# Check to see if the 'stderr' file was created
try:
err_f = open(err_file)
except IOError:
err = lldbutil.read_file_on_target(self, err_file)
if err != "This should go to stderr.\n":
success = False
err_msg = err_msg + " ERROR: stderr file was not created.\n"
else:
# Check to see if the 'stderr' file contains the right output
line = err_f.readline()
if line != "This should go to stderr.\n":
success = False
err_msg = err_msg + " ERROR: stderr file does not contain correct output.\n\
"
err_f.close()

# Try to delete the 'stderr' file
try:
os.remove(err_file)
except OSError:
pass
err_msg = err_msg + " ERROR: stderr file does not contain correct output.\n"

if not success:
self.fail(err_msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil

import six

class SingleQuoteInCommandLineTestCase(TestBase):

Expand Down Expand Up @@ -50,32 +50,24 @@ def test_lldb_invocation_with_single_quote_in_filename(self):
self.getBuildArtifact(self.myexe)))
child = self.child
child.setecho(True)
# Turn on logging for input/output to/from the child.
with open('child_send.txt', 'w') as f_send:
with open('child_read.txt', 'w') as f_read:
child.logfile_send = f_send
child.logfile_read = f_read

child.expect_exact(prompt)
child.logfile_send = send = six.StringIO()
child.logfile_read = read = six.StringIO()
child.expect_exact(prompt)

child.send("help watchpoint")
child.sendline('')
child.expect_exact(prompt)
child.send("help watchpoint")
child.sendline('')
child.expect_exact(prompt)

# Now that the necessary logging is done, restore logfile to None to
# stop further logging.
child.logfile_send = None
child.logfile_read = None

with open('child_send.txt', 'r') as fs:
if self.TraceOn():
print("\n\nContents of child_send.txt:")
print(fs.read())
with open('child_read.txt', 'r') as fr:
from_child = fr.read()
if self.TraceOn():
print("\n\nContents of child_read.txt:")
print(from_child)
if self.TraceOn():
print("\n\nContents of send")
print(send.getvalue())
print("\n\nContents of read")
print(read.getvalue())

self.expect(from_child, exe=False,
substrs=["Current executable set to"])
self.expect(read.getvalue(), exe=False,
substrs=["Current executable set to"])
22 changes: 16 additions & 6 deletions lldb/packages/Python/lldbsuite/test/lldbutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,21 @@ def find_library_callable(test):
target))


def read_file_on_target(test, remote):
if lldb.remote_platform:
local = test.getBuildArtifact("file_from_target")
error = lldb.remote_platform.Get(lldb.SBFileSpec(remote, False),
lldb.SBFileSpec(local, True))
test.assertTrue(error.Success(), "Reading file {0} failed: {1}".format(remote, error))
else:
local = remote
with open(local, 'r') as f:
return f.read()

def read_file_from_process_wd(test, name):
path = append_to_process_working_directory(test, name)
return read_file_on_target(test, path)

def wait_for_file_on_target(testcase, file_path, max_attempts=6):
for i in range(max_attempts):
err, retcode, msg = testcase.run_platform_command("ls %s" % file_path)
Expand All @@ -1335,9 +1350,4 @@ def wait_for_file_on_target(testcase, file_path, max_attempts=6):
"File %s not found even after %d attempts." %
(file_path, max_attempts))

err, retcode, data = testcase.run_platform_command("cat %s" % (file_path))

testcase.assertTrue(
err.Success() and retcode == 0, "Failed to read file %s: %s, retcode: %d" %
(file_path, err.GetCString(), retcode))
return data
return read_file_on_target(testcase, file_path)
74 changes: 19 additions & 55 deletions lldb/packages/Python/lldbsuite/test/settings/TestSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
class SettingsCommandTestCase(TestBase):

mydir = TestBase.compute_mydir(__file__)
NO_DEBUG_INFO_TESTCASE = True

@classmethod
def classCleanup(cls):
"""Cleanup the test byproducts."""
cls.RemoveTempFile("output1.txt")
cls.RemoveTempFile("output2.txt")
cls.RemoveTempFile("stderr.txt")
cls.RemoveTempFile("stdout.txt")

@no_debug_info_test
def test_apropos_should_also_search_settings_description(self):
"""Test that 'apropos' command should also search descriptions for the settings variables."""

Expand All @@ -35,7 +27,6 @@ def test_apropos_should_also_search_settings_description(self):
"environment variables",
"executable's environment"])

@no_debug_info_test
def test_append_target_env_vars(self):
"""Test that 'append target.run-args' works."""
# Append the env-vars.
Expand All @@ -48,7 +39,6 @@ def test_append_target_env_vars(self):
self.expect('settings show target.env-vars',
substrs=['MY_ENV_VAR=YES'])

@no_debug_info_test
def test_insert_before_and_after_target_run_args(self):
"""Test that 'insert-before/after target.run-args' works."""
# Set the run-args first.
Expand All @@ -70,7 +60,6 @@ def test_insert_before_and_after_target_run_args(self):
'[3]: "b"',
'[4]: "c"'])

@no_debug_info_test
def test_replace_target_run_args(self):
"""Test that 'replace target.run-args' works."""
# Set the run-args and then replace the index-0 element.
Expand All @@ -88,7 +77,6 @@ def test_replace_target_run_args(self):
'[1]: "b"',
'[2]: "c"'])

@no_debug_info_test
def test_set_prompt(self):
"""Test that 'set prompt' actually changes the prompt."""

Expand All @@ -106,7 +94,6 @@ def test_set_prompt(self):
# Use '-r' option to reset to the original default prompt.
self.runCmd("settings clear prompt")

@no_debug_info_test
def test_set_term_width(self):
"""Test that 'set term-width' actually changes the term-width."""

Expand Down Expand Up @@ -153,7 +140,8 @@ def cleanup():
substrs=[format_string])

self.runCmd("breakpoint set -n main")
self.runCmd("run")
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)
self.expect("thread backtrace",
substrs=["`main", self.getSourceDir()])

Expand Down Expand Up @@ -231,13 +219,11 @@ def test_run_args_and_env_vars(self):
self.addTearDownHook(
lambda: self.runCmd("settings clear target.env-vars"))

self.runCmd("run", RUN_SUCCEEDED)
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)

# Read the output file produced by running the program.
if lldb.remote_platform:
self.runCmd('platform get-file "output2.txt" "output2.txt"')
with open('output2.txt', 'r') as f:
output = f.read()
output = lldbutil.read_file_from_process_wd(self, "output2.txt")

self.expect(
output,
Expand Down Expand Up @@ -272,13 +258,11 @@ def unset_env_variables():
os.environ.pop("MY_HOST_ENV_VAR2")

self.addTearDownHook(unset_env_variables)
self.runCmd("run", RUN_SUCCEEDED)
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)

# Read the output file produced by running the program.
if lldb.remote_platform:
self.runCmd('platform get-file "output1.txt" "output1.txt"')
with open('output1.txt', 'r') as f:
output = f.read()
output = lldbutil.read_file_from_process_wd(self, "output1.txt")

self.expect(
output,
Expand All @@ -296,8 +280,10 @@ def test_set_error_output_path(self):
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

# Set the error-path and output-path and verify both are set.
self.runCmd("settings set target.error-path stderr.txt")
self.runCmd("settings set target.output-path stdout.txt")
self.runCmd("settings set target.error-path '{0}'".format(
lldbutil.append_to_process_working_directory(self, "stderr.txt")))
self.runCmd("settings set target.output-path '{0}".format(
lldbutil.append_to_process_working_directory(self, "stdout.txt")))
# And add hooks to restore the original settings during tearDown().
self.addTearDownHook(
lambda: self.runCmd("settings clear target.output-path"))
Expand All @@ -306,60 +292,40 @@ def test_set_error_output_path(self):

self.expect("settings show target.error-path",
SETTING_MSG("target.error-path"),
substrs=['target.error-path (file) = "stderr.txt"'])
substrs=['target.error-path (file)', 'stderr.txt"'])

self.expect("settings show target.output-path",
SETTING_MSG("target.output-path"),
substrs=['target.output-path (file) = "stdout.txt"'])

self.runCmd("run", RUN_SUCCEEDED)

if lldb.remote_platform:
self.runCmd('platform get-file "stderr.txt" "stderr.txt"')
self.runCmd('platform get-file "stdout.txt" "stdout.txt"')

# The 'stderr.txt' file should now exist.
self.assertTrue(os.path.isfile("stderr.txt"),
"'stderr.txt' exists due to target.error-path.")
substrs=['target.output-path (file)', 'stdout.txt"'])

# Read the output file produced by running the program.
with open('stderr.txt', 'r') as f:
output = f.read()
self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
RUN_SUCCEEDED)

output = lldbutil.read_file_from_process_wd(self, "stderr.txt")
message = "This message should go to standard error."
if lldbplatformutil.hasChattyStderr(self):
self.expect(output, exe=False, substrs=[message])
else:
self.expect(output, exe=False, startstr=message)

# The 'stdout.txt' file should now exist.
self.assertTrue(os.path.isfile("stdout.txt"),
"'stdout.txt' exists due to target.output-path.")

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

output = lldbutil.read_file_from_process_wd(self, "stdout.txt")
self.expect(output, exe=False,
startstr="This message should go to standard out.")

@no_debug_info_test
def test_print_dictionary_setting(self):
self.runCmd("settings clear target.env-vars")
self.runCmd("settings set target.env-vars [\"MY_VAR\"]=some-value")
self.expect("settings show target.env-vars",
substrs=["MY_VAR=some-value"])
self.runCmd("settings clear target.env-vars")

@no_debug_info_test
def test_print_array_setting(self):
self.runCmd("settings clear target.run-args")
self.runCmd("settings set target.run-args gobbledy-gook")
self.expect("settings show target.run-args",
substrs=['[0]: "gobbledy-gook"'])
self.runCmd("settings clear target.run-args")

@no_debug_info_test
def test_settings_with_quotes(self):
self.runCmd("settings clear target.run-args")
self.runCmd("settings set target.run-args a b c")
Expand Down Expand Up @@ -392,7 +358,6 @@ def test_settings_with_quotes(self):
'thread-format (format-string) = "abc def "')
self.runCmd('settings clear thread-format')

@no_debug_info_test
def test_settings_with_trailing_whitespace(self):

# boolean
Expand Down Expand Up @@ -517,7 +482,6 @@ def test_settings_with_trailing_whitespace(self):
substrs=['disassembly-format (format-string) = "foo "'])
self.runCmd("settings clear disassembly-format", check=False)

@no_debug_info_test
def test_all_settings_exist(self):
self.expect("settings show",
substrs=["auto-confirm",
Expand Down

0 comments on commit 107052f

Please sign in to comment.