test_winpty_wrapper.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. """winpty wrapper tests."""
  3. # yapf: disable
  4. # Standard library imports
  5. import os
  6. # Third party imports
  7. from flaky import flaky
  8. from winpty.winpty_wrapper import PTY, PY2
  9. from winpty.ptyprocess import which
  10. import pytest
  11. # yapf: enable
  12. CMD = which('cmd')
  13. if PY2:
  14. CMD = unicode(CMD) # noqa
  15. @pytest.fixture(scope='module')
  16. def pty_fixture():
  17. def _pty_factory():
  18. pty = PTY(80, 25)
  19. pty.spawn(CMD)
  20. return pty
  21. return _pty_factory
  22. @flaky(max_runs=4, min_passes=1)
  23. def test_read(pty_fixture):
  24. pty = pty_fixture()
  25. loc = os.getcwd()
  26. line = ''
  27. while loc not in line:
  28. line += pty.read().decode('utf-8')
  29. assert loc in line
  30. pty.close()
  31. del pty
  32. def test_write(pty_fixture):
  33. pty = pty_fixture()
  34. line = pty.read()
  35. while len(line) < 10:
  36. line = pty.read()
  37. text = u'Eggs, ham and spam ünicode'
  38. pty.write(text)
  39. line = u''
  40. while text not in line:
  41. line += pty.read().decode('utf-8')
  42. assert text in line
  43. pty.close()
  44. del pty
  45. def test_isalive(pty_fixture):
  46. pty = pty_fixture()
  47. pty.write(u'exit\r\n')
  48. text = u'exit'
  49. line = u''
  50. while text not in line:
  51. line += pty.read().decode('utf-8')
  52. while pty.isalive():
  53. pty.read()
  54. continue
  55. assert not pty.isalive()
  56. pty.close()
  57. del pty