77"""
88import os
99import sys
10+ import io
1011import tempfile
1112
1213import mock
@@ -849,6 +850,7 @@ def test_cmdloop_without_rawinput():
849850 # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
850851 app = cmd2 .Cmd ()
851852 app .use_rawinput = False
853+ app .echo = False
852854 app .intro = 'Hello World, this is an intro ...'
853855 app .stdout = StdOut ()
854856
@@ -858,7 +860,7 @@ def test_cmdloop_without_rawinput():
858860
859861 # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
860862 testargs = ["prog" ]
861- expected = app .intro + '\n {}' . format ( app . prompt )
863+ expected = app .intro + '\n '
862864 with mock .patch .object (sys , 'argv' , testargs ):
863865 # Run the command loop
864866 app .cmdloop ()
@@ -1367,7 +1369,6 @@ def test_eos(base_app):
13671369 # And make sure it reduced the length of the script dir list
13681370 assert len (base_app ._script_dir ) == 0
13691371
1370-
13711372def test_echo (capsys ):
13721373 app = cmd2 .Cmd ()
13731374 # Turn echo on and pre-stage some commands in the queue, simulating like we are in the middle of a script
@@ -1388,7 +1389,106 @@ def test_echo(capsys):
13881389 assert app ._current_script_dir is None
13891390 assert out .startswith ('{}{}\n ' .format (app .prompt , command ) + 'history [arg]: lists past commands issued' )
13901391
1391-
1392+ def test_pseudo_raw_input_tty_rawinput_true ():
1393+ # use context managers so original functions get put back when we are done
1394+ # we dont use decorators because we need m_input for the assertion
1395+ with mock .patch ('sys.stdin.isatty' ,
1396+ mock .MagicMock (name = 'isatty' , return_value = True )):
1397+ with mock .patch ('six.moves.input' ,
1398+ mock .MagicMock (name = 'input' , side_effect = ['set' , EOFError ])) as m_input :
1399+ # run the cmdloop, which should pull input from our mocks
1400+ app = cmd2 .Cmd ()
1401+ app .use_rawinput = True
1402+ app ._cmdloop ()
1403+ # because we mocked the input() call, we won't get the prompt
1404+ # or the name of the command in the output, so we can't check
1405+ # if its there. We assume that if input got called twice, once
1406+ # for the 'set' command, and once for the 'quit' command,
1407+ # that the rest of it worked
1408+ assert m_input .call_count == 2
1409+
1410+ def test_pseudo_raw_input_tty_rawinput_false ():
1411+ # gin up some input like it's coming from a tty
1412+ fakein = io .StringIO (u'{}' .format ('set\n ' ))
1413+ mtty = mock .MagicMock (name = 'isatty' , return_value = True )
1414+ fakein .isatty = mtty
1415+ mreadline = mock .MagicMock (name = 'readline' , wraps = fakein .readline )
1416+ fakein .readline = mreadline
1417+
1418+ # run the cmdloop, telling it where to get input from
1419+ app = cmd2 .Cmd (stdin = fakein )
1420+ app .use_rawinput = False
1421+ app ._cmdloop ()
1422+
1423+ # because we mocked the readline() call, we won't get the prompt
1424+ # or the name of the command in the output, so we can't check
1425+ # if its there. We assume that if readline() got called twice, once
1426+ # for the 'set' command, and once for the 'quit' command,
1427+ # that the rest of it worked
1428+ assert mreadline .call_count == 2
1429+
1430+ # the next helper function and two tests check for piped
1431+ # input when use_rawinput is True.
1432+ def piped_rawinput_true (capsys , echo , command ):
1433+ app = cmd2 .Cmd ()
1434+ app .use_rawinput = True
1435+ app .echo = echo
1436+ # run the cmdloop, which should pull input from our mock
1437+ app ._cmdloop ()
1438+ out , err = capsys .readouterr ()
1439+ return (app , out )
1440+
1441+ # using the decorator puts the original function at six.moves.input
1442+ # back when this method returns
1443+ @mock .patch ('six.moves.input' ,
1444+ mock .MagicMock (name = 'input' , side_effect = ['set' , EOFError ]))
1445+ def test_pseudo_raw_input_piped_rawinput_true_echo_true (capsys ):
1446+ command = 'set'
1447+ app , out = piped_rawinput_true (capsys , True , command )
1448+ out = out .splitlines ()
1449+ assert out [0 ] == '{}{}' .format (app .prompt , command )
1450+ assert out [1 ] == 'abbrev: False'
1451+
1452+ # using the decorator puts the original function at six.moves.input
1453+ # back when this method returns
1454+ @mock .patch ('six.moves.input' ,
1455+ mock .MagicMock (name = 'input' , side_effect = ['set' , EOFError ]))
1456+ def test_pseudo_raw_input_piped_rawinput_true_echo_false (capsys ):
1457+ command = 'set'
1458+ app , out = piped_rawinput_true (capsys , False , command )
1459+ firstline = out .splitlines ()[0 ]
1460+ assert firstline == 'abbrev: False'
1461+ assert not '{}{}' .format (app .prompt , command ) in out
1462+
1463+ # the next helper function and two tests check for piped
1464+ # input when use_rawinput=False
1465+ def piped_rawinput_false (capsys , echo , command ):
1466+ fakein = io .StringIO (u'{}' .format (command ))
1467+ # run the cmdloop, telling it where to get input from
1468+ app = cmd2 .Cmd (stdin = fakein )
1469+ app .use_rawinput = False
1470+ app .echo = echo
1471+ app .abbrev = False
1472+ app ._cmdloop ()
1473+ out , err = capsys .readouterr ()
1474+ return (app , out )
1475+
1476+ def test_pseudo_raw_input_piped_rawinput_false_echo_true (capsys ):
1477+ command = 'set'
1478+ app , out = piped_rawinput_false (capsys , True , command )
1479+ out = out .splitlines ()
1480+ assert out [0 ] == '{}{}' .format (app .prompt , command )
1481+ assert out [1 ] == 'abbrev: False'
1482+
1483+ def test_pseudo_raw_input_piped_rawinput_false_echo_false (capsys ):
1484+ command = 'set'
1485+ app , out = piped_rawinput_false (capsys , False , command )
1486+ firstline = out .splitlines ()[0 ]
1487+ assert firstline == 'abbrev: False'
1488+ assert not '{}{}' .format (app .prompt , command ) in out
1489+
1490+ #
1491+ # other input tests
13921492def test_raw_input (base_app ):
13931493 base_app .use_raw_input = True
13941494 fake_input = 'quit'
0 commit comments