@@ -1369,7 +1369,6 @@ def test_eos(base_app):
1369
1369
# And make sure it reduced the length of the script dir list
1370
1370
assert len (base_app ._script_dir ) == 0
1371
1371
1372
-
1373
1372
def test_echo (capsys ):
1374
1373
app = cmd2 .Cmd ()
1375
1374
# Turn echo on and pre-stage some commands in the queue, simulating like we are in the middle of a script
@@ -1390,112 +1389,72 @@ def test_echo(capsys):
1390
1389
assert app ._current_script_dir is None
1391
1390
assert out .startswith ('{}{}\n ' .format (app .prompt , command ) + 'history [arg]: lists past commands issued' )
1392
1391
1393
- def test_piped_input_echo_false_rawinput_false (capsys ):
1394
- command = 'set'
1395
-
1396
- # mock up the input
1397
- fakein = io .StringIO (command )
1392
+ # the next helper function and two tests check for piped
1393
+ # input when use_rawinput is True.
1394
+ #
1395
+ # the only way to make this testable is to mock the builtin input()
1396
+ # function
1397
+ def piped_input_rawinput_true (capsys , echo , command ):
1398
+ m = mock .Mock (name = 'input' , side_effect = [command , 'quit' ])
1399
+ sm .input = m
1398
1400
1399
- # run the cmdloop, which should pull input from stdin
1400
- app = cmd2 .Cmd (stdin = fakein )
1401
- app .use_rawinput = False
1402
- app .echo = False
1401
+ # run the cmdloop, which should pull input from our mocked input_list
1402
+ app = cmd2 .Cmd ()
1403
+ app .use_rawinput = True
1404
+ app .echo = echo
1403
1405
app .abbrev = False
1404
1406
app ._cmdloop ()
1405
1407
out , err = capsys .readouterr ()
1408
+ return (app , out )
1409
+
1410
+ def test_piped_input_rawinput_true_echo_true (capsys ):
1411
+ command = 'set'
1412
+ app , out = piped_input_rawinput_true (capsys , True , command )
1413
+ out = out .splitlines ()
1414
+ assert out [0 ] == '{}{}' .format (app .prompt , command )
1415
+ assert out [1 ] == 'abbrev: False'
1406
1416
1417
+ def test_piped_input_rawinput_true_echo_false (capsys ):
1418
+ command = 'set'
1419
+ app , out = piped_input_rawinput_true (capsys , False , command )
1407
1420
firstline = out .splitlines ()[0 ]
1408
1421
assert firstline == 'abbrev: False'
1409
1422
assert not '{}{}' .format (app .prompt , command ) in out
1410
1423
1424
+ # the next helper function and two tests check for piped
1425
+ # input when use_rawinput is False
1411
1426
#
1412
- # WARNING:
1413
- #
1414
- # this test passes, and validates the proper behavior of
1415
- # cmd2.pseudo_raw_input() when use_rawinput = True
1416
- #
1417
- # However, there is only one way to patch/mock/hack input()
1418
- # or raw_input() for testing: that is to change the
1419
- # sys.stdin file descriptor. This results in unpredictable
1420
- # failures when 'pytest -n8' parallelizes tests.
1421
- #
1422
- # @pytest.mark.parametrize('rawinput', [True, False])
1423
- # def test_piped_input_echo_false_stdin_hack(capsys, rawinput):
1424
- # command = 'set'
1425
- #
1426
- # # hack up stdin
1427
- # fakein = io.StringIO(command)
1428
- # realin = sys.stdin
1429
- # sys.stdin = fakein
1430
- #
1431
- # # run the cmdloop, which should pull input from stdin
1432
- # app = cmd2.Cmd(stdin=fakein)
1433
- # app.use_rawinput = rawinput
1434
- # app.echo = False
1435
- # app.abbrev = False
1436
- # app._cmdloop()
1437
- # out, err = capsys.readouterr()
1438
- #
1439
- # # put stdin back
1440
- # sys.stdin = realin
1441
- #
1442
- # firstline = out.splitlines()[0]
1443
- # assert firstline == 'abbrev: False'
1444
- # assert not '{}{}'.format(app.prompt, command) in out
1445
-
1446
- def test_piped_input_echo_true_rawinput_false (capsys ):
1447
- command = 'set'
1448
-
1427
+ # the only way to make this testable is to pass a file handle
1428
+ # as stdin
1429
+ def piped_input_rawinput_false (capsys , echo , command ):
1449
1430
# mock up the input
1450
1431
fakein = io .StringIO (command )
1451
1432
1452
1433
# run the cmdloop, which should pull input from stdin
1453
1434
app = cmd2 .Cmd (stdin = fakein )
1454
1435
app .use_rawinput = False
1455
- app .echo = True
1436
+ app .echo = echo
1456
1437
app .abbrev = False
1457
1438
app ._cmdloop ()
1458
1439
out , err = capsys .readouterr ()
1440
+ return (app , out )
1459
1441
1442
+ def test_piped_input_rawinput_false_echo_true (capsys ):
1443
+ command = 'set'
1444
+ app , out = piped_input_rawinput_false (capsys , True , command )
1460
1445
out = out .splitlines ()
1461
1446
assert out [0 ] == '{}{}' .format (app .prompt , command )
1462
1447
assert out [1 ] == 'abbrev: False'
1463
1448
1464
- #
1465
- # WARNING:
1466
- #
1467
- # this test passes, and validates the proper behavior of
1468
- # cmd2.pseudo_raw_input() when use_rawinput = True
1469
- #
1470
- # However, there is only one way to patch/mock/hack input()
1471
- # or raw_input() for testing: that is to change the
1472
- # sys.stdin file descriptor. This results in unpredictable
1473
- # failures when 'pytest -n8' parallelizes tests.
1474
- #
1475
- # @pytest.mark.parametrize('rawinput', [True, False])
1476
- # def test_piped_input_echo_true_stdin_hack(capsys, rawinput):
1477
- # command = 'set'
1478
- #
1479
- # # hack up stdin
1480
- # fakein = io.StringIO(command)
1481
- # realin = sys.stdin
1482
- # sys.stdin = fakein
1483
- #
1484
- # # run the cmdloop, which should pull input from stdin
1485
- # app = cmd2.Cmd()
1486
- # app.use_rawinput = rawinput
1487
- # app.echo = True
1488
- # app.abbrev = False
1489
- # app._cmdloop()
1490
- # out, err = capsys.readouterr()
1491
- #
1492
- # # put stdin back
1493
- # sys.stdin = realin
1494
- #
1495
- # out = out.splitlines()
1496
- # assert out[0] == '{}{}'.format(app.prompt, command)
1497
- # assert out[1] == 'abbrev: False'
1449
+ def test_piped_input_rawinput_false_echo_false (capsys ):
1450
+ command = 'set'
1451
+ app , out = piped_input_rawinput_false (capsys , False , command )
1452
+ firstline = out .splitlines ()[0 ]
1453
+ assert firstline == 'abbrev: False'
1454
+ assert not '{}{}' .format (app .prompt , command ) in out
1498
1455
1456
+ #
1457
+ # other input tests
1499
1458
def test_raw_input (base_app ):
1500
1459
base_app .use_raw_input = True
1501
1460
fake_input = 'quit'
0 commit comments