PageRenderTime 111ms CodeModel.GetById 36ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/test_misc.py

https://github.com/elitak/pexpect
Python | 169 lines | 166 code | 2 blank | 1 comment | 3 complexity | f8bfad99b4397c7604711839668a46bb MD5 | raw file
  1. #!/usr/bin/env python
  2. import pexpect
  3. import unittest
  4. import PexpectTestCase
  5. import time
  6. import os
  7. import re
  8. class TestCaseMisc(PexpectTestCase.PexpectTestCase):
  9. def test_isatty (self):
  10. child = pexpect.spawn('cat')
  11. assert child.isatty(), "Not returning True. Should always be True."
  12. def test_read (self):
  13. child = pexpect.spawn('cat')
  14. child.sendline ("abc")
  15. child.sendeof()
  16. assert child.read(0) == '', "read(0) did not return ''"
  17. assert child.read(1) == 'a', "read(1) did not return 'a'"
  18. assert child.read(1) == 'b', "read(1) did not return 'b'"
  19. assert child.read(1) == 'c', "read(1) did not return 'c'"
  20. assert child.read(2) == '\r\n', "read(2) did not return '\\r\\n'"
  21. assert child.read() == 'abc\r\n', "read() did not return 'abc\\r\\n'"
  22. def test_readline (self):
  23. child = pexpect.spawn('cat')
  24. child.sendline ("abc")
  25. child.sendline ("123")
  26. child.sendeof()
  27. line1 = child.readline(0)
  28. line2 = child.readline()
  29. line3 = child.readline(2)
  30. line4 = child.readline(1)
  31. line5 = child.readline()
  32. assert line1 == '', "readline(0) did not return ''. Returned: " + repr(line1)
  33. assert line2 == 'abc\r\n', "readline() did not return 'abc\\r\\n'. Returned: " + repr(line2)
  34. assert line3 == 'abc\r\n', "readline(2) did not return 'abc\\r\\n'. Returned: " + repr(line3)
  35. assert line4 == '123\r\n', "readline(1) did not return '123\\r\\n'. Returned: " + repr(line4)
  36. assert line5 == '123\r\n', "readline() did not return '123\\r\\n'. Returned: " + repr(line5)
  37. def test_iter (self):
  38. child = pexpect.spawn('cat')
  39. child.sendline ("abc")
  40. child.sendline ("123")
  41. child.sendeof()
  42. # Don't use "".join() because we want to test the ITERATOR.
  43. page = ""
  44. for line in child:
  45. page = page + line
  46. assert page == 'abc\r\nabc\r\n123\r\n123\r\n', "iterator did not work. page=%s"%repr(page)
  47. def test_readlines(self):
  48. child = pexpect.spawn('cat')
  49. child.sendline ("abc")
  50. child.sendline ("123")
  51. child.sendeof()
  52. page = child.readlines()
  53. page = ''.join(page)
  54. assert page == 'abc\r\nabc\r\n123\r\n123\r\n', "readlines() did not work. page=%s"%repr(page)
  55. def test_write (self):
  56. child = pexpect.spawn('cat')
  57. child.write('a')
  58. child.write('\r')
  59. assert child.readline() == 'a\r\n', "write() did not work"
  60. def test_writelines (self):
  61. child = pexpect.spawn('cat')
  62. child.writelines(['abc','123','xyz','\r'])
  63. child.sendeof()
  64. line = child.readline()
  65. assert line == 'abc123xyz\r\n', "writelines() did not work. line=%s"%repr(line)
  66. def test_eof(self):
  67. child = pexpect.spawn('cat')
  68. child.sendeof()
  69. try:
  70. child.expect ('the unexpected')
  71. except:
  72. pass
  73. assert child.eof(), "child.eof() did not return True"
  74. def test_terminate(self):
  75. child = pexpect.spawn('cat')
  76. child.terminate(force=1)
  77. assert child.terminated, "child.terminated is not True"
  78. def test_bad_child_pid(self):
  79. child = pexpect.spawn('cat')
  80. child.terminate(force=1)
  81. child.terminated = 0 # Force invalid state to test code
  82. try:
  83. child.isalive()
  84. except pexpect.ExceptionPexpect, e:
  85. pass
  86. else:
  87. self.fail ("child.isalive() should have raised a pexpect.ExceptionPexpect")
  88. child.terminated = 1 # Force back to valid state so __del__ won't complain
  89. def test_bad_arguments (self):
  90. """This tests that we get a graceful error when passing bad arguments."""
  91. try:
  92. p = pexpect.spawn(1)
  93. except pexpect.ExceptionPexpect, e:
  94. pass
  95. else:
  96. self.fail ("pexpect.spawn(1) should have raised a pexpect.ExceptionPexpect.")
  97. try:
  98. p = pexpect.spawn('ls', '-la') # should really use pexpect.spawn('ls', ['-ls'])
  99. except TypeError, e:
  100. pass
  101. else:
  102. self.fail ("pexpect.spawn('ls', '-la') should have raised a TypeError.")
  103. try:
  104. p = pexpect.spawn('cat')
  105. p.close()
  106. p.read_nonblocking(size=1, timeout=3)
  107. except ValueError, e:
  108. pass
  109. else:
  110. self.fail ("read_nonblocking on closed spawn object should have raised a ValueError.")
  111. def test_isalive(self):
  112. child = pexpect.spawn('cat')
  113. assert child.isalive(), "child.isalive() did not return True"
  114. child.sendeof()
  115. child.expect(pexpect.EOF)
  116. assert not child.isalive(), "child.isalive() did not return False"
  117. def test_bad_type_in_expect(self):
  118. child = pexpect.spawn('cat')
  119. try:
  120. child.expect({}) # We don't support dicts yet. Should give TypeError
  121. except TypeError, e:
  122. pass
  123. else:
  124. self.fail ("child.expect({}) should have raised a TypeError")
  125. def test_winsize(self):
  126. child = pexpect.spawn('cat')
  127. child.setwinsize(10,13)
  128. assert child.getwinsize()==(10,13), "getwinsize() did not return (10,13)"
  129. def test_env(self):
  130. default = pexpect.run('env')
  131. userenv = pexpect.run('env', env={'foo':'pexpect'})
  132. assert default!=userenv, "'default' and 'userenv' should be different"
  133. assert 'foo' in userenv and 'pexpect' in userenv, "'foo' and 'pexpect' should be in 'userenv'"
  134. def test_cwd (self): # This assumes 'pwd' and '/tmp' exist on this platform.
  135. default = pexpect.run('pwd')
  136. tmpdir = pexpect.run('pwd', cwd='/tmp')
  137. assert default!=tmpdir, "'default' and 'tmpdir' should be different"
  138. assert ('tmp' in tmpdir), "'tmp' should be returned by 'pwd' command"
  139. def test_which (self):
  140. p = os.defpath
  141. ep = os.environ['PATH']
  142. os.defpath = ":/tmp"
  143. os.environ['PATH'] = ":/tmp"
  144. wp = pexpect.which ("ticker.py")
  145. assert wp == 'ticker.py', "Should return a string. Returned %s" % wp
  146. os.defpath = "/tmp"
  147. os.environ['PATH'] = "/tmp"
  148. wp = pexpect.which ("ticker.py")
  149. assert wp == None, "Executable should not be found. Returned %s" % wp
  150. os.defpath = p
  151. os.environ['PATH'] = ep
  152. def test_searcher_re (self):
  153. ss = pexpect.searcher_re ([re.compile('this'),re.compile('that'),re.compile('and'),re.compile('the'),re.compile('other')])
  154. assert ss.__str__() == 'searcher_re:\n 0: re.compile("this")\n 1: re.compile("that")\n 2: re.compile("and")\n 3: re.compile("the")\n 4: re.compile("other")'
  155. ss = pexpect.searcher_re ([pexpect.TIMEOUT,re.compile('this'),re.compile('that'),re.compile('and'),pexpect.EOF,re.compile('other')])
  156. assert ss.__str__() == 'searcher_re:\n 0: TIMEOUT\n 1: re.compile("this")\n 2: re.compile("that")\n 3: re.compile("and")\n 4: EOF\n 5: re.compile("other")'
  157. def test_searcher_string (self):
  158. ss = pexpect.searcher_string (['this','that','and','the','other'])
  159. assert ss.__str__() == 'searcher_string:\n 0: "this"\n 1: "that"\n 2: "and"\n 3: "the"\n 4: "other"', repr(ss.__str__())
  160. ss = pexpect.searcher_string (['this',pexpect.EOF,'that','and','the','other',pexpect.TIMEOUT])
  161. assert ss.__str__() == 'searcher_string:\n 0: "this"\n 1: EOF\n 2: "that"\n 3: "and"\n 4: "the"\n 5: "other"\n 6: TIMEOUT'
  162. if __name__ == '__main__':
  163. unittest.main()
  164. suite = unittest.makeSuite(TestCaseMisc,'test')