Skip to content

Commit ae42c7c

Browse files
committed
Discovered hacking sys.stdin doesn’t always work
When ‘pytest -n8’ parallelizes the test execution, hacking sys.stdin to some other file descriptor yields unpredictable results
1 parent dde0b69 commit ae42c7c

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed

tests/test_cmd2.py

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,14 +1390,11 @@ def test_echo(capsys):
13901390
assert app._current_script_dir is None
13911391
assert out.startswith('{}{}\n'.format(app.prompt, command) + 'history [arg]: lists past commands issued')
13921392

1393-
#@pytest.mark.parametrize('rawinput', [True, False])
1394-
def test_piped_input_echo_false(capsys):
1393+
def test_piped_input_echo_false_rawinput_false(capsys):
13951394
command = 'set'
13961395

1397-
# hack up stdin
1396+
# mock up the input
13981397
fakein = io.StringIO(command)
1399-
#realin = sys.stdin
1400-
#sys.stdin = fakein
14011398

14021399
# run the cmdloop, which should pull input from stdin
14031400
app = cmd2.Cmd(stdin=fakein)
@@ -1407,21 +1404,50 @@ def test_piped_input_echo_false(capsys):
14071404
app._cmdloop()
14081405
out, err = capsys.readouterr()
14091406

1410-
# put stdin back
1411-
#sys.stdin = realin
1412-
14131407
firstline = out.splitlines()[0]
14141408
assert firstline == 'abbrev: False'
14151409
assert not '{}{}'.format(app.prompt, command) in out
14161410

1417-
#@pytest.mark.parametrize('rawinput', [True, False])
1418-
def test_piped_input_echo_true(capsys):
1411+
#
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):
14191447
command = 'set'
14201448

1421-
# hack up stdin
1449+
# mock up the input
14221450
fakein = io.StringIO(command)
1423-
# realin = sys.stdin
1424-
# sys.stdin = fakein
14251451

14261452
# run the cmdloop, which should pull input from stdin
14271453
app = cmd2.Cmd(stdin=fakein)
@@ -1431,13 +1457,45 @@ def test_piped_input_echo_true(capsys):
14311457
app._cmdloop()
14321458
out, err = capsys.readouterr()
14331459

1434-
# put stdin back
1435-
# sys.stdin = realin
1436-
14371460
out = out.splitlines()
14381461
assert out[0] == '{}{}'.format(app.prompt, command)
14391462
assert out[1] == 'abbrev: False'
1440-
1463+
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'
1498+
14411499
def test_raw_input(base_app):
14421500
base_app.use_raw_input = True
14431501
fake_input = 'quit'

0 commit comments

Comments
 (0)