test_pty.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. """winpty wrapper tests."""
  3. # Standard library imports
  4. import os
  5. import time
  6. # Third party imports
  7. from winpty import PTY, WinptyError
  8. from winpty.enums import Backend
  9. from winpty.ptyprocess import which
  10. import pytest
  11. CMD = which('cmd').lower()
  12. def pty_factory(backend):
  13. if os.environ.get('CI_RUNNING', None) == '1':
  14. if backend == Backend.ConPTY:
  15. os.environ['CONPTY_CI'] = '1'
  16. elif backend == Backend.WinPTY:
  17. os.environ.pop('CONPTY_CI', None)
  18. @pytest.fixture(scope='function')
  19. def pty_fixture():
  20. pty = PTY(80, 20, backend=backend)
  21. # loc = bytes(os.getcwd(), 'utf8')
  22. assert pty.spawn(CMD)
  23. time.sleep(0.3)
  24. # if backend == Backend.ConPTY:
  25. # pty.write("\x1b[?1;0c\x1b[0;0R")
  26. return pty
  27. return pty_fixture
  28. conpty_provider = pty_factory(Backend.ConPTY)
  29. winpty_provider = pty_factory(Backend.WinPTY)
  30. @pytest.fixture(scope='module', params=['WinPTY', 'ConPTY'])
  31. def pty_fixture(request):
  32. backend = request.param
  33. if os.environ.get('CI_RUNNING', None) == '1':
  34. if backend == 'ConPTY':
  35. os.environ['CI'] = '1'
  36. os.environ['CONPTY_CI'] = '1'
  37. if backend == 'WinPTY':
  38. os.environ.pop('CI', None)
  39. os.environ.pop('CONPTY_CI', None)
  40. backend = getattr(Backend, backend)
  41. def _pty_factory():
  42. try:
  43. pty = PTY(80, 25, backend=backend)
  44. except WinptyError:
  45. pytest.skip()
  46. return None
  47. assert pty.spawn(CMD)
  48. time.sleep(0.3)
  49. return pty
  50. return _pty_factory
  51. # @pytest.fixture(scope='function', params=[
  52. # pytest.lazy_fixture('conpty_provider'),
  53. # pytest.lazy_fixture('winpty_provider')])
  54. # def pty_fixture(request):
  55. # pty = request.param
  56. # return pty
  57. def test_read(pty_fixture, capsys):
  58. pty = pty_fixture()
  59. loc = os.getcwd()
  60. readline = ''
  61. with capsys.disabled():
  62. start_time = time.time()
  63. while loc not in readline:
  64. if time.time() - start_time > 5:
  65. break
  66. readline += pty.read()
  67. assert loc in readline or 'cmd' in readline
  68. del pty
  69. def test_write(pty_fixture):
  70. pty = pty_fixture()
  71. line = pty.read()
  72. str_text = 'Eggs, ham and spam ünicode'
  73. # text = bytes(str_text, 'utf-8')
  74. num_bytes = pty.write(str_text)
  75. line = ''
  76. start_time = time.time()
  77. while str_text not in line:
  78. if time.time() - start_time > 5:
  79. break
  80. line += pty.read()
  81. assert str_text in line
  82. del pty
  83. def test_isalive(pty_fixture):
  84. pty = pty_fixture()
  85. pty.write('exit\r\n')
  86. text = 'exit'
  87. line = ''
  88. while text not in line:
  89. try:
  90. line += pty.read()
  91. except Exception:
  92. break
  93. while pty.isalive():
  94. try:
  95. pty.read()
  96. # continue
  97. except Exception:
  98. break
  99. assert not pty.isalive()
  100. del pty
  101. # def test_agent_spawn_fail(pty_fixture):
  102. # pty = pty_fixture
  103. # try:
  104. # pty.spawn(CMD)
  105. # assert False
  106. # except WinptyError:
  107. # pass
  108. # @pytest.mark.parametrize(
  109. # 'backend_name,backend',
  110. # [("ConPTY", Backend.ConPTY), ('WinPTY', Backend.WinPTY)])
  111. # def test_pty_create_size_fail(backend_name, backend):
  112. # try:
  113. # PTY(80, -25, backend=backend)
  114. # assert False
  115. # except WinptyError:
  116. # pass
  117. # def test_agent_resize_fail(pty_fixture):
  118. # pty = pty_fixture()
  119. # try:
  120. # pty.set_size(-80, 70)
  121. # assert False
  122. # except WinptyError:
  123. # pass
  124. # finally:
  125. # del pty
  126. def test_agent_resize(pty_fixture):
  127. pty = pty_fixture()
  128. pty.set_size(80, 70)
  129. del pty