diff cmd2/cmd2.py @ 105:130340609e19

testing beginning to work?
author catherine@dellzilla
date Fri, 24 Oct 2008 14:32:23 -0400
parents bd91925d813c
children 2d3232693807
line wrap: on
line diff
--- a/cmd2/cmd2.py	Fri Oct 24 13:29:22 2008 -0400
+++ b/cmd2/cmd2.py	Fri Oct 24 14:32:23 2008 -0400
@@ -688,19 +688,15 @@
 class OutputTrap(Borg):
     '''Instantiate an OutputTrap to divert/capture ALL stdout output.  For use in unit testing.
     Call `tearDown()` to return to normal output.'''
-    old_stdout = sys.stdout
     def __init__(self):
-        #self.old_stdout = sys.stdout
+        self.old_stdout = sys.stdout
         self.trap = tempfile.TemporaryFile()
         sys.stdout = self.trap
-    def dump(self):
-        'Reads trapped stdout output.'
+    def read(self):
         self.trap.seek(0)
         result = self.trap.read()
-        self.trap.close()
-        self.trap = tempfile.TemporaryFile()
-        sys.stdout = self.trap
-        return result
+        self.trap.truncate(0)
+        return result.strip('\x00')        
     def tearDown(self):
         sys.stdout = self.old_stdout
 
@@ -744,14 +740,14 @@
     transcriptFileName = ''
     def setUp(self):
         if self.CmdApp:
+            self.outputTrap = OutputTrap()
             self.cmdapp = self.CmdApp()
-            self.outputTrap = OutputTrap()
             self.transcriptReader = TranscriptReader(self.cmdapp, self.transcriptFileName)
     def testall(self):
         if self.CmdApp:            
             for (cmdInput, lineNum) in self.transcriptReader.inputGenerator():
                 self.cmdapp.onecmd(cmdInput)
-                result = self.outputTrap.dump()
+                result = self.outputTrap.read()
                 expected = self.transcriptReader.nextExpected()
                 self.assertEqual(result.strip(), expected.strip(), 
                     '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' %