Add a HideStdout() method to our TestBase class and call it from TestAbbreviations.py

and TestAliases.py.  Pass the keyword argument 'check=False' to:

    self.runCmd("script my.date()", check=False)

since we want to restore sys.stdout no matter what the outcome of the runCmd is.

llvm-svn: 129949
This commit is contained in:
Johnny Chen 2011-04-21 22:50:23 +00:00
parent d4257d847e
commit 9ee96e7b40
4 changed files with 34 additions and 13 deletions

View File

@ -59,12 +59,7 @@ class AbbreviationsTestCase(TestBase):
# We don't want to display the stdout if not in TraceOn() mode.
if not self.TraceOn():
old_stdout = sys.stdout
session = StringIO.StringIO()
sys.stdout = session
def restore_stdout():
sys.stdout = old_stdout
self.addTearDownHook(restore_stdout)
self.HideStdout()
self.runCmd (r'''sc print "\n\n\tHello!\n"''')

View File

@ -32,12 +32,7 @@ class AliasTestCase(TestBase):
# We don't want to display the stdout if not in TraceOn() mode.
if not self.TraceOn():
old_stdout = sys.stdout
session = StringIO.StringIO()
sys.stdout = session
def restore_stdout():
sys.stdout = old_stdout
self.addTearDownHook(restore_stdout)
self.HideStdout()
self.runCmd (r'''python print "\n\n\nWhoopee!\n\n\n"''')
# self.expect (r'''python print "\n\n\nWhoopee!\n\n\n"''',

View File

@ -27,7 +27,8 @@ class CommandSourceTestCase(TestBase):
sys.stdout = session
# Python should evaluate "my.date()" successfully.
self.runCmd("script my.date()")
# Pass 'check=False' so that sys.stdout gets restored unconditionally.
self.runCmd("script my.date()", check=False)
# Now restore stdout to the way we were. :-)
sys.stdout = old_stdout

View File

@ -566,6 +566,9 @@ class TestBase(unittest2.TestCase):
# function to be run during tearDown() time.
self.hooks = []
# See HideStdout(self).
self.sys_stdout_hidden = False
def markError(self):
"""Callback invoked when an error (unexpected exception) errored."""
self.__errored__ = True
@ -955,3 +958,30 @@ class TestBase(unittest2.TestCase):
def TraceOn(self):
"""Returns True if we are in trace mode (i.e., tracing lldb command execution)."""
return traceAlways
def HideStdout(self):
"""Hide output to stdout from the user.
During test execution, there might be cases where we don't want to show the
standard output to the user. For example,
self.runCmd(r'''sc print "\n\n\tHello!\n"''')
tests whether command abbreviation for 'script' works or not. There is no
need to show the 'Hello' output to the user as long as the 'script' command
succeeds and we are not in TraceOn() mode (see the '-t' option).
In this case, the test method calls self.HideStdout(self) to redirect the
sys.stdout to a null device, and restores the sys.stdout upon teardown.
Note that you should only call this method at most once during a test case
execution. Any subsequent call has no effect at all."""
if self.sys_stdout_hidden:
return
self.sys_stdout_hidden = True
old_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def restore_stdout():
sys.stdout = old_stdout
self.addTearDownHook(restore_stdout)