1010import sys
1111import re
1212import random
13+ import tempfile
1314
1415from unittest import mock
1516import pytest
1617
1718from cmd2 import cmd2
18- from .conftest import run_cmd , StdOut , normalize
19+ from .conftest import run_cmd , StdOut
1920
2021class CmdLineApp (cmd2 .Cmd ):
2122
@@ -26,7 +27,6 @@ class CmdLineApp(cmd2.Cmd):
2627 def __init__ (self , * args , ** kwargs ):
2728 self .multiline_commands = ['orate' ]
2829 self .maxrepeats = 3
29- self .redirector = '->'
3030
3131 # Add stuff to settable and/or shortcuts before calling base class initializer
3232 self .settable ['maxrepeats' ] = 'Max number of `--repeat`s allowed'
@@ -63,7 +63,7 @@ def do_speak(self, opts, arg):
6363 def do_mumble (self , opts , arg ):
6464 """Mumbles what you tell me to."""
6565 repetitions = opts .repeat or 1
66- arg = arg .split ()
66+ # arg = arg.split()
6767 for i in range (min (repetitions , self .maxrepeats )):
6868 output = []
6969 if random .random () < .33 :
@@ -77,136 +77,6 @@ def do_mumble(self, opts, arg):
7777 self .poutput (' ' .join (output ))
7878
7979
80- class DemoApp (cmd2 .Cmd ):
81- hello_parser = argparse .ArgumentParser ()
82- hello_parser .add_argument ('-n' , '--name' , help = "your name" )
83- @cmd2 .with_argparser_and_unknown_args (hello_parser )
84- def do_hello (self , opts , arg ):
85- """Says hello."""
86- if opts .name :
87- self .stdout .write ('Hello {}\n ' .format (opts .name ))
88- else :
89- self .stdout .write ('Hello Nobody\n ' )
90-
91-
92- @pytest .fixture
93- def _cmdline_app ():
94- c = CmdLineApp ()
95- c .stdout = StdOut ()
96- return c
97-
98-
99- @pytest .fixture
100- def _demo_app ():
101- c = DemoApp ()
102- c .stdout = StdOut ()
103- return c
104-
105-
106- def _get_transcript_blocks (transcript ):
107- cmd = None
108- expected = ''
109- for line in transcript .splitlines ():
110- if line .startswith ('(Cmd) ' ):
111- if cmd is not None :
112- yield cmd , normalize (expected )
113-
114- cmd = line [6 :]
115- expected = ''
116- else :
117- expected += line + '\n '
118- yield cmd , normalize (expected )
119-
120-
121- def test_base_with_transcript (_cmdline_app ):
122- app = _cmdline_app
123- transcript = """
124- (Cmd) help
125-
126- Documented commands (type help <topic>):
127- ========================================
128- alias help load orate pyscript say shell speak
129- edit history mumble py quit set shortcuts unalias
130-
131- (Cmd) help say
132- usage: speak [-h] [-p] [-s] [-r REPEAT]
133-
134- Repeats what you tell me to.
135-
136- optional arguments:
137- -h, --help show this help message and exit
138- -p, --piglatin atinLay
139- -s, --shout N00B EMULATION MODE
140- -r REPEAT, --repeat REPEAT
141- output [n] times
142-
143- (Cmd) say goodnight, Gracie
144- goodnight, Gracie
145- (Cmd) say -ps --repeat=5 goodnight, Gracie
146- OODNIGHT, GRACIEGAY
147- OODNIGHT, GRACIEGAY
148- OODNIGHT, GRACIEGAY
149- (Cmd) set maxrepeats 5
150- maxrepeats - was: 3
151- now: 5
152- (Cmd) say -ps --repeat=5 goodnight, Gracie
153- OODNIGHT, GRACIEGAY
154- OODNIGHT, GRACIEGAY
155- OODNIGHT, GRACIEGAY
156- OODNIGHT, GRACIEGAY
157- OODNIGHT, GRACIEGAY
158- (Cmd) history
159- -------------------------[1]
160- help
161- -------------------------[2]
162- help say
163- -------------------------[3]
164- say goodnight, Gracie
165- -------------------------[4]
166- say -ps --repeat=5 goodnight, Gracie
167- -------------------------[5]
168- set maxrepeats 5
169- -------------------------[6]
170- say -ps --repeat=5 goodnight, Gracie
171- (Cmd) history -r 4
172- OODNIGHT, GRACIEGAY
173- OODNIGHT, GRACIEGAY
174- OODNIGHT, GRACIEGAY
175- OODNIGHT, GRACIEGAY
176- OODNIGHT, GRACIEGAY
177- (Cmd) set prompt "---> "
178- prompt - was: (Cmd)
179- now: --->
180- """
181-
182- for cmd , expected in _get_transcript_blocks (transcript ):
183- out = run_cmd (app , cmd )
184- assert out == expected
185-
186-
187- class TestMyAppCase (cmd2 .Cmd2TestCase ):
188- CmdApp = CmdLineApp
189- CmdApp .testfiles = ['tests/transcript.txt' ]
190-
191-
192- def test_comment_stripping (_cmdline_app ):
193- out = run_cmd (_cmdline_app , 'speak it was /* not */ delicious! # Yuck!' )
194- expected = normalize ("""it was delicious!""" )
195- assert out == expected
196-
197-
198- def test_argparser_correct_args_with_quotes_and_midline_options (_cmdline_app ):
199- out = run_cmd (_cmdline_app , "speak 'This is a' -s test of the emergency broadcast system!" )
200- expected = normalize ("""THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM!""" )
201- assert out == expected
202-
203-
204- def test_argparser_options_with_spaces_in_quotes (_demo_app ):
205- out = run_cmd (_demo_app , "hello foo -n 'Bugs Bunny' bar baz" )
206- expected = normalize ("""Hello Bugs Bunny""" )
207- assert out == expected
208-
209-
21080def test_commands_at_invocation ():
21181 testargs = ["prog" , "say hello" , "say Gracie" , "quit" ]
21282 expected = "This is an intro banner ...\n hello\n Gracie\n "
@@ -217,27 +87,20 @@ def test_commands_at_invocation():
21787 out = app .stdout .buffer
21888 assert out == expected
21989
220- def test_invalid_syntax (_cmdline_app , capsys ):
221- run_cmd (_cmdline_app , 'speak "' )
222- out , err = capsys .readouterr ()
223- expected = normalize ("""ERROR: Invalid syntax: No closing quotation""" )
224- assert normalize (str (err )) == expected
225-
226-
227- @pytest .mark .parametrize ('filename, feedback_to_output' , [
90+ @pytest .mark .parametrize ('filename,feedback_to_output' , [
22891 ('bol_eol.txt' , False ),
22992 ('characterclass.txt' , False ),
23093 ('dotstar.txt' , False ),
23194 ('extension_notation.txt' , False ),
232- # ('from_cmdloop.txt', True),
95+ ('from_cmdloop.txt' , True ),
23396 ('multiline_no_regex.txt' , False ),
23497 ('multiline_regex.txt' , False ),
23598 ('regex_set.txt' , False ),
23699 ('singleslash.txt' , False ),
237100 ('slashes_escaped.txt' , False ),
238101 ('slashslash.txt' , False ),
239102 ('spaces.txt' , False ),
240- # ('word_boundaries.txt', False),
103+ ('word_boundaries.txt' , False ),
241104 ])
242105def test_transcript (request , capsys , filename , feedback_to_output ):
243106 # Create a cmd2.Cmd() instance and make sure basic settings are
@@ -263,6 +126,32 @@ def test_transcript(request, capsys, filename, feedback_to_output):
263126 assert err .startswith (expected_start )
264127 assert err .endswith (expected_end )
265128
129+ def test_history_transcript (request , capsys ):
130+ app = CmdLineApp ()
131+ app .stdout = StdOut ()
132+ run_cmd (app , 'orate this is\n a /multiline/\n command;\n ' )
133+ run_cmd (app , 'speak /tmp/file.txt is not a regex' )
134+
135+ expected = r"""(Cmd) orate this is
136+ > a /multiline/
137+ > command;
138+ this is a \/multiline\/ command
139+ (Cmd) speak /tmp/file.txt is not a regex
140+ \/tmp\/file.txt is not a regex
141+ """
142+
143+ # make a tmp file
144+ fd , history_fname = tempfile .mkstemp (prefix = '' , suffix = '.txt' )
145+ os .close (fd )
146+
147+ # tell the history command to create a transcript
148+ run_cmd (app , 'history -t "{}"' .format (history_fname ))
149+
150+ # read in the transcript created by the history command
151+ with open (history_fname ) as f :
152+ transcript = f .read ()
153+
154+ assert transcript == expected
266155
267156@pytest .mark .parametrize ('expected, transformed' , [
268157 # strings with zero or one slash or with escaped slashes means no regular
0 commit comments