test_ptyprocess.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. """winpty wrapper tests."""
  3. # yapf: disable
  4. # Standard library imports
  5. import os
  6. import signal
  7. import sys
  8. # Third party imports
  9. from flaky import flaky
  10. from winpty.ptyprocess import PtyProcess
  11. import pytest
  12. # yapf: enable
  13. @pytest.fixture(scope='module')
  14. def pty_fixture():
  15. def _pty_factory(cmd=None, env=None):
  16. cmd = cmd or 'cmd'
  17. return PtyProcess.spawn(cmd, env=env)
  18. return _pty_factory
  19. @flaky(max_runs=4, min_passes=1)
  20. def test_read(pty_fixture):
  21. pty = pty_fixture()
  22. loc = os.getcwd()
  23. data = ''
  24. while loc not in data:
  25. data += pty.read()
  26. pty.terminate()
  27. def test_write(pty_fixture):
  28. pty = pty_fixture()
  29. text = u'Eggs, ham and spam ünicode'
  30. pty.write(text)
  31. data = ''
  32. while text not in data:
  33. data += pty.read()
  34. pty.terminate()
  35. def test_isalive(pty_fixture):
  36. pty = pty_fixture()
  37. pty.write('exit\r\n')
  38. text = 'exit'
  39. data = ''
  40. while text not in data:
  41. data += pty.read()
  42. while 1:
  43. try:
  44. pty.read()
  45. except EOFError:
  46. break
  47. assert not pty.isalive()
  48. pty.terminate()
  49. def test_readline(pty_fixture):
  50. env = os.environ.copy()
  51. env['foo'] = 'bar'
  52. pty = pty_fixture(env=env)
  53. pty.write('echo %foo%\r\n')
  54. while 'bar' not in pty.readline():
  55. pass
  56. pty.terminate()
  57. def test_close(pty_fixture):
  58. pty = pty_fixture()
  59. pty.close()
  60. assert not pty.isalive()
  61. def test_flush(pty_fixture):
  62. pty = pty_fixture()
  63. pty.flush()
  64. pty.terminate()
  65. def test_intr(pty_fixture):
  66. pty = pty_fixture(cmd=[sys.executable, 'import time; time.sleep(10)'])
  67. pty.sendintr()
  68. assert pty.wait() != 0
  69. def test_send_control(pty_fixture):
  70. pty = pty_fixture(cmd=[sys.executable, 'import time; time.sleep(10)'])
  71. pty.sendcontrol('d')
  72. assert pty.wait() != 0
  73. def test_send_eof(pty_fixture):
  74. cat = pty_fixture('cat')
  75. cat.sendeof()
  76. assert cat.wait() == 0
  77. def test_isatty(pty_fixture):
  78. pty = pty_fixture()
  79. assert pty.isatty()
  80. pty.terminate()
  81. assert not pty.isatty()
  82. def test_wait(pty_fixture):
  83. pty = pty_fixture(cmd=[sys.executable, '--version'])
  84. assert pty.wait() == 0
  85. def test_exit_status(pty_fixture):
  86. pty = pty_fixture(cmd=[sys.executable])
  87. pty.write('import sys;sys.exit(1)\r\n')
  88. pty.wait()
  89. assert pty.exitstatus == 1
  90. def test_kill(pty_fixture):
  91. pty = pty_fixture()
  92. pty.kill(signal.SIGTERM)
  93. assert not pty.isalive()
  94. assert pty.exitstatus == signal.SIGTERM
  95. def test_getwinsize(pty_fixture):
  96. pty = pty_fixture()
  97. assert pty.getwinsize() == (24, 80)
  98. pty.terminate()
  99. def test_setwinsize(pty_fixture):
  100. pty = pty_fixture()
  101. pty.setwinsize(50, 110)
  102. assert pty.getwinsize() == (50, 110)
  103. pty.terminate()
  104. pty = PtyProcess.spawn('cmd', dimensions=(60, 120))
  105. assert pty.getwinsize() == (60, 120)
  106. pty.terminate()