@@ -618,7 +618,7 @@ class Cmd(cmd.Cmd):
618
618
for editor in ['vim' , 'vi' , 'emacs' , 'nano' , 'pico' , 'gedit' , 'kate' , 'subl' , 'geany' , 'atom' ]:
619
619
if _which (editor ):
620
620
break
621
- feedback_to_output = False # Do include nonessentials in >, | output
621
+ feedback_to_output = True # Do include nonessentials in >, | output
622
622
locals_in_py = True
623
623
quiet = False # Do not suppress nonessential output
624
624
timing = False # Prints elapsed time for each command
@@ -1691,7 +1691,7 @@ def run_transcript_tests(self, callargs):
1691
1691
:param callargs: List[str] - list of transcript test file names
1692
1692
"""
1693
1693
class TestMyAppCase (Cmd2TestCase ):
1694
- CmdApp = self . __class__
1694
+ cmdapp = self
1695
1695
1696
1696
self .__class__ .testfiles = callargs
1697
1697
sys .argv = [sys .argv [0 ]] # the --test argument upsets unittest.main()
@@ -1731,12 +1731,12 @@ def cmdloop(self, intro=None):
1731
1731
if callopts .test :
1732
1732
self ._transcript_files = callargs
1733
1733
1734
+ # Always run the preloop first
1735
+ self .preloop ()
1736
+
1734
1737
if self ._transcript_files is not None :
1735
1738
self .run_transcript_tests (self ._transcript_files )
1736
1739
else :
1737
- # Always run the preloop first
1738
- self .preloop ()
1739
-
1740
1740
# If an intro was supplied in the method call, allow it to override the default
1741
1741
if intro is not None :
1742
1742
self .intro = intro
@@ -1754,8 +1754,8 @@ def cmdloop(self, intro=None):
1754
1754
if not stop :
1755
1755
self ._cmdloop ()
1756
1756
1757
- # Run the postloop() no matter what
1758
- self .postloop ()
1757
+ # Run the postloop() no matter what
1758
+ self .postloop ()
1759
1759
1760
1760
1761
1761
class HistoryItem (str ):
@@ -1960,25 +1960,11 @@ def restore(self):
1960
1960
setattr (self .obj , attrib , getattr (self , attrib ))
1961
1961
1962
1962
1963
- class Borg (object ):
1964
- """All instances of any Borg subclass will share state.
1965
- from Python Cookbook, 2nd Ed., recipe 6.16"""
1966
- _shared_state = {}
1967
-
1968
- def __new__ (cls , * a , ** k ):
1969
- obj = object .__new__ (cls )
1970
- obj .__dict__ = cls ._shared_state
1971
- return obj
1972
-
1973
-
1974
- class OutputTrap (Borg ):
1975
- """Instantiate an OutputTrap to divert/capture ALL stdout output. For use in unit testing.
1976
- Call `tearDown()` to return to normal output."""
1963
+ class OutputTrap (object ):
1964
+ """Instantiate an OutputTrap to divert/capture ALL stdout output. For use in transcript testing."""
1977
1965
1978
1966
def __init__ (self ):
1979
1967
self .contents = ''
1980
- self .old_stdout = sys .stdout
1981
- sys .stdout = self
1982
1968
1983
1969
def write (self , txt ):
1984
1970
"""Add text to the internal contents.
@@ -1996,17 +1982,12 @@ def read(self):
1996
1982
self .contents = ''
1997
1983
return result
1998
1984
1999
- def tear_down (self ):
2000
- """Restores normal output."""
2001
- sys .stdout = self .old_stdout
2002
- self .contents = ''
2003
-
2004
1985
2005
1986
class Cmd2TestCase (unittest .TestCase ):
2006
1987
"""Subclass this, setting CmdApp, to make a unittest.TestCase class
2007
1988
that will execute the commands in a transcript file and expect the results shown.
2008
1989
See example.py"""
2009
- CmdApp = None
1990
+ cmdapp = None
2010
1991
regexPattern = pyparsing .QuotedString (quoteChar = r'/' , escChar = '\\ ' , multiline = True , unquoteResults = True )
2011
1992
regexPattern .ignore (pyparsing .cStyleComment )
2012
1993
notRegexPattern = pyparsing .Word (pyparsing .printables )
@@ -2016,7 +1997,7 @@ class Cmd2TestCase(unittest.TestCase):
2016
1997
2017
1998
def fetchTranscripts (self ):
2018
1999
self .transcripts = {}
2019
- for fileset in self .CmdApp .testfiles :
2000
+ for fileset in self .cmdapp .testfiles :
2020
2001
for fname in glob .glob (fileset ):
2021
2002
tfile = open (fname )
2022
2003
self .transcripts [fname ] = iter (tfile .readlines ())
@@ -2025,17 +2006,15 @@ def fetchTranscripts(self):
2025
2006
raise Exception ("No test files found - nothing to test." )
2026
2007
2027
2008
def setUp (self ):
2028
- if self .CmdApp :
2029
- self .outputTrap = OutputTrap ()
2030
- self .cmdapp = self .CmdApp ()
2009
+ if self .cmdapp :
2031
2010
self .fetchTranscripts ()
2032
2011
2033
- # Make sure any required initialization gets done and flush the output buffer
2034
- self .cmdapp .preloop ()
2035
- self .outputTrap . read ()
2012
+ # Trap stdout
2013
+ self ._orig_stdout = self . cmdapp .stdout
2014
+ self .cmdapp . stdout = OutputTrap ()
2036
2015
2037
2016
def runTest (self ): # was testall
2038
- if self .CmdApp :
2017
+ if self .cmdapp :
2039
2018
its = sorted (self .transcripts .items ())
2040
2019
for (fname , transcript ) in its :
2041
2020
self ._test_transcript (fname , transcript )
@@ -2071,7 +2050,7 @@ def _test_transcript(self, fname, transcript):
2071
2050
# Send the command into the application and capture the resulting output
2072
2051
# TODO: Should we get the return value and act if stop == True?
2073
2052
self .cmdapp .onecmd_plus_hooks (command )
2074
- result = self .outputTrap .read ()
2053
+ result = self .cmdapp . stdout .read ()
2075
2054
# Read the expected result from transcript
2076
2055
if line .startswith (self .cmdapp .prompt ):
2077
2056
message = '\n File %s, line %d\n Command was:\n %r\n Expected: (nothing)\n Got:\n %r\n ' % \
@@ -2098,11 +2077,9 @@ def _test_transcript(self, fname, transcript):
2098
2077
self .assertTrue (re .match (expected , result , re .MULTILINE | re .DOTALL ), message )
2099
2078
2100
2079
def tearDown (self ):
2101
- if self .CmdApp :
2102
- # Make sure any required cleanup gets done
2103
- self .cmdapp .postloop ()
2104
-
2105
- self .outputTrap .tear_down ()
2080
+ if self .cmdapp :
2081
+ # Restore stdout
2082
+ self .cmdapp .stdout = self ._orig_stdout
2106
2083
2107
2084
2108
2085
def namedtuple_with_two_defaults (typename , field_names , default_values = ('' , '' )):
0 commit comments