@@ -1390,37 +1390,37 @@ def test_echo(capsys):
1390
1390
assert out .startswith ('{}{}\n ' .format (app .prompt , command ) + 'history [arg]: lists past commands issued' )
1391
1391
1392
1392
def test_pseudo_raw_input_tty_rawinput_true ():
1393
- minput = mock .MagicMock (name = 'input' , side_effect = ['set' , 'quit' ])
1394
- sm .input = minput
1395
- mtty = mock .MagicMock (name = 'isatty' , return_value = True )
1396
- sys .stdin .isatty = mtty
1397
-
1398
- # run the cmdloop, which should pull input from the mocked input
1399
- app = cmd2 .Cmd ()
1400
- app .use_rawinput = True
1401
- app ._cmdloop ()
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' , 'quit' ])) 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
1402
1409
1403
- # because we mocked the input() call, we won't see 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 minput .call_count == 2
1409
-
1410
1410
def test_pseudo_raw_input_tty_rawinput_false ():
1411
- # mock up the input
1411
+ # gin up some input like it's coming from a tty
1412
1412
fakein = io .StringIO (u'{}' .format ('set\n quit\n ' ))
1413
1413
mtty = mock .MagicMock (name = 'isatty' , return_value = True )
1414
1414
fakein .isatty = mtty
1415
1415
mreadline = mock .MagicMock (name = 'readline' , wraps = fakein .readline )
1416
1416
fakein .readline = mreadline
1417
-
1417
+
1418
1418
# run the cmdloop, telling it where to get input from
1419
1419
app = cmd2 .Cmd (stdin = fakein )
1420
1420
app .use_rawinput = False
1421
1421
app ._cmdloop ()
1422
1422
1423
- # because we mocked the readline() call, we won't see the prompt
1423
+ # because we mocked the readline() call, we won't get the prompt
1424
1424
# or the name of the command in the output, so we can't check
1425
1425
# if its there. We assume that if readline() got called twice, once
1426
1426
# for the 'set' command, and once for the 'quit' command,
@@ -1429,29 +1429,28 @@ def test_pseudo_raw_input_tty_rawinput_false():
1429
1429
1430
1430
# the next helper function and two tests check for piped
1431
1431
# input when use_rawinput is True.
1432
- #
1433
- # the only way to make this testable is to mock the builtin input()
1434
- # function
1435
1432
def piped_rawinput_true (capsys , echo , command ):
1436
- m = mock .Mock (name = 'input' , side_effect = [command , 'quit' ])
1437
- sm .input = m
1438
-
1439
- # run the cmdloop, which should pull input from our mocked input
1440
1433
app = cmd2 .Cmd ()
1441
1434
app .use_rawinput = True
1442
1435
app .echo = echo
1443
- app . abbrev = False
1436
+ # run the cmdloop, which should pull input from our mock
1444
1437
app ._cmdloop ()
1445
1438
out , err = capsys .readouterr ()
1446
1439
return (app , out )
1447
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' , mock .MagicMock (name = 'input' , side_effect = ['set' , 'quit' ]))
1448
1444
def test_pseudo_raw_input_piped_rawinput_true_echo_true (capsys ):
1449
1445
command = 'set'
1450
1446
app , out = piped_rawinput_true (capsys , True , command )
1451
1447
out = out .splitlines ()
1452
1448
assert out [0 ] == '{}{}' .format (app .prompt , command )
1453
1449
assert out [1 ] == 'abbrev: False'
1454
1450
1451
+ # using the decorator puts the original function at six.moves.input
1452
+ # back when this method returns
1453
+ @mock .patch ('six.moves.input' , mock .MagicMock (name = 'input' , side_effect = ['set' , 'quit' ]))
1455
1454
def test_pseudo_raw_input_piped_rawinput_true_echo_false (capsys ):
1456
1455
command = 'set'
1457
1456
app , out = piped_rawinput_true (capsys , False , command )
@@ -1460,14 +1459,9 @@ def test_pseudo_raw_input_piped_rawinput_true_echo_false(capsys):
1460
1459
assert not '{}{}' .format (app .prompt , command ) in out
1461
1460
1462
1461
# the next helper function and two tests check for piped
1463
- # input when use_rawinput is False
1464
- #
1465
- # the only way to make this testable is to pass a file handle
1466
- # as stdin
1462
+ # input when use_rawinput=False
1467
1463
def piped_rawinput_false (capsys , echo , command ):
1468
- # mock up the input
1469
1464
fakein = io .StringIO (u'{}' .format (command ))
1470
-
1471
1465
# run the cmdloop, telling it where to get input from
1472
1466
app = cmd2 .Cmd (stdin = fakein )
1473
1467
app .use_rawinput = False
0 commit comments