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
@@ -76,111 +77,6 @@ def do_mumble(self, opts, arg):
7677 self .poutput (' ' .join (output ))
7778
7879
79- class DemoApp (cmd2 .Cmd ):
80- hello_parser = argparse .ArgumentParser ()
81- hello_parser .add_argument ('-n' , '--name' , help = "your name" )
82- @cmd2 .with_argparser_and_unknown_args (hello_parser )
83- def do_hello (self , opts , arg ):
84- """Says hello."""
85- if opts .name :
86- self .stdout .write ('Hello {}\n ' .format (opts .name ))
87- else :
88- self .stdout .write ('Hello Nobody\n ' )
89-
90-
91- @pytest .fixture
92- def _cmdline_app ():
93- c = CmdLineApp ()
94- c .stdout = StdOut ()
95- return c
96-
97-
98- def _get_transcript_blocks (transcript ):
99- cmd = None
100- expected = ''
101- for line in transcript .splitlines ():
102- if line .startswith ('(Cmd) ' ):
103- if cmd is not None :
104- yield cmd , normalize (expected )
105-
106- cmd = line [6 :]
107- expected = ''
108- else :
109- expected += line + '\n '
110- yield cmd , normalize (expected )
111-
112-
113- def test_base_with_transcript (_cmdline_app ):
114- app = _cmdline_app
115- transcript = """
116- (Cmd) help
117-
118- Documented commands (type help <topic>):
119- ========================================
120- alias help load orate pyscript say shell speak
121- edit history mumble py quit set shortcuts unalias
122-
123- (Cmd) help say
124- usage: speak [-h] [-p] [-s] [-r REPEAT]
125-
126- Repeats what you tell me to.
127-
128- optional arguments:
129- -h, --help show this help message and exit
130- -p, --piglatin atinLay
131- -s, --shout N00B EMULATION MODE
132- -r REPEAT, --repeat REPEAT
133- output [n] times
134-
135- (Cmd) say goodnight, Gracie
136- goodnight, Gracie
137- (Cmd) say -ps --repeat=5 goodnight, Gracie
138- OODNIGHT, GRACIEGAY
139- OODNIGHT, GRACIEGAY
140- OODNIGHT, GRACIEGAY
141- (Cmd) set maxrepeats 5
142- maxrepeats - was: 3
143- now: 5
144- (Cmd) say -ps --repeat=5 goodnight, Gracie
145- OODNIGHT, GRACIEGAY
146- OODNIGHT, GRACIEGAY
147- OODNIGHT, GRACIEGAY
148- OODNIGHT, GRACIEGAY
149- OODNIGHT, GRACIEGAY
150- (Cmd) history
151- -------------------------[1]
152- help
153- -------------------------[2]
154- help say
155- -------------------------[3]
156- say goodnight, Gracie
157- -------------------------[4]
158- say -ps --repeat=5 goodnight, Gracie
159- -------------------------[5]
160- set maxrepeats 5
161- -------------------------[6]
162- say -ps --repeat=5 goodnight, Gracie
163- (Cmd) history -r 4
164- OODNIGHT, GRACIEGAY
165- OODNIGHT, GRACIEGAY
166- OODNIGHT, GRACIEGAY
167- OODNIGHT, GRACIEGAY
168- OODNIGHT, GRACIEGAY
169- (Cmd) set prompt "---> "
170- prompt - was: (Cmd)
171- now: --->
172- """
173-
174- for cmd , expected in _get_transcript_blocks (transcript ):
175- out = run_cmd (app , cmd )
176- assert out == expected
177-
178-
179- class TestMyAppCase (cmd2 .Cmd2TestCase ):
180- CmdApp = CmdLineApp
181- CmdApp .testfiles = ['tests/transcript.txt' ]
182-
183-
18480def test_commands_at_invocation ():
18581 testargs = ["prog" , "say hello" , "say Gracie" , "quit" ]
18682 expected = "This is an intro banner ...\n hello\n Gracie\n "
@@ -191,8 +87,7 @@ def test_commands_at_invocation():
19187 out = app .stdout .buffer
19288 assert out == expected
19389
194-
195- @pytest .mark .parametrize ('filename, feedback_to_output' , [
90+ @pytest .mark .parametrize ('filename,feedback_to_output' , [
19691 ('bol_eol.txt' , False ),
19792 ('characterclass.txt' , False ),
19893 ('dotstar.txt' , False ),
@@ -231,6 +126,34 @@ def test_transcript(request, capsys, filename, feedback_to_output):
231126 assert err .startswith (expected_start )
232127 assert err .endswith (expected_end )
233128
129+ def test_history_transcript (request , capsys ):
130+ app = CmdLineApp ()
131+ app .stdout = StdOut ()
132+ run_cmd (app , 'help' )
133+ run_cmd (app , 'speak lots of wierd [ /tmp ]: chars?' )
134+ run_cmd (app , 'speak /this is not a regex/' )
135+
136+ # Get location of the expected transcript
137+ test_dir = os .path .dirname (request .module .__file__ )
138+ expected_fname = os .path .join (test_dir , 'transcripts' , 'expected_history.txt' )
139+ with open (expected_fname ) as f :
140+ lines = f .readlines ()
141+ # trim off the first 7 lines so we can have a comment in the
142+ # expected_history.txt file explaining what it is
143+ expected = '' .join (lines [7 :])
144+
145+ # make a tmp file
146+ fd , history_fname = tempfile .mkstemp (prefix = '' , suffix = '.txt' )
147+ os .close (fd )
148+
149+ # tell the history command to create a transcript
150+ run_cmd (app , 'history -t "{}"' .format (history_fname ))
151+
152+ # read in the transcript created by the history command
153+ with open (history_fname ) as f :
154+ transcript = f .read ()
155+
156+ assert transcript == expected
234157
235158@pytest .mark .parametrize ('expected, transformed' , [
236159 # strings with zero or one slash or with escaped slashes means no regular
0 commit comments