[Lldb-commits] [lldb] r129949 - in /lldb/trunk/test: abbreviation_tests/TestAbbreviations.py alias_tests/TestAliases.py command_source/TestCommandSource.py lldbtest.py

Johnny Chen johnny.chen at apple.com
Thu Apr 21 15:50:23 PDT 2011


Author: johnny
Date: Thu Apr 21 17:50:23 2011
New Revision: 129949

URL: http://llvm.org/viewvc/llvm-project?rev=129949&view=rev
Log:
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.

Modified:
    lldb/trunk/test/abbreviation_tests/TestAbbreviations.py
    lldb/trunk/test/alias_tests/TestAliases.py
    lldb/trunk/test/command_source/TestCommandSource.py
    lldb/trunk/test/lldbtest.py

Modified: lldb/trunk/test/abbreviation_tests/TestAbbreviations.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/abbreviation_tests/TestAbbreviations.py?rev=129949&r1=129948&r2=129949&view=diff
==============================================================================
--- lldb/trunk/test/abbreviation_tests/TestAbbreviations.py (original)
+++ lldb/trunk/test/abbreviation_tests/TestAbbreviations.py Thu Apr 21 17:50:23 2011
@@ -59,12 +59,7 @@
 
         # 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"''')
 

Modified: lldb/trunk/test/alias_tests/TestAliases.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/alias_tests/TestAliases.py?rev=129949&r1=129948&r2=129949&view=diff
==============================================================================
--- lldb/trunk/test/alias_tests/TestAliases.py (original)
+++ lldb/trunk/test/alias_tests/TestAliases.py Thu Apr 21 17:50:23 2011
@@ -32,12 +32,7 @@
 
         # 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"''',

Modified: lldb/trunk/test/command_source/TestCommandSource.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/command_source/TestCommandSource.py?rev=129949&r1=129948&r2=129949&view=diff
==============================================================================
--- lldb/trunk/test/command_source/TestCommandSource.py (original)
+++ lldb/trunk/test/command_source/TestCommandSource.py Thu Apr 21 17:50:23 2011
@@ -27,7 +27,8 @@
         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

Modified: lldb/trunk/test/lldbtest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbtest.py?rev=129949&r1=129948&r2=129949&view=diff
==============================================================================
--- lldb/trunk/test/lldbtest.py (original)
+++ lldb/trunk/test/lldbtest.py Thu Apr 21 17:50:23 2011
@@ -566,6 +566,9 @@
         # 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 @@
     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)





More information about the lldb-commits mailing list